]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
Allow multiple uploads, look into ZIP and tar.gz files. Closes #8.
[memories.git] / Memories / Photo.pm
index bcfb59c39c8f7db2b3811c6e53fa1eff38f23b1f..1eb08244ea35afc1496cf795f4a66e8520633164 100644 (file)
@@ -1,26 +1,46 @@
 package Memories::Photo;
+use File::Basename;
+use File::Copy;
+use Archive::Any;
+use File::Temp qw(tempdir);
+use File::Path qw(rmtree);
+use File::Find;
+use File::MMagic;
 use strict;
 use Carp qw(cluck confess);
 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
+use constant INTERESTINGNESS_ALGORITHM => '((rating+3)/(rated+1))*(1+rated/hit_count)*(1+hit_count/(select max(hit_count) from photo))';
 use Time::Piece;
 use Image::Seek;
 use constant PAGER_SYNTAX => "LimitXY";
 __PACKAGE__->columns(Essential => qw(id title uploader uploaded x y rating rated hit_count));
 __PACKAGE__->untaint_columns(printable => [qw/title/]);
 __PACKAGE__->columns(TEMP => qw/exif_object/);
-__PACKAGE__->set_sql(recent => q{
-SELECT __ESSENTIAL__
-FROM __TABLE__
-ORDER BY uploaded DESC
-LIMIT 4
-});
-__PACKAGE__->set_sql(popular => q{
-SELECT __ESSENTIAL__
-FROM __TABLE__
-ORDER BY hit_count DESC
-LIMIT 4
-});
 
