Commit 2926a1e1

mo khan <mo@mokhan.ca>
2014-06-05 04:53:18
move photo version processing logic into photo class.
1 parent 210a401
Changed files (3)
app
models
services
application
infrastructure
app/models/photo.rb
@@ -1,10 +1,10 @@
 class Photo < ActiveRecord::Base
   belongs_to :creation, counter_cache: true, touch: true
-  mount_uploader :image, PhotoUploader
+  #mount_uploader :image, PhotoUploader
 
-  def thumb_url
-    image.thumb.url
-  end
+  #def thumb_url
+    #image.thumb.url
+  #end
 
   def watermark
     return '' if creation.nil?
@@ -14,4 +14,31 @@ class Photo < ActiveRecord::Base
   def is_processed?
     !self.image_processing
   end
+
+  def upload(file, blob_storage)
+    image = Image.new(file)
+    upload_original(image)
+  end
+
+  private
+
+  def upload_original(image, blob_storage)
+    blob_storage.upload(create_key, image.path)
+    upload_large_version(image, blob_storage)
+  end
+
+  def upload_large_version(image, blob_storage)
+    image.resize_to_fit(570, 630)
+    blob_storage.upload(create_key('large'), image.path)
+    upload_thumbnail_version(image, blob_storage)
+  end
+
+  def upload_thumbnail_version(image, blob_storage)
+    image.resize_to_fill(260, 180)
+    blob_storage.upload(create_key('thumb'), image.path)
+  end
+
+  def create_key(prefix = '')
+    "uploads/photo/image/#{id}/#{prefix}_#{original_filename}"
+  end
 end
app/services/application/handlers/process_photo.rb
@@ -17,88 +17,7 @@ class ProcessPhoto
     photo.content_type = message[:content_type]
     photo.original_filename = message[:original_filename]
     photo.latitude, photo.longitude = @exif_parser.parse_geolocation_from(file)
-    upload_original(message[:file_path], photo)
+    photo.upload(message[:file_path], @blob_storage)
     photo.save!
   end
-
-  private
-
-  def upload_original(file, photo)
-    key = "uploads/photo/image/#{photo.id}/#{File.basename(file)}"
-    @blob_storage.upload(key, file)
-    upload_large_version(file, photo)
-  end
-
-  def upload_large_version(file, photo)
-    key = "uploads/photo/image/#{photo.id}/large_#{File.basename(file)}"
-    Image.new(file).resize_to_fit(570, 630)
-    @blob_storage.upload(key, file)
-    upload_thumbnail_version(file, photo)
-  end
-
-  def upload_thumbnail_version(file, photo)
-    key = "uploads/photo/image/#{photo.id}/thumb_#{File.basename(file)}"
-    Image.new(file).resize_to_fill(260, 180)
-    @blob_storage.upload(key, file)
-  end
-
-end
-
-class Image
-  attr_reader :current_path
-
-  def initialize(current_path)
-    @current_path = current_path
-  end
-
-  def resize_to_fit(width, height)
-    manipulate! do |img|
-      img.resize "#{width}x#{height}"
-      img = yield(img) if block_given?
-      img
-    end
-  end
-
-  def resize_to_fill(width, height, gravity = 'Center')
-    manipulate! do |img|
-      cols, rows = img[:dimensions]
-      img.combine_options do |cmd|
-        if width != cols || height != rows
-          scale_x = width/cols.to_f
-          scale_y = height/rows.to_f
-          if scale_x >= scale_y
-            cols = (scale_x * (cols + 0.5)).round
-            rows = (scale_x * (rows + 0.5)).round
-            cmd.resize "#{cols}"
-          else
-            cols = (scale_y * (cols + 0.5)).round
-            rows = (scale_y * (rows + 0.5)).round
-            cmd.resize "x#{rows}"
-          end
-        end
-        cmd.gravity gravity
-        cmd.background "rgba(255,255,255,0.0)"
-        cmd.extent "#{width}x#{height}" if cols != width || rows != height
-      end
-      img = yield(img) if block_given?
-      img
-    end
-  end
-
-  private
-
-  def manipulate!
-    image = ::MiniMagick::Image.open(current_path)
-    begin
-      image = yield(image)
-      image.write(current_path)
-      image.run_command("identify", current_path)
-    ensure
-      image.destroy!
-    end
-  rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
-    default = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :locale => :en)
-    message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :default => default)
-    raise CarrierWave::ProcessingError, message
-  end
 end
app/services/infrastructure/image.rb
@@ -0,0 +1,58 @@
+class Image
+  attr_reader :path
+
+  def initialize(path)
+    @path = path
+  end
+
+  def resize_to_fit(width, height)
+    manipulate! do |img|
+      img.resize "#{width}x#{height}"
+      img = yield(img) if block_given?
+      img
+    end
+  end
+
+  def resize_to_fill(width, height, gravity = 'Center')
+    manipulate! do |img|
+      cols, rows = img[:dimensions]
+      img.combine_options do |cmd|
+        if width != cols || height != rows
+          scale_x = width/cols.to_f
+          scale_y = height/rows.to_f
+          if scale_x >= scale_y
+            cols = (scale_x * (cols + 0.5)).round
+            rows = (scale_x * (rows + 0.5)).round
+            cmd.resize "#{cols}"
+          else
+            cols = (scale_y * (cols + 0.5)).round
+            rows = (scale_y * (rows + 0.5)).round
+            cmd.resize "x#{rows}"
+          end
+        end
+        cmd.gravity gravity
+        cmd.background "rgba(255,255,255,0.0)"
+        cmd.extent "#{width}x#{height}" if cols != width || rows != height
+      end
+      img = yield(img) if block_given?
+      img
+    end
+  end
+
+  private
+
+  def manipulate!
+    image = ::MiniMagick::Image.open(path)
+    begin
+      image = yield(image)
+      image.write(path)
+      image.run_command("identify", path)
+    ensure
+      image.destroy!
+    end
+  rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
+    default = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :locale => :en)
+    message = I18n.translate(:"errors.messages.mini_magick_processing_error", :e => e, :default => default)
+    raise CarrierWave::ProcessingError, message
+  end
+end