]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
Fix some interesting breakages: you can't use ->session if there's no user.
[memories.git] / Memories / Photo.pm
index d14619ee03cd0440ba42d354438658a03d368d5c..6d861cc7c2cee76ce99d6517b71f9ed292387840 100644 (file)
@@ -1,4 +1,6 @@
 package Memories::Photo;
+use File::Basename;
+use File::Copy;
 use strict;
 use Carp qw(cluck confess);
 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
@@ -43,52 +45,82 @@ __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_jpeg($upload->tempname, ($r->params->{title}||basename($upload->filename)), $r->params->{tags}, $r));
+    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") } 
+    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;
+    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);
+            delete $q{$_};
+        }
+    }
+    $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->objects([ map { $self->retrieve($_) } @quarantined ]);
+    } else {
+        $r->objects([ map { $self->retrieve($_) } sort keys %q ]);
+    }
+}
+
+sub upload_jpeg {
+    my ($self, $filename, $title, $tags, $r) = @_;
     my $photo = $self->create({
         uploader => $r->user,
         uploaded => Time::Piece->new(),
-        title => $r->params->{title},
+        title => $title,
         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});
+    $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 {