diff options
| author | mo khan <mo@mokhan.ca> | 2015-01-20 20:53:03 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-01-20 20:53:03 -0700 |
| commit | 75d7617a7d44e49fd173dfbe4d98faec1538c9e7 (patch) | |
| tree | be9b424ebffed3f62e516daecd95d2968bd6a26a /app/services | |
| parent | 457646d8ffbf3043195b30b2e20b7350c0ec6a1c (diff) | |
extract temporary storage class.
Diffstat (limited to 'app/services')
| -rw-r--r-- | app/services/application/upload_photo.rb | 32 | ||||
| -rw-r--r-- | app/services/infrastructure/temporary_storage.rb | 15 |
2 files changed, 31 insertions, 16 deletions
diff --git a/app/services/application/upload_photo.rb b/app/services/application/upload_photo.rb index 659bcd95..f1fc7fae 100644 --- a/app/services/application/upload_photo.rb +++ b/app/services/application/upload_photo.rb @@ -1,28 +1,28 @@ class UploadPhoto - def initialize(cakes = Creation) - @cakes = cakes + attr_reader :cake, :storage + + def initialize(cake) + @cake = cake + @storage = TemporaryStorage.new end - def run(cake_id, params) - ActiveRecord::Base.transaction do - photo = @cakes.find(cake_id).photos.create!(image_processing: true, watermark: params[:watermark]) - tempfile = move_to_temporary_storage(params[:image].path, params[:image].original_filename) - ProcessPhotoJob.perform_later(photo, tempfile) - photo + def run(params) + with_transaction do + create_photo!(params[:watermark]) do |photo| + ProcessPhotoJob.perform_later(photo, storage.store(params[:image])) + end end end private - 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 + def with_transaction + ActiveRecord::Base.transaction do + yield + end end - def create_tmp_dir - directory = Rails.root.join("tmp/uploads/#{SecureRandom.uuid}") - system "mkdir -p #{directory}" - directory + def create_photo!(watermark) + cake.photos.create!(image_processing: true, watermark: watermark) end end diff --git a/app/services/infrastructure/temporary_storage.rb b/app/services/infrastructure/temporary_storage.rb new file mode 100644 index 00000000..5ce8db49 --- /dev/null +++ b/app/services/infrastructure/temporary_storage.rb @@ -0,0 +1,15 @@ +class TemporaryStorage + def store(file) + "#{tmp_dir}/#{file.original_filename}".tap do |new_path| + FileUtils.mv(file.path, new_path) + end + end + + private + + def tmp_dir + Rails.root.join("tmp/uploads/#{SecureRandom.uuid}").tap do |directory| + system "mkdir -p #{directory}" + end + end +end |
