]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
Update to policy version 3.8.0
[memories.git] / Memories / Photo.pm
index b48c69abd4b195b5bb349557cb7ccf527ae26ed1..76375a576dd9bf26774836e36f37f8315da42404 100644 (file)
@@ -1,7 +1,13 @@
 package Memories::Photo;
+#use Apache2::Upload;
 use File::Basename;
 use File::Copy;
 use Archive::Any;
+use File::Temp qw(tempdir tmpnam);
+use File::Path qw(rmtree);
+use File::Find;
+use File::MMagic;
+use Image::Size qw(imgsize);
 use strict;
 use Carp qw(cluck confess);
 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
@@ -9,7 +15,7 @@ use constant INTERESTINGNESS_ALGORITHM => '((rating+3)/(rated+1))*(1+rated/hit_c
 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__->columns(Essential => qw(id title uploader uploaded x y rating rated hit_count format));
 __PACKAGE__->untaint_columns(printable => [qw/title/]);
 __PACKAGE__->columns(TEMP => qw/exif_object/);
 
@@ -48,19 +54,18 @@ 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;
-    warn "Quarantined these photos: ".join(",", map {$_->id} @quarantined);
     # Set it up to go again
     if (@quarantined) { 
         $r->{session}{quarantined} = join ",", sort map { $_->id} @quarantined;
-        warn "Setting quarantineined to: ".( join ",", sort map { $_->id} @quarantined);
         $r->objects(\@quarantined);
         $r->template("quarantine");
         return;
     }
     $r->objects(\@photos);
-    if (@photos > 1) { $r->template("list") } 
+    if (@photos == 0) { $r->template("upload"); return  }
+    if (@photos > 1) { $r->template_args->{title} = "This upload"; $r->template("paged") } 
     else { $r->template("view"); }
     $r->message("Thanks for the upload!"); 
 }
@@ -69,10 +74,8 @@ sub quarantine :Exported {
     my ($self, $r) = @_;
     my @quarantined = split /,/, $r->{session}{quarantined};
     my %q = map { $_ => 1 } @quarantined;
-    warn "Before we had these quarantined: @{[ keys %q ]}";
     for (map /(\d+)/,grep /tags\d+/, keys %{$r->{params}}) {
         my $tags = $r->{params}{"tags$_"};
-        warn "Got tags for $_: <$tags>";
         next unless $tags;
         if (my $photo = $self->retrieve($_)) {
             $photo->add_tags($tags);
@@ -80,30 +83,73 @@ sub quarantine :Exported {
         }
     }
     $r->{session}{quarantined} = join ",", sort keys %q;
-    warn "After, we have these quarantined: @{[ keys %q ]}";
-    warn "And we set session to  $r->{session}{quarantined}";
     if (!$r->{session}{quarantined}) {
-        $r->template("list");
+        $r->template_args->{title} = "This upload"; $r->template("paged");
         $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);
+    if ($res =~ m{/x-zip} or $offered_name =~ /t(ar\.)?gz$/i) {
+        return $self->upload_archive($r, $filename);
+    } elsif ($offered_name =~ /\.(raw|nef|dng|cr2)/i) {
+        return $self->upload_raw($r, $filename, $offered_name);
+    } 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_raw {
+    my ($self, $r, $filename, $offered_name) = @_;
+    my $jpg = tmpnam().".jpg";
+    system("dcraw -c $filename | convert - $jpg");
+    $filename =~ /\.(.*)$/;
+    my $format = $1;
+    # Put the file in place
+    my $photo = $self->upload_jpeg($r, $jpg, $offered_name);
+    $photo->format($format);
+    copy($filename, 
+         Memories->config->{data_store}."/".$photo->id.".".$format);
+    return $photo;
+}
+
 sub upload_jpeg {
-    my ($self, $filename, $title, $tags, $r) = @_;
+    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,
     });
     if (!copy($filename, $photo->path("file"))) {
-        $photo->delete(); die "Couldn't copy photo: $!";
+       warn "Couldn't copy photo to ".$photo->path("file").": $!";
+        $photo->delete(); die;
     }
-    my ($x, $y) = dim(image_info($photo->path));
+    my ($x, $y, undef) = imgsize($photo->path);
     $photo->x($x); $photo->y($y);
 
     # Rotate?
@@ -113,14 +159,13 @@ 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();
-
     # Add system tags here
     my $tag = "date:".$photo->shot->ymd;
-    $photo->add_to_system_tags({tag => Memories::SystemTag->find_or_create({name =>$tag}) });
+    $photo->add_to_system_tags({system_tag => Memories::SystemTag->find_or_create({name =>$tag}) });
     return $photo;
 }
 
