Commit 643c57c0

mo khan <mo@mokhan.ca>
2014-09-23 01:41:30
write script to re-process all photos.
1 parent 5317da5
script/migrate-avatars.rb
@@ -1,16 +0,0 @@
-#!/usr/bin/env ruby
-require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
-
-BlobStorage.new.tap do |blob_storage|
-  Avatar.includes(:user).where('avatar IS NOT NULL').find_each do |avatar|
-    begin
-      key = avatar.avatar.path
-      blob_storage.download(key) do |file|
-        puts file.path
-        UploadAvatar.new.run(avatar.user, OpenStruct.new(path: file.path, original_filename: File.basename(key), content_type: 'image/jpeg'))
-      end
-    rescue StandardError => error
-      puts error.message
-    end
-  end
-end
script/migrate-photos.rb
@@ -0,0 +1,33 @@
+#!/usr/bin/env ruby
+require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
+
+class Command
+  attr_reader :bus, :storage
+
+  def initialize(message_bus = Spank::IOC.resolve(:message_bus), storage = BlobStorage.new)
+    @bus = message_bus
+    @storage = storage
+  end
+
+  def run
+    Photo.find_each do |photo|
+      original = OriginalVersion.new(photo)
+      storage.download(original.create_key) do |file|
+        message_bus.publish(:upload_photo, {
+          photo_id: photo.id,
+          file_path: move_to_temporary_storage(file.path, File.basename(original.create_key))
+        })
+      end
+    end
+  end
+
+  private
+
+  def move_to_temporary_storage(temp_file_path, original_filename)
+    "#{create_tmp_dir}/#{original_filename}".tap do |new_path|
+      FileUtils.mv(temp_file_path, new_path)
+    end
+  end
+end
+
+Command.new.run