0
-desc "Explaining what the task does"
0
-namespace :fleximage do
0
+namespace :fleximage do
0
- desc "Convert a flat images/123.png style image store to a images/2007/11/12/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
- task :to_nested => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ # Find the model class
0
+ raise 'You must specify a FLEXIMAGE_CLASS=MyClass' unless ENV['FLEXIMAGE_CLASS']
0
+ @model_class ||= ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ def convert_directory_format(to_format)
0
model_class.find(:all).each do |obj|
0
+ # Get the creation date
0
creation = obj[:created_at] || obj[:created_on]
0
- old_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
- new_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
+ # Generate both types of file paths
0
+ flat_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
+ nested_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
- if old_path != new_path && File.exists?(old_path)
0
- FileUtils.mkdir_p(File.dirname(new_path))
0
- FileUtils.move old_path, new_path
0
- puts "#{old_path} -> #{new_path}"
0
+ # Assign old path and new path based on desired directory format
0
+ if to_format == :nested
0
+ new_path = nested_path
0
+ old_path = nested_path
0
- desc "Convert a nested images/2007/11/12/123.png style image store to a images/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
- task :to_flat => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
- model_class.find(:all).each do |obj|
0
- creation = obj[:created_at] || obj[:created_on]
0
- old_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{creation.year}/#{creation.month}/#{creation.day}/#{obj.id}.#{model_class.image_storage_format}"
0
- new_path = "#{RAILS_ROOT}/#{model_class.image_directory}/#{obj.id}.#{model_class.image_storage_format}"
0
if old_path != new_path && File.exists?(old_path)
0
FileUtils.mkdir_p(File.dirname(new_path))
0
FileUtils.move old_path, new_path
0
@@ -35,36 +35,55 @@ namespace :fleximage do
0
- desc "Convert master images stored as JPGs to PNGs"
0
- task :to_png => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
+ def convert_image_format(to_format)
0
model_class.find(:all).each do |obj|
0
- old_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
- new_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
+ # Generate both types of file paths
0
+ png_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
+ jpg_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
+ output = (to_format == :jpg) ? 'PNG -> JPG' : 'JPG -> PNG'
0
+ # Assign old path and new path based on desired image format
0
if File.exists?(old_path)
0
image = Magick::Image.read(old_path).first
0
+ image.format =
to_format.to_s.upcase0
- puts "JPG -> PNG : Image #{obj.id}"
0
+ puts "#{output} : Image #{obj.id}"
0
+ desc "Convert a flat images/123.png style image store to a images/2007/11/12/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
+ task :to_nested => :environment do
0
+ convert_directory_format :nested
0
+ desc "Convert a nested images/2007/11/12/123.png style image store to a images/123.png style. Requires FLEXIMAGE_CLASS=ModelName"
0
+ task :to_flat => :environment do
0
+ convert_directory_format :flat
0
+ desc "Convert master images stored as JPGs to PNGs"
0
+ task :to_png => :environment do
0
+ convert_image_format :png
0
desc "Convert master images stored as PNGs to JPGs"
0
task :to_jpg => :environment do
0
- model_class = ENV['FLEXIMAGE_CLASS'].camelcase.constantize
0
- model_class.find(:all).each do |obj|
0
- old_path = obj.file_path.gsub(/\.jpg$/, '.png')
0
- new_path = obj.file_path.gsub(/\.png$/, '.jpg')
0
- if File.exists?(old_path)
0
- image = Magick::Image.read(old_path).first
0
- puts "PNG -> JPG : Image #{obj.id}"
0
+ convert_image_format :jpg
Comments
No one has commented yet.