@@ -134,7 +179,7 @@ sub add_rating :Exported {
     my ($self, $r) = @_;
     my $photo = $r->{objects}[0];
     my $delta = $r->{params}{rating};
-    if ($delta < 0 or $delta > 5) { return; } # Scammer
+    if ($delta <= 0 or $delta > 5) { return; } # Scammer
     # XXX Race
     $photo->rating($photo->rating() + $delta);
     $photo->rated($photo->rated() + 1);
@@ -166,6 +211,16 @@ sub comment :Exported {}
 sub tagedit :Exported {}
 sub similar :Exported {}
 sub sized :Exported {}
+sub delete :Exported {
+    my ($self, $r, $photo) = @_;
+    if ($r) { 
+    if ($photo and $photo->uploader == $r->user) {
+        $photo->delete;
+        $r->message("Photo deleted!");
+    }
+    $r->template("frontpage");
+    } else { $self->SUPER::delete() } 
+}
 
 use Class::DBI::Plugin::Pager;
 use Class::DBI::Plugin::AbstractCount;
@@ -188,18 +243,15 @@ sub view_paged_ordered {
 sub add_comment :Exported {
     my ($self, $r, $photo) = @_;
     $r->template("view");
+    if ($r->params->{content} =~ /\S/) {
     $r->objects->[0]->add_to_comments({
         name => $r->params->{name},
         content => $r->params->{content}
     });
+    }
 }
 
-sub format { 
-    "jpg" # For now
-} 
-
 use Cache::MemoryCache;
-use Image::Info qw(dim image_info);
 use Image::ExifTool;
 my $cache = new Cache::MemoryCache( { 'namespace' => 'MemoriesInfo' });
 
@@ -262,7 +314,7 @@ my %banned_tags = map { $_ => 1 }
 sub _exif_info {
     my $exifTool = new Image::ExifTool;
     $exifTool->Options(Group0 => ['IPTC', 'EXIF', 'XMP', 'MakerNotes', 'Composite']);
-    my $info = $exifTool->ImageInfo(shift->path);
+    my $info = $exifTool->ImageInfo(shift->path(0,0,1));
     my $hash = {};
     foreach my $tag ($exifTool->GetFoundTags('Group0')) {
         next if $banned_tags{$tag};
@@ -299,14 +351,18 @@ sub sized_url { # Use this rather than ->path from TT
 }
 
 sub path { 
-    my ($self, $is_url, $scale) = @_;
+    my ($self, $is_url, $scale, $raw) = @_;
     my $path =
         Memories->config->{$is_url eq "url" ? "data_store_external" : "data_store" };
     if ($scale) { $path .= "$scale/" }
     # Make dir if it doesn't exist, save trouble later
     use File::Path;
-    if ($is_url ne "url") {mkpath($path);}
-    $path .= $self->id.".".$self->format;
+    if ($is_url ne "url" and ! -d $path) {mkpath($path) or die "Couldn't make path $path: $!";}
+    if ($scale or ($is_url ne "url" and !$raw)) { 
+        $path .= $self->id.".jpg";
+    } else {
+        $path .= $self->id.".".($self->format||"jpg");
+    }
     return $path;
 }
 
@@ -343,22 +399,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
@@ -402,7 +464,7 @@ sub tags_exif {
         map { split /\s*,\s*/, $md{$_}}
         grep {$md{$_} and $md{$_} =~/[^ 0:]/}
         (qw(Keywords Subject City State Location Country Province-State), 
-        'Transmission Reference', 'Intellectual Genre', 
+        'Intellectual Genre', 
         'Country-Primary Location Name'
         );
     return keys %tags;