]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
It's unlikely we'll get DOS people doing that but reminds me to do the
[memories.git] / Memories / Photo.pm
index 8286480aa1f952191c75216b33664978e48f3379..b7696b14befef481ace2a63fb958dca39e827ef6 100644 (file)
@@ -2,6 +2,10 @@ 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);
@@ -48,27 +52,85 @@ sub do_upload :Exported {
     my ($self, $r) = @_;
     my $upload = $r->{ar}->upload("photo");
     # Check $self->type
-    my @photos = $self->upload_jpeg($upload->tempname, ($r->params->{title}||basename($upload->filename)), $r->params->{tags}, $r);
+    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);
-    $r->template("view");
+    if (@photos == 0) { $r->template("upload"); return  }
+    if (@photos > 1) { $r->template("list") } 
+    else { $r->template("view"); }
     $r->message("Thanks for the upload!"); 
-    # Deal with the quarantined
+}
+
+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 ]);
+    }
+}
+
+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/i) {
+        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, $filename, $title, $tags, $r) = @_;
-    my $quarantine;
+    my ($self, $r, $filename, $offered_name) = @_;
     my $photo = $self->create({
         uploader => $r->user,
         uploaded => Time::Piece->new(),
-        title => $title,
+        title => ($r->{params}{title} || basename($offered_name)),
         hit_count => 0,
         rating => 0,
         rated => 0,
     });
-    copy($filename, $photo->path("file"));
+    if (!copy($filename, $photo->path("file"))) {
+        $photo->delete(); die "Couldn't copy photo: $!";
+    }
     my ($x, $y) = dim(image_info($photo->path));
     $photo->x($x); $photo->y($y);
 
@@ -79,7 +141,7 @@ sub upload_jpeg {
     }
 
     $photo->make_thumb;
-    $tags ||= join " ", map { qq{"$_"} } $photo->tags_exif;
+    my $tags = $r->{params}{tags}.join " ", map { qq{"$_"} } $photo->tags_exif;
     $photo->add_tags($tags);
     $photo->add_to_imageseek_library;
     Memories->zap_cache();
@@ -259,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);
@@ -309,22 +372,28 @@ sub edit_tags :Exported {
     my ($self, $r) = @_;
     my $photo = $r->objects->[0];
     my %params = %{$r->params};
+    my $exifTool = new Image::ExifTool;
     for (keys %params) { 
         next unless /delete_(\d+)/;
         my $tagging = Memories::Tagging->retrieve($1) or next;
         next unless $tagging->photo->id == $photo->id;
+        $exifTool->SetNewValue(Keywords => $1, DelValue => 1);
         $tagging->delete;
     }
+    $exifTool->WriteInfo($photo->path);
     $photo->add_tags($params{newtags});
     $r->template("view");
 }
 
 sub add_tags {
     my ($photo, $tagstring) = @_;
+    my $exifTool = new Image::ExifTool;
 
     for my $tag (Tagtools->separate_tags($tagstring)) {
-        $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) })
+        $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) });
+        $exifTool->SetNewValue(Keywords => $tag, AddValue => 1);
     }
+    $exifTool->WriteInfo($photo->path);
 }
 
 # Work out some common properties from a set of potential photo metadata