X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=Memories%2FPhoto.pm;h=939af0078b69d502ffe4491a5fa83bdd31cf682e;hb=fdb21fe7e3d32442fad6c8acf6639de2ec055e37;hp=b7696b14befef481ace2a63fb958dca39e827ef6;hpb=cb25f33bf3c79a5842ddb9461e56108e73dfbf40;p=memories.git diff --git a/Memories/Photo.pm b/Memories/Photo.pm index b7696b1..939af00 100644 --- a/Memories/Photo.pm +++ b/Memories/Photo.pm @@ -1,11 +1,13 @@ package Memories::Photo; +#use Apache2::Upload; use File::Basename; use File::Copy; use Archive::Any; -use File::Temp qw(tempdir); +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); @@ -13,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/); @@ -63,7 +65,7 @@ sub do_upload :Exported { } $r->objects(\@photos); if (@photos == 0) { $r->template("upload"); return } - if (@photos > 1) { $r->template("list") } + if (@photos > 1) { $r->template_args->{title} = "This upload"; $r->template("paged") } else { $r->template("view"); } $r->message("Thanks for the upload!"); } @@ -82,7 +84,7 @@ sub quarantine :Exported { } $r->{session}{quarantined} = join ",", sort keys %q; 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 ]); @@ -93,9 +95,10 @@ 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/i) { + 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 { @@ -118,6 +121,20 @@ sub upload_archive { 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, $r, $filename, $offered_name) = @_; my $photo = $self->create({ @@ -129,9 +146,10 @@ sub upload_jpeg { 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? @@ -145,7 +163,6 @@ sub upload_jpeg { $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}) }); @@ -194,6 +211,14 @@ sub comment :Exported {} sub tagedit :Exported {} sub similar :Exported {} sub sized :Exported {} +sub delete :Exported { + my ($self, $r, $photo) = @_; + if ($photo and $photo->uploader == $r->user) { + $photo->delete; + $r->message("Photo deleted!"); + } + $r->template("frontpage"); +} use Class::DBI::Plugin::Pager; use Class::DBI::Plugin::AbstractCount; @@ -222,12 +247,7 @@ sub add_comment :Exported { }); } -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' }); @@ -290,7 +310,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}; @@ -321,21 +341,24 @@ 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); } 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; } @@ -437,7 +460,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;