When uploading a picture using Carrierwave, multiple versions of the file name are unified

Source: Internet
Author: User

The first time to use Carrierwavewe do upload, can not say, although rails has been the upload has become ultra-simple, and carrierwave is to upload into a kind of enjoyment, especially to do picture upload, now this era, the picture display platform is not just a computer, Each mobile is also necessary to take care of, which led to the uploading of pictures, generally need to have several version size, if also pure manual use of Rmagick and other slowly processing, is a bit depressed, and carrierwave with Minimagick, a version of Everything, It's really a feeling of "good times".

However, in the use of the process, also found a few small problems, the most important thing is that when there are multiple picture size version, the resulting file name is inconsistent. The carrierwave itself generates a file name with a timestamp, but when there are multiple versions, the first version, which starts a new instance, causes the resulting file name to be different in time, resulting in a different filename.

Online search, better and more convenient is Daniel Li Huashun method, using the temporary file path timestamp to do MD5, get a consistent file name. But the file name format I requested this time is different and requires the following:

User Id_ Time _ random number. suffix

After many attempts, it was implemented in a less beautiful way.

Class Shopimguploader < Carrierwave::uploader::baseinclude carrierwave::minimagickprocess:resize_to_fit =>[ 500,500]version:thumb Doprocess:resize_to_fill = [200,200]end# key, add a method, directly set the file name # my p here is the user ID, Plus name is because each new version will start a new instance # new instance, there is no @name, so when we generate the file name, we force a call to def set_prefix p,name=nil@prefix = pif name.nil?@ Name = "#{@prefix}_#{time.now.strftime '%y%m%d%h%m%s%l '}_#{rand (1..100)}" Else@name = nameendend# Generate file name Rule def filename# The @versions is not empty until the original file instance, so the unless @versions will not be called repeatedly. nil? @versions. Each does |version,obj| #很粗暴的直接指定名称, each version is preceded by a version prefix. such as Thumb_obj.set_prefix (@prefix, @name) endend "#{@name}.#{file.extension}" EndEnd

  

When uploading a picture using Carrierwave, multiple versions of the file name are unified

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.