Skip to content

Instantly share code, notes, and snippets.

@af23me
Created February 26, 2017 22:26
Show Gist options
  • Save af23me/b01c3669a3d0ced6fb98a65576c96c4d to your computer and use it in GitHub Desktop.
Save af23me/b01c3669a3d0ced6fb98a65576c96c4d to your computer and use it in GitHub Desktop.
Paperclip old styles (resolutions) cleaner.
require 'fileutils'
class PaperclipCleaner
class << self
def clean(parent_resource_id)
book_images = ImageModel.where(parent_resource_id: parent_resource_id).to_a
images.each do |image_object|
image_dir = "#{Rails.root}/public#{image_object.page.url.split('/original').first}"
destination_folders = Dir.entries(image_dir).select {|entry| File.directory? File.join(image_dir, entry) and !(entry =='.' || entry == '..') }
current_styles = image_object.page.styles.keys.map(&:to_s) + ["original"]
destination_folders.each do |folder|
dest_path = "#{image_dir}/#{folder}"
if File.exist?(dest_path) && current_styles.exclude?(folder)
FileUtils.remove_dir(dest_path, true)
print "removed: #{dest_path}\n"
else
puts "skipped: #{dest_path}\n"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment