summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2014-09-22 19:41:30 -0600
committermo khan <mo@mokhan.ca>2014-09-22 19:41:30 -0600
commit643c57c0fdd4f7a1aa61755ca21268a87c68e8ee (patch)
tree547f673761e5279c7b06b662b19f6549bb0c4e76
parent5317da58469cb1373914bcbb054168ae8353c79f (diff)
write script to re-process all photos.
-rw-r--r--script/migrate-avatars.rb16
-rw-r--r--script/migrate-photos.rb33
2 files changed, 33 insertions, 16 deletions
diff --git a/script/migrate-avatars.rb b/script/migrate-avatars.rb
deleted file mode 100644
index 2e0c2380..00000000
--- a/script/migrate-avatars.rb
+++ /dev/null
@@ -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
diff --git a/script/migrate-photos.rb b/script/migrate-photos.rb
new file mode 100644
index 00000000..bcc42f32
--- /dev/null
+++ b/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