Paperclip renaming files after they're saved

前端 未结 9 1421
情书的邮戳
情书的邮戳 2020-12-07 23:34

How do I rename a file after is has been uploaded and saved? My problem is that I need to parse information about the files automatically in order to come up with the file

相关标签:
9条回答
  • 2020-12-08 00:09

    The following migration solved the problem to me.

    Renaming avatar to photo:

    class RenamePhotoColumnFromUsers < ActiveRecord::Migration
      def up
        add_attachment :users, :photo
    
        # Add `avatar` method (from Paperclip) temporarily, because it has been deleted from the model
        User.has_attached_file :avatar, styles: { medium: '300x300#', thumb: '100x100#' }
        User.validates_attachment_content_type :avatar, content_type: %r{\Aimage\/.*\Z}
    
        # Copy `avatar` attachment to `photo` in S3, then delete `avatar`
        User.where.not(avatar_file_name: nil).each do |user|
          say "Updating #{user.email}..."
    
          user.update photo: user.avatar
          user.update avatar: nil
        end
    
        remove_attachment :users, :avatar
      end
    
      def down
        raise ActiveRecord::IrreversibleMigration
      end
    end
    

    Hope it helps :)

    0 讨论(0)
  • 2020-12-08 00:15

    To add to @Fotios's answer:

    its the best way I think to make custom file name, but in case you want file name based on md5 you can use fingerprint which is already available in Paperclip.

    All you have to do is to put this to config/initializers/paperclip_defaults.rb

    Paperclip::Attachment.default_options.update({
        # :url=>"/system/:class/:attachment/:id_partition/:style/:filename"
        :url=>"/system/:class/:attachment/:style/:fingerprint.:extension"
        })
    

    There's no need to set :path here as by default it's made that way:

    :path=>":rails_root/public:url"
    

    I didn't check if it's necessary but in case it doesn't work for you make sure your model is able to save fingerprints in the database -> here

    One more tip which I find handy is to use rails console to check how it works:

    $ rails c --sandbox
    > Paperclip::Attachment.default_options
    ..
    > s = User.create(:avatar => File.open('/foo/bar.jpg', 'rb'))
    ..
    > s.avatar.path
     => "/home/groovy_user/rails_projectes/funky_app/public/system/users/avatars/original/49332b697a83d53d3f3b5bebce7548ea.jpg" 
    > s.avatar.url 
     => "/system/users/avatars/original/49332b697a83d53d3f3b5bebce7548ea.jpg?1387099146" 
    
    0 讨论(0)
  • 2020-12-08 00:15

    Another option is set to default, work for all upload.

    This example change name file to 'name default' for web, example: test áé.jpg to test_ae.jpg

    helper/application_helper.rb

    def sanitize_filename(filename)
        fn = filename.split /(?<=.)\.(?=[^.])(?!.*\.[^.])/m
        fn[0] = fn[0].parameterize
        return fn.join '.'
    end
    

    Create config/initializers/paperclip_defaults.rb

    include ApplicationHelper
    
    Paperclip::Attachment.default_options.update({
        :path => ":rails_root/public/system/:class/:attachment/:id/:style/:parameterize_file_name",
        :url => "/system/:class/:attachment/:id/:style/:parameterize_file_name",
    })
    
    Paperclip.interpolates :parameterize_file_name do |attachment, style|
        sanitize_filename(attachment.original_filename)
    end
    

    Need restart, after put this code

    0 讨论(0)
提交回复
热议问题