]> git.decadent.org.uk Git - memories.git/blobdiff - Memories/Photo.pm
A bit of defense.
[memories.git] / Memories / Photo.pm
index 57a53c46d93b9c68471195f0ac070e3206444fa3..e2175c5ca80737387fae96525b7f4e559369a8b5 100644 (file)
@@ -1,4 +1,7 @@
 package Memories::Photo;
+use File::Basename;
+use File::Copy;
+use Archive::Any;
 use strict;
 use Carp qw(cluck confess);
 use base qw(Memories::DBI Maypole::Model::CDBI::Plain);
@@ -43,52 +46,50 @@ __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;
+    # Set it up to go again
+    $r->objects(\@photos);
+    $r->template("view");
+    $r->message("Thanks for the upload!"); 
+    # Deal with the quarantined
+}
 
+sub upload_jpeg {
+    my ($self, $filename, $title, $tags, $r) = @_;
+    my $quarantine;
     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 {
@@ -132,6 +133,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;