ruby - Manually upload and save files in Carrierwave -


i have directory of existing files need migrate rails app part of legacy migration. need upload these files manually , save new record them in database. haven't quite found proper way this. have following in rake task:

@attachments.each |attachment|   begin     new_attachment = attachment.new      @attachment_file_path = "/home/username/attachments/" + attachment.filename     file = file.open(@attachment_file_path)     new_attachment[:file] = new_attachment.file.store!(file)      # map old record fields new     new_attachment.attributes = {         :project_id => attachment.projectid,         :name => attachment.description,         :user_id => attachment.userid,         :created_at => attachment.createddate,         :updated_at => attachment.lastmodifieddate     }      new_attachment.save!      puts "attachment added "    rescue => error     puts "error migrating attachment: #{error}"   end end 

attachment.rb

class attachment < activerecord::base      mount_uploader :file, fileuploader end 

uploader:

class fileuploader < carrierwave::uploader::base    include carrierwave::rmagick   include carrierwave::mimetypes    process :set_content_type   storage :fog    def store_dir     "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"   end    def extension_white_list     %w(jpg jpeg gif png pdf doc docx txt)   end    version :thumb     process resize_to_fit: [152, nil]   end    def default_url       actioncontroller::base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))   end    protected     def image?(new_file)       if new_file.content_type == nil         return false       else         new_file.content_type.include? 'image'       end     end   end 

this not work currently. file never gets uploaded, , following error:

failed manipulate rmagick, maybe not image? original error: no decode delegate image format 

in instance, file '.doc' file.

what correct way open local file , upload manually via carrierwave?

any appreciated.

try

@attachments.each |attachment|   begin     options =  {         :project_id => attachment.projectid,         :name => attachment.description,         :user_id => attachment.userid,         :created_at => attachment.createddate,         :updated_at => attachment.lastmodifieddate,         :file => file.new(file.join("/home/username/attachments/",attachment.filename))      }       new_attachment = attachment.new(options)       new_attachment.save!      puts "attachment added "    rescue => error     puts "error migrating attachment: #{error}"   end end 

perhaps carrierwave internally call store!

question?

failed manipulate rmagick, maybe not image? original error: no decode delegate image format 

not sure trying on here because have define image? method not specified in condition want content_type present image file

if no perhaps process call work

process :set_content_type

if yes perhaps have

process :set_content_type , :if => :image?  def image?(new_file)   %w(jpg jpeg gif).include?(new_file.extension) end 

hope help

edit based upon comment

try used condition same logic

   version :thumb ,:if => image?      // code     end    

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -