Skip to content

Instantly share code, notes, and snippets.

@seancdavis
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seancdavis/b86cfab4b701e2bbe9d0 to your computer and use it in GitHub Desktop.
Save seancdavis/b86cfab4b701e2bbe9d0 to your computer and use it in GitHub Desktop.
Transition from CarrierWave to Dragonfly in Rails
require 'csv'
desc 'Import local files (with csv references) using Dragonfly'
task :dragonfly_import => :environment do
config = [
{:model => Attachment, :attr => :document}
]
storage_dir = "#{Rails.root}/lib/cw_refs"
config.each do |c|
config_file = "#{c[:model].to_s.underscore}_#{c[:attr].to_s}"
dir = "#{storage_dir}/#{config_file}"
csv_file = "#{dir}/#{config_file}.csv"
CSV.foreach(csv_file, :headers => true) do |row|
obj = c[:model].find_by_id(row['id'].to_i)
unless obj.nil?
file = File.open(row[c[:attr].to_s])
unless file.nil?
if obj.update(c[:attr].to_sym => file)
puts "IMPORTED FILE >> #{row[c[:attr].to_s].split('/').last}"
else
puts "-- COULD NOT IMPORT FILE >> #{row[c[:attr].to_s].split('/').last}"
end
end
end
end
end
end
require 'csv'
require 'fileutils'
require 'open-uri'
desc 'Store references to CarrierWave files'
task :store_cw_refs => :environment do
# This example assumes an Image model with an attached "image" uploader. Add
# your models and their attributes here.
#
# If your model has more than one uploader, then reuse the model, but add each
# in their own hash here.
#
config = [
{:model => Image, :attr => :image}
]
# Create storage directories
#
# If you want to rename this directory, do so below:
#
storage_dir = "#{Rails.root}/lib/cw_refs"
FileUtils.mkdir_p(storage_dir) unless Dir.exists?(storage_dir)
# Write to CSV files based on config and download files
#
config.each do |c|
# create references to this group of files
#
config_file = "#{c[:model].to_s.underscore}_#{c[:attr].to_s}"
dir = "#{storage_dir}/#{config_file}"
csv_file = "#{dir}/#{config_file}.csv"
FileUtils.mkdir_p(dir) unless Dir.exists?(dir)
# Create CSV file
#
File.open(csv_file, 'w+') do |f|
f << "id,#{c[:attr].to_s}\n"
end
puts "CREATED CSV FILE >> #{csv_file.split('/').last}"
# Step through each record, store the reference and download the file
#
File.open(csv_file, 'a') do |f|
# Step through each record in the database for this config
#
c[:model].all.each do |obj|
# We don't need to waste our time if we don't have any data
#
unless obj.send(c[:attr].to_s).to_s.blank?
# The url reference to the file (using your CarrierWave uploader). You
# may have to change this based on your config.
#
url = obj.send(c[:attr].to_s).url
# Safely parse and encode the url if using fog storage.
#
# NOTE: ONLY UNCOMMENT THIS LINE IF YOU ARE USING FOG STORAGE
#
# url = URI.parse(URI.encode(url.strip))
# Create directory for storing file, then download the file.
#
img_dir = "#{dir}/#{obj.id}"
img_file = "#{img_dir}/#{url.to_s.split('/').last}"
img_filename = img_file.split('/').last
FileUtils.mkdir_p(img_dir) unless Dir.exists?(img_dir)
puts "DOWNLOADING IMAGE >> #{img_filename} ..."
open(img_file, 'wb') do |file|
# If using fog storage ...
#
# file << url.open.read
# If using local storage ...
#
file << File.read(url)
end
puts "STORED IMAGE >> #{img_filename}"
f << "#{obj.id},\"#{img_file}\"\n"
puts "RECORDED REFERENCE TO >> #{img_filename}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment