App-dropboxapi

 view release on metacpan or  search on metacpan

script/dropbox-api  view on Meta::CPAN

                return 'true';
            } else {
                return 'false';
            }
        } else {
            return exists $content->{ $key } ? $content->{ $key } : '-';
        }
    };
    if ($printf) {
        my $line = eval qq{"$printf"};
        if ($content->{server_modified}) {
            $line=~s/\%T([^\%])/
                $dt ||= $strpz->parse_datetime($content->{server_modified});
                $dt->strftime('%'.$1);
            /egx;
        } else {
            $line=~s/\%TY/----/g;
            $line=~s/\%T([^\%])/--/g;
        }
        if ($content->{client_modified}) {
            $line=~s/\%C([^\%])/
                $ct ||= $strpz->parse_datetime($content->{client_modified});
                $ct->strftime('%'.$1);
            /egx;
        } else {
            $line=~s/\%CY/----/g;
            $line=~s/\%C([^\%])/--/g;
        }
        $line=~s|\%([^\%])|$get->($1)|eg;
        return $line;
    } else {
        return sprintf "%s %8s %s %s\n",
            ($content->{'.tag'} eq 'folder' ? 'd' : '-'),
            $get->($human ? 's' : 'b'),
            $get->('t'),
            $content->{path_display};
    }
}

sub find {
    my $remote_base = decode('locale_fs', slash(shift));
    if ($remote_base eq '/') {
        $remote_base = '';
    }
    $printf ||= "%p\n";
    my $entries = _find($remote_base);
    for my $entry (@{ $entries }) {
        print &_line($entry);
    }
}

sub _find ($) {
    my $remote_base = decode('locale_fs', slash(shift));
    if ($remote_base eq '/') {
        $remote_base = '';
    }
    my @entries;
    my $fetch;
    my $count = 0;
    $fetch = sub {
        my $cursor = shift;
        my $list;
        if ($cursor) {
            $list = $box->list_folder_continue($cursor) or die $box->error;
        } else {
            $list = $box->list_folder($remote_base, {
                recursive => JSON::true,
            }) or die $box->error;
        }
        push @entries, @{ $list->{entries} };
        if ($list->{has_more}) {
            if ($verbose) {
                $| = 1;
                $count++;
                printf("\r" . (('.') x $count));
            }
            $fetch->($list->{cursor});
        }
    };
    $fetch->();
    if ($verbose) {
        print "\n";
    }
    [ sort { $a->{path_lower} cmp $b->{path_lower} } @entries ];
}

sub copy {
    my ($src, $dst) = @_;
    my $res = $box->copy(decode('locale_fs', slash($src)), decode('locale_fs', slash($dst))) or die $box->error;
    print pretty($res) if $verbose;
}

sub move {
    my ($src, $dst) = @_;
    my $res = $box->move(decode('locale_fs', slash($src)), decode('locale_fs', slash($dst))) or die $box->error;
    print pretty($res) if $verbose;
}

sub mkdir {
    my ($dir) = @_;
    my $res = $box->create_folder(decode('locale_fs', slash($dir))) or die $box->error;
    print pretty($res) if $verbose;
}

sub delete {
    my ($file_or_dir) = @_;
    my $res = $box->delete(decode('locale_fs', slash($file_or_dir))) or die $box->error;
    print pretty($res) if $verbose;
}

sub upload {
    my ($file, $path) = @_;
    $path =~ s|^dropbox:/|/|
        or die "Usage: \n    dropbox-api upload /tmp/local.txt dropbox:/Public/some.txt";
    my $local_path = file($file);
    if ((! length $path) or $path =~ m|/$|) {
        $path.= basename($file);
    }
    my $res = &put($local_path, decode('locale_fs', $path)) or die $box->error;

    if ($verbose) {
        print pretty($res);
    }

    my $id = $res->{id};

    if ($public) {
        my $list_shared_links = $box->api({
            url => 'https://api.dropboxapi.com/2/sharing/list_shared_links',
            params => {
                path => $id,
            }
        }) or die $box->error;
        for (@{ $list_shared_links->{links} }) {
            if ($id eq $_->{id} && $_->{link_permissions}{resolved_visibility}{'.tag'} eq 'public') {
                print $_->{url}, "\n";
                return;

script/dropbox-api  view on Meta::CPAN

        printf "local:  %10s %10s %s\n", $local_epoch, $local_size, decode('locale_fs', $local_path);
    }

    if (($remote_size != $local_size) || ($remote_epoch != $local_epoch)) {
        return 1;
    }

    return;
}

sub put {
    my ($file, $path, $optional_params) = @_;

    my $commit_params = {
        path => "$path",
        mode => 'overwrite',
        %{ $optional_params || +{} },
    };

    my $content = $file->openr;
    my $size = -s $file;
    my $threshold = 10 * 1024 * 1024;

    if ($size < $threshold) {
        return $box->upload("$path", $content, $commit_params);
    }

    my $session_id;
    my $offset = 0;

    my $limit = 4 * 1024 * 1024;

    $| = 1;

    my $upload;
    $upload = sub {
        my $buf;
        my $total = 0;
        my $chunk = 1024;
        my $tmp = File::Temp->new;
        my $is_last;
        while (my $read = read($content, $buf, $chunk)) {
            $tmp->print($buf);
            $total += $read;
            my $remaining = $limit - $total;
            if ($chunk > $remaining) {
                $chunk = $remaining;
            }
            unless ($chunk) {
                last;
            }
        }

        $tmp->flush;
        $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;
}



( run in 1.827 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )