+sub upload_file {
+ my ($self, $r, $filename, $offered_name) = @_;
+ my $mm = File::MMagic->new;
+ my $res = $mm->checktype_filename($filename);
+ 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 {
+ $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_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;
+}
+