在Carrierwave中手动上传和保存文件

时间:2013-07-31 19:11:31

标签: ruby file-upload carrierwave ruby-on-rails-4 rmagick

我有一个现有文件的目录,我需要将其作为旧版迁移的一部分迁移到我的rails应用程序中。基本上我需要手动上传这些文件并在数据库中为它们保存新记录。我还没有找到正确的方法来做到这一点。目前我在rake任务中有以下内容:

@attachments.each do |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 to 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 successfully "

  rescue => error
    puts "Error migrating Attachment: #{error}"
  end
end

attachment.rb

class Attachment < ActiveRecord::Base
     mount_uploader :file, FileUploader
end

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 do
    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

目前无效。该文件永远不会上传,偶尔我会收到以下错误:

Failed to manipulate with rmagick, maybe it is not an image? Original Error: no decode delegate for this image format

在这种情况下,该文件是'.doc'文件。

打开本地文件并通过Carrierwave手动上传的正确方法是什么?

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

试试这个

@attachments.each do |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 successfully "

  rescue => error
    puts "Error migrating Attachment: #{error}"
  end
end

也许这会对你有用,因为carrierwave会在内部为你调用store!

问题?

Failed to manipulate with rmagick, maybe it is not an image? Original Error: no decode delegate for this image format

不确定你在这里尝试了什么,因为你已经定义了一个image?方法,该方法未在条件中指定,也就是你希望content_type仅存在于{{1文件

如果image可能只有进程调用可以正常工作

no

如果process :set_content_type那么也许你必须做这样的事情

yes

希望这个帮助

根据评论编辑

尝试这个只是使用条件相同的逻辑

process :set_content_type , :if => :image?

def image?(new_file)
  %w(jpg jpeg gif).include?(new_file.extension)
end