Changeset 38

Show
Ignore:
Timestamp:
02/24/07 20:08:09 (2 years ago)
Author:
simon
Message:

Allow multiple uploads, look into ZIP and tar.gz files. Closes #8.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Memories/Photo.pm

    r37 r38  
    22use File::Basename; 
    33use File::Copy; 
     4use Archive::Any; 
     5use File::Temp qw(tempdir); 
     6use File::Path qw(rmtree); 
     7use File::Find; 
     8use File::MMagic; 
    49use strict; 
    510use Carp qw(cluck confess); 
     
    4853    my $upload = $r->{ar}->upload("photo"); 
    4954    # Check $self->type 
    50     my @photos = ($self->upload_jpeg($upload->tempname, ($r->params->{title}||basename($upload->filename)), $r->params->{tags}, $r)); 
     55    my @photos = ($self->upload_file($r, $upload->tempname, $upload->filename)); 
    5156    my @quarantined = grep { !$_->tags } @photos; 
    52     warn "Quarantined these photos: ".join(",", map {$_->id} @quarantined); 
    5357    # Set it up to go again 
    5458    if (@quarantined) {  
    5559        $r->{session}{quarantined} = join ",", sort map { $_->id} @quarantined; 
    56         warn "Setting quarantineined to: ".( join ",", sort map { $_->id} @quarantined); 
    5760        $r->objects(\@quarantined); 
    5861        $r->template("quarantine"); 
     
    6063    } 
    6164    $r->objects(\@photos); 
     65    if (@photos == 0) { $r->template("upload"); return  } 
    6266    if (@photos > 1) { $r->template("list") }  
    6367    else { $r->template("view"); } 
     
    6973    my @quarantined = split /,/, $r->{session}{quarantined}; 
    7074    my %q = map { $_ => 1 } @quarantined; 
    71     warn "Before we had these quarantined: @{[ keys %q ]}"; 
    7275    for (map /(\d+)/,grep /tags\d+/, keys %{$r->{params}}) { 
    7376        my $tags = $r->{params}{"tags$_"}; 
    74         warn "Got tags for $_: <$tags>"; 
    7577        next unless $tags; 
    7678        if (my $photo = $self->retrieve($_)) { 
     
    8082    } 
    8183    $r->{session}{quarantined} = join ",", sort keys %q; 
    82     warn "After, we have these quarantined: @{[ keys %q ]}"; 
    83     warn "And we set session to  $r->{session}{quarantined}"; 
    8484    if (!$r->{session}{quarantined}) { 
    8585        $r->template("list"); 
     
    9090} 
    9191 
     92sub upload_file { 
     93    my ($self, $r, $filename, $offered_name) = @_; 
     94    my $mm = File::MMagic->new; 
     95    my $res = $mm->checktype_filename($filename); 
     96    warn "$filename is a $res\n"; 
     97    if ($res =~ m{/x-zip} or $offered_name =~ /t(ar\.)?gz/) { 
     98        return $self->upload_archive($r, $filename); 
     99    } elsif ($res =~ m{image/jpeg}) { 
     100        return $self->upload_jpeg($r, $filename, $offered_name); 
     101    } else { 
     102        $r->message(basename($offered_name).": I can't handle $res files yet"); 
     103        return (); 
     104    } 
     105} 
     106 
     107sub upload_archive { 
     108    my ($self, $r, $filename, $tags) = @_; 
     109    $r->{params}{title} = ""; # Kill that dead. 
     110    my $archive = Archive::Any->new($filename); 
     111    my $dir = tempdir(); 
     112    $archive->extract($dir); 
     113    my @results;  
     114    find({ wanted   => sub { return unless -f $_;  
     115                             push @results, $self->upload_file($r, $_, $_) },  
     116           no_chdir => 1}, $dir); 
     117    rmtree($dir); 
     118    return @results; 
     119} 
     120 
    92121sub upload_jpeg { 
    93     my ($self, $filename, $title, $tags, $r) = @_; 
     122    my ($self, $r, $filename, $offered_name) = @_; 
    94123    my $photo = $self->create({ 
    95124        uploader => $r->user, 
    96125        uploaded => Time::Piece->new(), 
    97         title => $title
     126        title => ($r->{params}{title} || basename($offered_name))
    98127        hit_count => 0, 
    99128        rating => 0, 
     
    113142 
    114143    $photo->make_thumb; 
    115     $tags ||= join " ", map { qq{"$_"} } $photo->tags_exif; 
     144    my $tags = $r->{params}{tags}.join " ", map { qq{"$_"} } $photo->tags_exif; 
    116145    $photo->add_tags($tags); 
    117146    $photo->add_to_imageseek_library; 
     
    293322    if (!$resized) { cluck "Asked for crazy size $size"; return; } 
    294323    if ($resized eq "full") { return $self->path("url") } 
     324    warn "Looking for path ".$self->path(file => $resized); 
    295325    $self->scale($resized)  
    296326        unless -e $self->path( file => $resized ); 
  • trunk/templates/custom/list

    r2 r38  
    88    [% FOR object = minilist %] 
    99        <td> 
    10         <a href="[%base%]/object/view/[%object.id%]">[% object %]<br/> 
     10        <a href="[%base%]/[%object.moniker%]/view/[%object.id%]">[% object %]<br/> 
    1111        <img src="[% object.photos.last.thumb_url |uri%]"></a><br/> 
    1212        <span class="info"> 
  • trunk/templates/photo/upload

    r35 r38  
    22<h1> Upload a photo </h1> 
    33<p> 
    4 This is where you can upload your photographs. At the moment, you must 
    5 upload them one at a time; in the near future, you will be able to 
    6 upload a Zip file containing several photos. 
     4This is where you can upload your photographs. You can upload 
     5photographs individually or a Zip file (or Unix tar/tar.gz) full of 
     6pictures. 
    77</p> 
    88<p>