Guest User

Untitled

a guest
Jun 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. ##
  2. # CarrierWave Amazon S3 File Reprocessor Rake Task
  3. #
  4. # Written (specifically) for:
  5. # - CarrierWave
  6. # - Ruby on Rails 3
  7. # - Amazon S3
  8. #
  9. # Works with:
  10. # - Any server of which you have write-permissions in the Rails.root/tmp directory
  11. # - Works with Heroku
  12. #
  13. # Not tested with, but might work with:
  14. # - Ruby on Rails 2
  15. #
  16. # Might work with, after a couple of tweaks:
  17. # - File System Storage
  18. # - Cloud Files Storage
  19. # - GridFS
  20. #
  21. # Examples:
  22. #
  23. # Reprocess all versions of User#avatar
  24. # rake carrierwave:reprocess class=User mounted_uploader=avatar
  25. #
  26. # Reprocess the versions: thumb, small, medium for User#avatar
  27. # rake carrierwave:reprocess class=User mounted_uploader=avatar versions='thumb, small, medium'
  28. #
  29. # Reprocess for an underlying association, for things like Embedded MongoDB Documents
  30. # which are models you cannot access directly, but rather through a regular Document
  31. #
  32. # Embeds One (picture) Association
  33. # rake carrierwave:reprocess class=User association=picture mounted_uploader=image versions='thumb, small, medium'
  34. #
  35. # Embeds Many (pictures) Association
  36. # rake carrierwave:reprocess class=User association=pictures mounted_uploader=image versions='thumb, small, medium'
  37. #
  38. # WARNING
  39. # There is an issue with "Rake", that you cannot name your mounted_uploader "file".
  40. # If you do this, then you will not be able to reprocess images through this rake task
  41. # class User
  42. # include Mongoid::Document
  43. # mount_uploader :file, PictureUploader
  44. # end
  45. #
  46. # This will NOT work with reprocessing through Rake because the mounted_uploader uses the "file" attribute.
  47.  
  48. namespace :carrierwave do
  49.  
  50. ##
  51. # Only tested with Amazon S3 Storage
  52. # Needs some minor modifications if you want to use this for File System Store, Cloud Files and GridFS probably.
  53. # This should work without Ruby on Rails as well, just set a different TMP_PATH.
  54. desc "Reprocesses Carrier Wave file versions of a given model."
  55. task :reprocess => :environment do
  56.  
  57. ##
  58. # Load in the OPEN URI library to be able
  59. # to pull down and store the original file in a temp directory
  60. require 'open-uri'
  61.  
  62. ##
  63. # Default constants
  64. TMP_PATH = "#{Rails.root}/tmp/carrierwave"
  65.  
  66. ##
  67. # Set environment constants
  68. CLASS = ENV['class'].capitalize
  69. ASSOCIATION = ENV['association'] || nil
  70. MOUNTED_UPLOADER = ENV['mounted_uploader'].to_sym
  71. VERSIONS = ENV['versions'].nil? ? Array.new : ENV['versions'].split(',').map {|version| version.strip.to_sym}
  72.  
  73. ##
  74. # Find the Model
  75. MODEL = Kernel.const_get(CLASS)
  76.  
  77. ##
  78. # Create the temp directory
  79. %x(mkdir -p "#{TMP_PATH}")
  80.  
  81. ##
  82. # Find all records for the provided Model
  83. records = MODEL.all
  84.  
  85. ##
  86. # Output to console
  87. puts "\nCarrier Wave Version Reprocessing!"
  88. puts "======================================="
  89. puts "Model: #{CLASS}"
  90. puts "Mounted Uploader: #{MOUNTED_UPLOADER}"
  91. puts "Association: #{ASSOCIATION}" if ASSOCIATION
  92. puts "Versions: #{VERSIONS.empty? ? "all" : VERSIONS.join(', ')}\n\n"
  93.  
  94. ##
  95. # Run through all records
  96. records.each do |record|
  97.  
  98. ##
  99. # Set the mounted uploader object
  100. # If it has a one-to-one association (singular) then that object
  101. # will be returned and wrapped in an array so we can "iterate" through it below.
  102. #
  103. # If it has a one-to-many association then it will return the array of associated objects
  104. #
  105. # If no association is specified, it assumes the amounted uploader is attached to the specified CLASS
  106. if ASSOCIATION
  107. if ASSOCIATION.singular?
  108. objects = [record.send(ASSOCIATION)]
  109. else
  110. objects = record.send(ASSOCIATION)
  111. end
  112. else
  113. objects = [record]
  114. end
  115.  
  116. ##
  117. # Iterates through the objects
  118. objects.each do |object|
  119.  
  120. ##
  121. # Returns the mounted uploader object
  122. mounted_object = object.send(MOUNTED_UPLOADER)
  123.  
  124. next if mounted_object.path.blank? # Got no file uploaded so we'll skip it.
  125.  
  126. ##
  127. # Retrieve Filename
  128. filename = mounted_object.path.split('/').last
  129.  
  130. ##
  131. # Output to console
  132. puts "Reprocessing: #{filename}"
  133.  
  134. ##
  135. # Read out the original file from the remote location
  136. # and write it out to the temp directory (TMP_PATH)
  137. # This file will be used as the base file to reprocess
  138. # the versions. Once all versions have been processed,
  139. # this temp file will be directly removed.
  140. open(mounted_object.url) do |original_object|
  141. File.open(File.join(TMP_PATH, filename), 'w') do |temp_file|
  142. temp_file.write(original_object.read)
  143. end
  144. end
  145.  
  146. ##
  147. # By default it will add all available versions to the versions variable
  148. # which means that all available versions will be reprocessed.
  149. # If the "versions" argument has been provided, then only the specified
  150. # version(s) will be set to the versions variable, and thus, only these
  151. # will be reprocessed.
  152. versions = mounted_object.versions.map {|version| version[0]}
  153. versions = VERSIONS unless VERSIONS.empty?
  154.  
  155. ##
  156. # Reprocesses the versions
  157. versions.each do |version|
  158. mounted_object.send(version).cache!(File.open(File.join(TMP_PATH, filename)))
  159. mounted_object.send(version).store!
  160. end
  161.  
  162. ##
  163. # Removes the temp file
  164. %x(rm "#{TMP_PATH}/#{filename}")
  165. end
  166. end
  167. end
  168. end
Add Comment
Please, Sign In to add comment