From 43fee9b2dd36d5f99e3318e91f7845bcbdae4ff7 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Sat, 24 Feb 2007 20:08:09 +0000 Subject: [PATCH] Allow multiple uploads, look into ZIP and tar.gz files. Closes #8. git-svn-id: http://svn.simon-cozens.org/memories/trunk@38 041978f6-d955-411f-a9d7-1d8545c9c3c7 --- Memories/Photo.pm | 50 +++++++++++++++++++++++++++++++++--------- templates/custom/list | 2 +- templates/photo/upload | 6 ++--- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/Memories/Photo.pm b/Memories/Photo.pm index 6d861cc..1eb0824 100644 --- a/Memories/Photo.pm +++ b/Memories/Photo.pm @@ -1,6 +1,11 @@ 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); @@ -47,18 +52,17 @@ 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 == 0) { $r->template("upload"); return } if (@photos > 1) { $r->template("list") } else { $r->template("view"); } $r->message("Thanks for the upload!"); @@ -68,10 +72,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); @@ -79,8 +81,6 @@ 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->objects([ map { $self->retrieve($_) } @quarantined ]); @@ -89,12 +89,41 @@ sub quarantine :Exported { } } +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, $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, @@ -112,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(); @@ -292,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); diff --git a/templates/custom/list b/templates/custom/list index 3dd50b3..a0aaa51 100644 --- a/templates/custom/list +++ b/templates/custom/list @@ -7,7 +7,7 @@ [% SET minilist = objects.splice(0,3) %] [% FOR object = minilist %] - [% object %]
+
[% object %]

[%object.photos.size %] diff --git a/templates/photo/upload b/templates/photo/upload index 02ef5e8..9921f49 100644 --- a/templates/photo/upload +++ b/templates/photo/upload @@ -1,9 +1,9 @@ [% INCLUDE header %]

Upload a photo

-This is where you can upload your photographs. At the moment, you must -upload them one at a time; in the near future, you will be able to -upload a Zip file containing several photos. +This is where you can upload your photographs. You can upload +photographs individually or a Zip file (or Unix tar/tar.gz) full of +pictures.

Please note that it may take a while to transfer your photograph to the -- 2.39.2