App-dropboxapi

 view release on metacpan or  search on metacpan

script/dropbox-api  view on Meta::CPAN

    print "> dropbox-api find /\n";

    $config_file->openw->print(encode_json($config));

    chmod 0600, $config_file;

    exit(0);
}

sub du {
    my $remote_base = decode('locale_fs', slash(shift));
    $remote_base =~ s|/$||;
    my $entries = _find($remote_base);
    my $dir_map = {};
    for my $content (@{ $entries }) {
        if ($content->{'.tag'} eq 'folder') {
            next;
        }
        my @paths = _paths($remote_base, $content->{path_lower});
        for my $path (@paths) {
            $dir_map->{ lc $path } ||= 0;

script/dropbox-api  view on Meta::CPAN

        if ($dir ne '/') {
            $dir .= '/';
        }
        $dir .= $name;
        push @paths, $dir;
    }
    return @paths;
}

sub list {
    my $remote_base = decode('locale_fs', slash(shift));
    if ($remote_base eq '/') {
        $remote_base = '';
    }
    my $list = $box->list_folder($remote_base) or die $box->error;
    for my $entry (@{ $list->{entries} }) {
        print &_line($entry);
    }
}

sub _line {

script/dropbox-api  view on Meta::CPAN

    } 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) {

script/dropbox-api  view on Meta::CPAN

    };
    $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',

script/dropbox-api  view on Meta::CPAN

        }) or die $box->error;
        print $res->{url}, "\n";
    }
}

sub download {
    my ($path, $file) = @_;
    $path =~ s|^dropbox:/|/|
        or die "Usage: \n    dropbox-api download dropbox:/Public/some.txt /tmp/local.txt";
    my $fh = file($file)->openw or die $!;
    $box->download(decode('locale_fs', $path), $fh) or die $box->error;
    $fh->close;
}

