Commit adfdf683

mo khan <mo@mokhan.ca>
2014-05-31 23:44:40
extract some methods and use the original file name when processing the file.
1 parent b91ab79
Changed files (2)
app
models
services
application
app/models/photo.rb
@@ -15,6 +15,6 @@ class Photo < ActiveRecord::Base
   end
 
   def is_processed?
-    self.image_processing == nil
+    !self.image_processing
   end
 end
app/services/application/upload_photo.rb
@@ -1,22 +1,12 @@
 class UploadPhoto
-  def initialize(message_bus = Spank::IOC.resolve(:message_bus))
+  def initialize(message_bus = Spank::IOC.resolve(:message_bus), cakes = Creation)
     @message_bus = message_bus
+    @cakes = cakes
   end
 
   def run(cake_id, params)
-    photo = Creation.find(cake_id).photos.create!(image_processing: true)
-    system "mkdir -p #{Rails.root.join("tmp/uploads/")}"
-    original_path = params[:image].path
-    new_path = "#{Rails.root.join("tmp", "uploads")}/#{SecureRandom.uuid}#{File.extname(params[:image].original_filename)}"
-    FileUtils.mv(original_path, new_path)
-    message = {
-      cake_id: cake_id,
-      photo_id: photo.id,
-      file_path: new_path,
-      original_filename: params[:image].original_filename,
-      content_type: params[:image].content_type,
-    }
-    @message_bus.publish(:upload_photo, message)
+    photo = @cakes.find(cake_id).photos.create!(image_processing: true)
+    @message_bus.publish(:upload_photo, create_message_from(cake_id, params, photo))
     map_from(photo.id, params[:image].original_filename)
   end
 
@@ -31,4 +21,26 @@ class UploadPhoto
       delete_type: "DELETE"
     }
   end
+
+  def move_to_temporary_storage(temp_file_path, original_filename)
+    new_path = "#{create_tmp_dir}/#{original_filename}"
+    FileUtils.mv(temp_file_path, new_path)
+    new_path
+  end
+
+  def create_tmp_dir
+    directory = Rails.root.join("tmp/uploads/#{SecureRandom.uuid}/")
+    system "mkdir -p #{directory}"
+    directory
+  end
+
+  def create_message_from(cake_id, payload, photo)
+    {
+      cake_id: cake_id,
+      photo_id: photo.id,
+      file_path: move_to_temporary_storage(payload[:image].path, payload[:image].original_filename),
+      original_filename: payload[:image].original_filename,
+      content_type: payload[:image].content_type,
+    }
+  end
 end