]> 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 960810d59ab8fcf6414489c4fa35b430492d2496..6d861cc7c2cee76ce99d6517b71f9ed292387840 100644 (file)
@@ -1,20 +1,41 @@
 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);
+use constant INTERESTINGNESS_ALGORITHM => '((rating+3)/(rated+1))*(1+rated/hit_count)*(1+hit_count/(select max(hit_count) from photo))';
 use Time::Piece;
 use Image::Seek;
 use constant PAGER_SYNTAX => "LimitXY";
-__PACKAGE__->columns(Essential => qw(id title uploader uploaded x y));
+__PACKAGE__->columns(Essential => qw(id title uploader uploaded x y rating rated hit_count));
 __PACKAGE__->untaint_columns(printable => [qw/title/]);
 __PACKAGE__->columns(TEMP => qw/exif_object/);
-__PACKAGE__->set_sql(recent => q{
-SELECT __ESSENTIAL__
-FROM __TABLE__
-ORDER BY uploaded DESC
-LIMIT 4
-});
 
+BEGIN {
+my %order_by = (
+     recent => "uploaded",
+     popular => "hit_count",
+     loved => "rating/(1+rated)",
+     interesting => INTERESTINGNESS_ALGORITHM,
+     random => "rand()"
+);
+
+while (my($label, $how) = each %order_by) {
+    __PACKAGE__->set_sql($label => qq{
+    SELECT __ESSENTIAL__
+    FROM __TABLE__
+    ORDER BY $how DESC
+    LIMIT 4
+    });
+    no strict;
+    *$label = sub {
+        my ($self, $r) = @_;
+        $self->view_paged_ordered($r, "$how desc");
+    };
+    __PACKAGE__->MODIFY_CODE_ATTRIBUTES(*{$label}{CODE}, "Exported"); # Hack
+}
+}
 __PACKAGE__->has_many(comments => "Memories::Comment");
 __PACKAGE__->has_a(uploader => "Memories::User");
 __PACKAGE__->has_a(uploaded => "Time::Piece",
@@ -24,55 +45,106 @@ __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!"); 
+}
 
-    # XXX Stop anonymous uploads!
+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,
     });
-
-    # 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;
-    $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}) });
+    return $photo;
+}
 
-    # 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>"
-        )
-    ); 
+sub approx_rating {
+    my $self = shift;
+    $self->rated or return 0;
+    int($self->rating/$self->rated*10)/10;
+}
+
+sub add_rating :Exported {
+    my ($self, $r) = @_;
+    my $photo = $r->{objects}[0];
+    my $delta = $r->{params}{rating};
+    if ($delta < 0 or $delta > 5) { return; } # Scammer
+    # XXX Race
+    $photo->rating($photo->rating() + $delta);
+    $photo->rated($photo->rated() + 1);
+    $r->output(""); # Only used by ajax
 }
 
 sub view :Exported {
     my ($self, $r) = @_;
+    my $photo = $r->{objects}[0];
+    $photo->hit_count($photo->hit_count()+1);
     if ($r->{session}{last_search}) {
-        my $photo = $r->{objects}[0];
         # This is slightly inefficient
         my @search = split/,/, $r->{session}{last_search};
         my $found = -1;
@@ -88,22 +160,28 @@ sub view :Exported {
     }
 }
 sub upload :Exported {}
+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;
 
-sub recent :Exported {
-    my ($self, $r) = @_;
+sub view_paged_ordered {
+    my ($self, $r, $how) = @_;
     my $page = $r->params->{page} || 1;
     my $pager = $self->pager(
         per_page => Memories->config->{photos_per_page}, 
         page => $page,
         syntax => PAGER_SYNTAX,
-        order_by => "uploaded desc"
+        order_by => $how
     );
     $r->objects([$pager->retrieve_all ]);
     $r->{template_args}{pager} = $pager;
     $r->last_search;
+    $r->template("paged"); # Set the what using the action name
 }
 
 sub add_comment :Exported {
@@ -260,7 +338,6 @@ sub scale {
     }
 }
 
-use Text::Balanced qw(extract_multiple extract_quotelike);
 sub edit_tags :Exported {
     my ($self, $r) = @_;
     my $photo = $r->objects->[0];
@@ -278,7 +355,7 @@ sub edit_tags :Exported {
 sub add_tags {
     my ($photo, $tagstring) = @_;
 
-    for my $tag (map { s/^"|"$//g; $_} extract_multiple(lc $tagstring, [ \&extract_quotelike, qr/([^\s]+)/ ], undef,1)) {
+    for my $tag (Tagtools->separate_tags($tagstring)) {
         $photo->add_to_tags({tag => Memories::Tag->find_or_create({name =>$tag}) })
     }
 }
@@ -314,4 +391,19 @@ sub description {
 sub title_exif { shift->_grovel_metadata( 'Headline', 'Title'); }
 sub license { shift->_grovel_metadata( 'Rights Usage Terms', 'Usage Terms' ) }
 sub copyright { shift->_grovel_metadata( 'Rights', 'Copyright', 'Copyright Notice') }
+
+# This one's slightly different since we want everything we can get...
+sub tags_exif {
+    my $self = shift;
+    my %md = map {%$_} values %{$self->exif_info};
+    my %tags = 
+        map { s/\s+/-/g; lc $_ => 1  }
+        map { split /\s*,\s*/, $md{$_}}
+        grep {$md{$_} and $md{$_} =~/[^ 0:]/}
+        (qw(Keywords Subject City State Location Country Province-State), 
+        'Transmission Reference', 'Intellectual Genre', 
+        'Country-Primary Location Name'
+        );
+    return keys %tags;
+}
 1;