API workers using Gearman::Driver
[cloudstore.git] / gearman / transmission.pl
index 901070d..6e6e04a 100755 (executable)
@@ -6,6 +6,7 @@ use Transmission::Client;
 use JSON::XS;
 use Data::Dump qw(dump);
 use autodie;
+use File::Path qw(make_path remove_tree);
 
 sub user_info {
        my $login = shift;
@@ -23,12 +24,19 @@ my $j = JSON::XS->new;
 $j->allow_blessed(1);
 $j->convert_blessed(0);
 
-use TokyoCabinet;
+use lib 'lib';
+use CloudStore::MD5sum;
 
 my $name = $ENV{ZSLICE} || die "need ZSLICE";
 my $torrent_dir = "/$name/torrent";
 $name =~ s/\W+/_/g;
 
+=head1 host_s1_torrent_list
+
+Returns JSON encoded list of all torrents
+
+=cut
+
 $worker->register_function( $name . '_torrent_list' => sub {
        my ($job) = @_;
        my @cols = split(/\s+/,$job->arg);
@@ -44,6 +52,12 @@ $worker->register_function( $name . '_torrent_list' => sub {
        return $j->encode( \@list );
 } );
 
+=head1 host_s1_torrent_download ~login/path.torrent
+
+Start download of torrent
+
+=cut
+
 $worker->register_function( $name . '_torrent_download' => sub {
        my $work = $_[0]->arg;
        chomp $work;
@@ -54,15 +68,15 @@ $worker->register_function( $name . '_torrent_download' => sub {
 
        my $dir = (getpwnam($user))[7] || die "no user $user";
 
-       my $to_dir = "$torrent_dir/download/$user";
-       if ( ! -e $to_dir ) {
-               mkdir $to_dir;
-               # fix perm
-               my ($uid,$gid) = (stat($torrent_dir))[4,5];
-               chown $uid, $gid, $to_dir;
-       }
+       my $torrent_file = "$dir/$path";
+
+       $path =~ s/\.torrent$//;
+       my $to_dir = "$torrent_dir/download/$user/$path";
+       my ($uid,$gid) = (stat($torrent_dir))[4,5];
+       make_path $to_dir, { uid => $uid, gid => $gid };
 
-       $tc->add( filename => "$dir/$path", 'download-dir' => $to_dir );
+       my $t = $tc->add( filename => $torrent_file, 'download-dir' => $to_dir );
+       warn "# added $to_dir = ",dump($t);
 
        return $to_dir;
 } );
@@ -74,36 +88,42 @@ $worker->register_function( $name . '_torrent_download_done' => sub {
 
        my ( $id, $dir, $name, $hash ) = split(/\s+#\s+/,$work);
 
-       $tc->remove( $id );
-
-       if ( $dir =~ m{/(\w+)$} ) {
+       if ( $dir =~ m{/download/(\w+)/(.+)$} ) {
                my $user = $1;
+               my $path = $2;
 
                warn "# check $name md5sum";
                my $md5sum = `md5sum "$dir/$name"`;
-               $md5sum =~ s/\s+.*$//;
+               $md5sum =~ s/\s+.*$//s;
                if ( $name ne $md5sum ) {
-                       warn "ERROR: invalid md5sum for $name -> $md5sum\n";
-               } else {
+                       warn "ERROR: invalid md5sum filename ",dump( $name, $md5sum );
+               }
 
-                       warn "# move $name to $user\n";
-                       my ( $uid, $gid, $home_dir ) = user_info $user;
-                       link "$dir/$name", "$home_dir/$name";
-                       chown $uid, $gid,  "$home_dir/$name";
-                       warn "FIXME: add to md5 pool?";
+               warn "# move $name to $user $path\n";
+               my ( $uid, $gid, $home_dir ) = user_info $user;
+               link "$dir/$name", "$home_dir/$path";
+               chown $uid, $gid,  "$home_dir/$path";
+               warn "FIXME: add to md5 pool?";
 
-               }
+               my $t = $tc->remove( $id );
+               warn "# remove $id = ",dump($t);
+
+               warn "# $id removed $dir $name $hash\n";
 
+               remove_tree $dir;
        } else {
                warn "ERROR: can't find user in $dir for $name\n";
        }
 
-       warn "# $id removed $dir $name $hash\n";
-
-       unlink "$dir/$name";
 
 } );
 
+=head1 host_s1_torrent_share ~login/file.txt
+
+Create torrent and start seeding it
+
+=cut
+
 $worker->register_function( $name . '_torrent_share' => sub {
        my $work = $_[0]->arg;
        chomp $work;
@@ -113,11 +133,7 @@ $worker->register_function( $name . '_torrent_share' => sub {
        my $path = $work;
 
        my $dir = (getpwnam($user))[7] || die "no user $user";
-       my $md5_path = "$dir/.md5";
-       my %h;
-       tie %h, "TokyoCabinet::HDB", $md5_path || die "$md5_path: $!";
-       my $md5 = $h{$path} || die "can't find $path in $md5_path";
-       untie %h;
+       my $md5 = CloudStore::MD5sum->md5_get( "$dir/$path" );
 
        warn "# $md5 $path";