App-dropboxapi

 view release on metacpan or  search on metacpan

script/dropbox-api  view on Meta::CPAN

        $tmp->seek(0, 0);

        # finish or small file
        if ($total < $limit) {
            if ($session_id) {
                my $params = {
                    cursor => {
                        session_id => $session_id,
                        offset     => $offset,
                    },
                    commit => $commit_params,
                };
                return $box->upload_session_finish($tmp, $params);
            } else {
                return $box->upload("$path", $tmp, $commit_params);
            }
        }

        # append
        elsif ($session_id) {
            my $params = {
                cursor => {
                    session_id => $session_id,
                    offset     => $offset,
                },
            };
            unless ($box->upload_session_append_v2($tmp, $params)) {
                # some error
                return;
            }
            $offset += $total;
        }

        # start
        else {
            my $res = $box->upload_session_start($tmp);
            if ($res && $res->{session_id}) {
                $session_id = $res->{session_id};
                $offset = $total;
            } else {
                # some error
                return;
            }
        }

        # ProgressBar
        if ($verbose) {
            my $rate = sprintf('%2.1d%%', $offset / $size * 100);
            my $bar = '=' x int(($cols - length($rate) - 4) * $offset / $size);
            my $space = ' ' x ($cols - length($rate) - length($bar) - 4);
            printf "\r%s [%s>%s]", $rate, $bar, $space;
        }

        $upload->();
    };
    $upload->();
}

sub inode ($) {
    my $path = shift;
    my ($dev, $inode) = stat($path);
    return $dev . ':' . $inode if $inode;
    return $path;
}

sub remote_abs2rel ($$) {
    my ($remote_path, $remote_base) = @_;
    $remote_path =~ s|^\Q$remote_base\E/?||i;
    return $remote_path;
}

sub slash ($) {
    my $path = shift;
    unless (defined $path) {
        return '/';
    }
    if ($path !~ qr{ \A / }xms) {
        $path = '/' . $path;
    }
    $path;
}

sub chomp_slash ($) {
    my $path = shift;
    unless (defined $path) {
        return '';
    }
    $path =~ s|/$||;
    $path;
}

sub pretty($) {
    JSON->new->utf8->pretty->encode($_[0]);
}

use constant UNITS => [
    [ 'P', 1024 ** 4 * 1000, 1024 ** 5 ],
    [ 'T', 1024 ** 3 * 1000, 1024 ** 4 ],
    [ 'G', 1024 ** 2 * 1000, 1024 ** 3 ],
    [ 'M', 1024 * 1000, 1024 ** 2 ],
    [ 'K', 1000, 1024 ],
    [ 'B', 0, 1 ],
];

sub format_bytes ($) {
    my $size = shift;
    if ($size > 0) {
        for my $unit (@{ UNITS() }) {
            my ($unit_label, $unit_min, $unit_value) = @{ $unit };
            if ($size >= $unit_min) {
                my $size_unit = $size / $unit_value;
                if (round($size_unit) < 10) {
                    return sprintf('%1.1f%s', nearest(.1, $size_unit), $unit_label);
                } else {
                    return sprintf('%3s%s', round($size_unit), $unit_label);
                }
            }
        }
    }
    return '  0B';
}



( run in 1.600 second using v1.01-cache-2.11-cpan-39bf76dae61 )