sub sync {
    my ($arg1, $arg2) = @_;

    if ($dry) {
        print "!! enable dry run !!\n";
    }

    # download
    if ($arg1 =~ qr{ \A dropbox: }xms and $arg2 !~ qr{ \A dropbox: }xms) {

        my ($remote_base, $local_base) = ($arg1, $arg2);
        $remote_base = decode('locale_fs', $remote_base);
        $remote_base =~ s|^dropbox:||;

        if ($remote_base eq '/' || $remote_base eq '') {
            unless (-d $local_base) {
                die "missing $local_base";
            }
            &sync_download('/', dir(abs_path($local_base)));
        } else {
            my $content = $box->get_metadata(chomp_slash($remote_base)) or die $box->error;
            if ($content->{'.tag'} eq 'folder') {

script/dropbox-api  view on Meta::CPAN

                $local_base = -d $local_base ? dir(abs_path($local_base)) : -f $local_base ? file(abs_path($local_base)) : file($local_base);
                &sync_download_file($content, file($local_base));
            }
        }
    }

    # upload
    elsif ($arg1 !~ qr{ \A dropbox: }xms and $arg2 =~ qr{ \A dropbox: }xms) {

        my ($local_base, $remote_base) = ($arg1, $arg2);
        $remote_base = decode('locale_fs', $remote_base);
        $remote_base =~ s|^dropbox:||;

        if (-d $local_base) {
            &sync_upload($remote_base, dir(abs_path($local_base)));
        } elsif (-f $local_base) {
            &sync_upload_file($remote_base, file(abs_path($local_base)));
        } else {
            die "missing $local_base";
        }
    }

script/dropbox-api  view on Meta::CPAN

    for my $content (@{ $entries }) {
        my $remote_path = $content->{path_display};
        my $rel_path = remote_abs2rel($remote_path, $remote_base);
        unless (length $rel_path) {
            if ($content->{'.tag'} eq 'folder') {
                next;
            } else {
                $rel_path = $content->{name};
            }
        }
        my $rel_path_enc = encode('locale_fs', $rel_path);
        $remote_map->{$rel_path}++;
        printf "check: %s\n", $rel_path if $debug;
        my $is_dir = $content->{'.tag'} eq 'folder' ? 1 : 0;
        my $local_path = $is_dir ? dir($local_base, $rel_path_enc) : file($local_base, $rel_path_enc);
        if ($is_dir) {
            printf "remote: %s\n", $remote_path if $debug;
            printf "local:  %s\n", $local_path if $debug;
            if (!-d $local_path) {
                $local_path->mkpath unless $dry;
                printf "mkpath %s\n", decode('locale_fs', $local_path);
            } else {
                printf "skip %s\n", $rel_path if $verbose;
            }
        } else {

            if ((!-f $local_path) || has_change($local_path, $content)) {

                if ($dry) {
                    printf "download %s\n", decode('locale_fs', $local_path);
                    next;
                }

                # not displayed in the dry-run for the insurance
                unless (-d $local_path->dir) {
                    printf "mkpath %s\n", decode('locale_fs', $local_path->dir);
                    $local_path->dir->mkpath;
                }

                my $local_path_tmp = $local_path . '.dropbox-api.tmp';
                my $fh;
                unless (open($fh, '>', $local_path_tmp)) {
                    warn "open failure " . decode('locale_fs', $local_path) . " (" . $! . ")";
                    $exit_code = 1;
                    next;
                }
                if ($box->download($content->{path_display}, $fh)) {
                    printf "download %s\n", decode('locale_fs', $local_path);
                    close($fh);
                    my $remote_epoch = $strpz->parse_datetime($content->{client_modified})->epoch;
                    unless (utime($remote_epoch, $remote_epoch, $local_path_tmp)) {
                        warn "set modification time failure " .  decode('locale_fs', $local_path);
                        $exit_code = 1;
                    }
                    unless (rename($local_path_tmp, $local_path)) {
                        unlink($local_path_tmp);
                        warn "rename failure " . decode('locale_fs', $local_path_tmp);
                        $exit_code = 1;
                    }
                } else {
                    unlink($local_path_tmp);
                    chomp( my $error = $box->error );
                    warn "download failure " . decode('locale_fs', $local_path) . " (" . $error . ")";
                    $exit_code = 1;
                }
            } else {
                printf "skip %s\n", $rel_path if $verbose;
            }
        }
        $remote_inode_map->{ &inode($local_path) } = $content;
    }

    if ($exit_code) {

script/dropbox-api  view on Meta::CPAN

    $local_base->recurse(
        preorder => 0,
        depthfirst => 1,
        callback => sub {
            my $local_path = shift;
            if ($local_path eq $local_base) {
                return;
            }

            my $rel_path_enc = abs2rel($local_path, $local_base);
            my $rel_path = decode('locale_fs', $rel_path_enc);

            if (exists $remote_map->{$rel_path}) {
                if ($verbose) {
                    printf "skip %s\n", $rel_path;
                }
            } elsif (my $content = $remote_inode_map->{ &inode($local_path) }) {
                my $remote_path = $content->{path_display};
                my $rel_path_remote = remote_abs2rel($remote_path, $remote_base);
                if ($verbose) {
                    if ($debug) {

script/dropbox-api  view on Meta::CPAN

        print "local_base: $local_path\n";
    }

    if (-d $local_path) {
        $local_path = file($local_path, $content->{name});
    }

    if ((!-f $local_path) || has_change($local_path, $content)) {

        if ($dry) {
            printf "download %s\n", decode('locale_fs', $local_path);
            return;
        }

        unless (-d $local_path->dir) {
            printf "mkpath %s\n", decode('locale_fs', $local_path->dir);
            $local_path->dir->mkpath;
        }

        my $local_path_tmp = $local_path . '.dropbox-api.tmp';
        my $fh;
        unless (open($fh, '>', $local_path_tmp)) {
            warn "open failure " . decode('locale_fs', $local_path) . " (" . $! . ")";
            $exit_code = 1;
            return;
        }
        if ($box->download($content->{path_display}, $fh)) {
            printf "download %s\n", decode('locale_fs', $local_path);
            close($fh);
            my $remote_epoch = $strpz->parse_datetime($content->{client_modified})->epoch;
            unless (utime($remote_epoch, $remote_epoch, $local_path_tmp)) {
                warn "set modification time failure " .  decode('locale_fs', $local_path);
                $exit_code = 1;
            }
            unless (rename($local_path_tmp, $local_path)) {
                unlink($local_path_tmp);
                warn "rename failure " . decode('locale_fs', $local_path_tmp);
                $exit_code = 1;
            }
        } else {
            unlink($local_path_tmp);
            chomp( my $error = $box->error );
            warn "download failure " . decode('locale_fs', $local_path) . " (" . $error . ")";
            $exit_code = 1;
        }
    } else {
        printf "skip %s\n", $content->{path_display} if $verbose;
    }
}

sub sync_upload {
    my ($remote_base, $local_base) = @_;

script/dropbox-api  view on Meta::CPAN


    my @makedirs;
    $local_base->recurse(
        preorder => 0,
        depthfirst => 1,
        callback => sub {
            my $local_path = shift;
            if ($local_path eq $local_base) {
                return;
            }
            my $rel_path = decode('locale_fs', abs2rel($local_path, $local_base));
            my $remote_path = file($remote_base, $rel_path);
            my $content = delete $remote_map->{ lc $rel_path };

            # exists file or directory
            if ($content) {
                delete $remote_path_map->{ $content->{path_display} };

                unless (-f $local_path) {
                    return;
                }

script/dropbox-api  view on Meta::CPAN

sub has_change ($$) {
    my ($local_path, $content) = @_;

    my $remote_epoch = $strpz->parse_datetime($content->{client_modified})->epoch;
    my $local_epoch = $local_path->stat->mtime;
    my $remote_size = $content->{size};
    my $local_size = $local_path->stat->size;

    if ($debug) {
        printf "remote: %10s %10s %s\n", $remote_epoch, $remote_size, $content->{path_display};
        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 {



( run in 1.517 second using v1.01-cache-2.11-cpan-ceb78f64989 )