Commit ea372031

mo khan <mo@mokhan.ca>
2014-06-07 03:55:55
apply a sanitized file and content type to photos.
1 parent bd8f6b7
Changed files (5)
app
models
services
application
infrastructure
spec
models
services
application
app/models/photo.rb
@@ -13,7 +13,8 @@ class Photo < ActiveRecord::Base
 
   def upload(file, blob_storage)
     image = Image.new(file)
-    self.original_filename = image.filename
+    self.image = self.original_filename = image.filename
+    self.content_type = image.content_type
     versions.each do |version|
       version.adjust(image)
       blob_storage.upload(create_key(version.prefix), image.path)
app/services/application/handlers/process_photo.rb
@@ -11,10 +11,7 @@ class ProcessPhoto
 
   def handle(message)
     photo = @photos.find(message[:photo_id])
-    #photo.image = file
     photo.image_processing = nil
-    photo.content_type = message[:content_type]
-    photo.original_filename = message[:original_filename]
     photo.latitude, photo.longitude = @exif_parser.parse_geolocation_from(message[:file_path])
     photo.upload(message[:file_path], @blob_storage)
     photo.save!
app/services/infrastructure/image.rb
@@ -9,6 +9,10 @@ class Image
     @filename ||= sanitize(@path)
   end
 
+  def content_type
+    @content_type ||= ::MIME::Types.type_for(filename).first.to_s
+  end
+
   def resize_to_fit(width, height)
     manipulate! do |img|
       img.resize "#{width}x#{height}"
spec/models/photo_spec.rb
@@ -11,12 +11,23 @@ describe Photo do
       subject.id = rand(100)
       subject.upload(file, blob_storage)
       blob_storage.should have_received(:upload).with(upload_key, file)
-      blob_storage.should have_received(:upload).with(upload_key("large"), file)
-      blob_storage.should have_received(:upload).with(upload_key("thumb"), file)
+      blob_storage.should have_received(:upload).with(upload_key("large_"), file)
+      blob_storage.should have_received(:upload).with(upload_key("thumb_"), file)
+    end
+
+    it "sets the original filename" do
+      subject.upload(file, blob_storage)
+      subject.original_filename.should == "gps.jpg"
+      subject.image.should == "gps.jpg"
+    end
+
+    it "specifies the content type" do
+      subject.upload(file, blob_storage)
+      subject.content_type.should == "image/jpeg"
     end
 
     def upload_key(prefix = '')
-      "uploads/photo/image/#{subject.id}/#{prefix}_gps.jpg"
+      "uploads/photo/image/#{subject.id}/#{prefix}gps.jpg"
     end
   end
 end
spec/services/application/handlers/process_photo_spec.rb
@@ -24,7 +24,6 @@ describe ProcessPhoto do
         photo_id: photo.id,
         file_path: image_path,
         content_type: 'image/jpeg',
-        original_filename: 'blah.jpg'
       }
       subject.handle(message)
     end
@@ -42,7 +41,7 @@ describe ProcessPhoto do
     end
 
     it "specifies the original filename" do
-      photo.original_filename.should == 'blah.jpg'
+      photo.original_filename.should == 'gps.jpg'
     end
 
     it "applies the geolocation information" do