ruby-on-rails-3 – 使用Carrierwave和Ruby重新创建版本

前端之家收集整理的这篇文章主要介绍了ruby-on-rails-3 – 使用Carrierwave和Ruby重新创建版本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在进行的网站正在进行重新设计,因此我们的用户图像需要重新调整大小.该网站目前正在使用carrierwave gem处理所有图像和视频处理,每个图像都有一个原始文件,其文件名基于以下内容
  1. def filename
  2. if original_filename
  3. if model && model.read_attribute(:user_image).present?
  4. model.read_attribute(:user_image)
  5. else
  6. @name ||= "#{secure_token}.#{file.extension}" if original_filename.present?
  7. end
  8. end
  9. end

和secure_token由.生成

  1. def secure_token
  2. var = :"@#{mounted_as}_secure_token"
  3. model.instance_variable_get(var) or model.instance_variable_set(var,SecureRandom.uuid)
  4. end

为此执行创建的任务是:

  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,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,medium'
  34. #
  35. # Embeds Many (pictures) Association
  36. # rake carrierwave:reprocess class=User association=pictures mounted_uploader=image versions='thumb,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. ##
  125. # Retrieve Filename
  126. filename = mounted_object.path.split('/').last
  127.  
  128. ##
  129. # Output to console
  130. puts "Reprocessing: #{filename}"
  131.  
  132. ##
  133. # Read out the original file from the remote location
  134. # and write it out to the temp directory (TMP_PATH)
  135. # This file will be used as the base file to reprocess
  136. # the versions. Once all versions have been processed,# this temp file will be directly removed.
  137. open(mounted_object.url) do |original_object|
  138. File.open(File.join(TMP_PATH,filename),'w') do |temp_file|
  139. temp_file.write(original_object.read)
  140. end
  141. end
  142.  
  143. ##
  144. # By default it will add all available versions to the versions variable
  145. # which means that all available versions will be reprocessed.
  146. # If the "versions" argument has been provided,then only the specified
  147. # version(s) will be set to the versions variable,and thus,only these
  148. # will be reprocessed.
  149. versions = mounted_object.versions.map {|version| version[0]}
  150. versions = VERSIONS unless VERSIONS.empty?
  151.  
  152. ##
  153. # Reprocesses the versions
  154. versions.each do |version|
  155. mounted_object.send(version).cache!(File.open(File.join(TMP_PATH,filename)))
  156. mounted_object.send(version).store!
  157. end
  158.  
  159. ##
  160. # Removes the temp file
  161. %x(rm "#{TMP_PATH}/#{filename}")
  162. end
  163. end
  164. end
  165. end

问题是,虽然它创建新图像,但文件以新文件名保存,而不是遵循图像上传器中列出的命名,因此网站无法找到它们.以下是如何存储图像的示例.

它应该如何:

原始档案:
fdk392ks93_39ei.png

缩略图版本:
thumb_fdk392ks93_39ei.png

怎么样:

原始档案:
fdk392ks93_39ei.png

缩略图版本:
thumb_fajeilkadifej_jakdjfi.png

任何帮助将非常感激.

其他信息:

型号:用户

上传者:user_image(这也是存储文件夹/文件名的列名)

解决方法

调用recreate_versions之后!你得打电话保存!在模型上.您可以查看 this question,其中有人问基本相同的事情.

猜你在找的Ruby相关文章