+BEGIN {
+my %order_by = (
+     recent => "uploaded",
+     popular => "hit_count",
+     loved => "rating/(1+rated)",
+     interesting => INTERESTINGNESS_ALGORITHM,
+     random => "rand()"
+);
+
+while (my($label, $how) = each %order_by) {
+    __PACKAGE__->set_sql($label => qq{
+    SELECT __ESSENTIAL__
+    FROM __TABLE__
+    ORDER BY $how DESC
+    LIMIT 4
+    });
+    no strict;
+    *$label = sub {
+        my ($self, $r) = @_;
+        $self->view_paged_ordered($r, "$how desc");
+    };
+    __PACKAGE__->MODIFY_CODE_ATTRIBUTES(*{$label}{CODE}, "Exported"); # Hack
+}
+}
 __PACKAGE__->has_many(comments => "Memories::Comment");
 __PACKAGE__->has_a(uploader => "Memories::User");
 __PACKAGE__->has_a(uploaded => "Time::Piece",
@@ -30,53 +50,106 @@ __PACKAGE__->has_a(uploaded => "Time::Piece",
 
 sub do_upload :Exported {
     my ($self, $r) = @_;
-    my %upload = $r->upload("photo");
+    my $upload = $r->{ar}->upload("photo");
+    # Check $self->type
+    my @photos = ($self->upload_file($r, $upload->tempname, $upload->filename));
+    my @quarantined = grep { !$_->tags } @photos;
+    # Set it up to go again
+    if (@quarantined) { 
+        $r->{session}{quarantined} = join ",", sort map { $_->id} @quarantined;
+        $r->objects(\@quarantined);
+        $r->template("quarantine");
+        return;
+    }
+    $r->objects(\@photos);
+    if (@photos == 0) { $r->template("upload"); return  }
+    if (@photos > 1) { $r->template("list") } 
+    else { $r->template("view"); }
+    $r->message("Thanks for the upload!"); 
+}
+
+sub quarantine :Exported {
+    my ($self, $r) = @_;
+    my @quarantined = split /,/, $r->{session}{quarantined};
+    my %q = map { $_ => 1 } @quarantined;
+    for (map /(\d+)/,grep /tags\d+/, keys %{$r->{params}}) {
+        my $tags = $r->{params}{"tags$_"};
+        next unless $tags;
+        if (my $photo = $self->retrieve($_)) {
+            $photo->add_tags($tags);
+            delete $q{$_};
+        }
+    }
+    $r->{session}{quarantined} = join ",", sort keys %q;
+    if (!$r->{session}{quarantined}) {
+        $r->template("list");
+        $r->objects([ map { $self->retrieve($_) } @quarantined ]);
+    } else {
+        $r->objects([ map { $self->retrieve($_) } sort keys %q ]);
+    }
+}
 
-    # XXX Stop anonymous uploads!
+sub upload_file {
+    my ($self, $r, $filename, $offered_name) = @_;
+    my $mm = File::MMagic->new;
+    my $res = $mm->checktype_filename($filename);
+    warn "$filename is a $res\n";
+    if ($res =~ m{/x-zip} or $offered_name =~ /t(ar\.)?gz/) {
+        return $self->upload_archive($r, $filename);
+    } elsif ($res =~ m{image/jpeg}) {
+        return $self->upload_jpeg($r, $filename, $offered_name);
+    } else {
+        $r->message(basename($offered_name).": I can't handle $res files yet");
+        return ();
+    }
+}
+
+sub upload_archive {
+    my ($self, $r, $filename, $tags) = @_;
+    $r->{params}{title} = ""; # Kill that dead.
+    my $archive = Archive::Any->new($filename);
+    my $dir = tempdir();
+    $archive->extract($dir);
+    my @results; 
+    find({ wanted   => sub { return unless -f $_; 
+                             push @results, $self->upload_file($r, $_, $_) }, 
+           no_chdir => 1}, $dir);
+    rmtree($dir);
+    return @results;
+}
+
+sub upload_jpeg {
+    my ($self, $r, $filename, $offered_name) = @_;
     my $photo = $self->create({
         uploader => $r->user,
         uploaded => Time::Piece->new(),
-        title => $r->params->{title},
+        title => ($r->{params}{title} || basename($offered_name)),
         hit_count => 0,
         rating => 0,
-        rated => 0, # Oh, the potential for divide by zero errors...
+        rated => 0,
     });
-
-    # Dump content
-    if (!open OUT, ">". $photo->path("file")) {
-        die "Can't write ".$photo->path("file")." because $!";
+    if (!copy($filename, $photo->path("file"))) {
+        $photo->delete(); die "Couldn't copy photo: $!";
     }
-    # XXX Check it's a JPEG, etc.
-    # XXX Unzip ZIP file
-    print OUT $upload{content};
-    close OUT;
     my ($x, $y) = dim(image_info($photo->path));
     $photo->x($x); $photo->y($y);
 
     # Rotate?
     $photo->unrotate(); 
     if (!$photo->title){ 
-        $photo->title($photo->title_exif || $upload{filename});
+        $photo->title($photo->title_exif || basename($filename));
     }
 
     $photo->make_thumb;
-    $r->{params}{tags} ||= join " ", map { qq{"$_"} } $photo->tags_exif;
-    $photo->add_tags($r->{params}{tags});
+    my $tags = $r->{params}{tags}.join " ", map { qq{"$_"} } $photo->tags_exif;
+    $photo->add_tags($tags);
     $photo->add_to_imageseek_library;
     Memories->zap_cache();
 
     # Add system tags here
     my $tag = "date:".$photo->shot->ymd;
     $photo->add_to_system_tags({tag => Memories::SystemTag->find_or_create({name =>$tag}) });
-
-    # Set it up to go again
-    $r->objects([$photo]);
-    $r->template("view");
-    $r->message("Thanks for the upload! ".
-        ($r->{params}{tags} ? "" 
-        : "Don't forget to <a href=\"?".$r->config->uri_base."photo/view/".$photo->id."?active=tagedit\">tag your photo</a>"
-        )
-    ); 
+    return $photo;
 }
 
 sub approx_rating {
@@ -120,6 +193,7 @@ sub exif :Exported {}
 sub comment :Exported {}
 sub tagedit :Exported {}
 sub similar :Exported {}
+sub sized :Exported {}
 
 use Class::DBI::Plugin::Pager;
 use Class::DBI::Plugin::AbstractCount;
@@ -139,16 +213,6 @@ sub view_paged_ordered {
     $r->template("paged"); # Set the what using the action name
 }
 
-sub recent :Exported {
-    my ($self, $r) = @_;
-    $self->view_paged_ordered($r, "uploaded desc");
-}
-
-sub popular :Exported {
-    my ($self, $r) = @_;
-    $self->view_paged_ordered($r, "hit_count desc");
-}
-
 sub add_comment :Exported {
     my ($self, $r, $photo) = @_;
     $r->template("view");
@@ -257,6 +321,7 @@ sub sized_url { # Use this rather than ->path from TT
     my $resized = Memories->config->{sizes}->[$size];
     if (!$resized) { cluck "Asked for crazy size $size"; return; }
     if ($resized eq "full") { return $self->path("url") }
+    warn "Looking for path ".$self->path(file => $resized);
     $self->scale($resized) 
         unless -e $self->path( file => $resized );
     return $self->path(url => $resized);