1

我有一个现有文件的目录,作为遗留迁移的一部分,我需要将其迁移到我的 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

附件.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 手动上传的正确方法是什么?

任何帮助表示赞赏。

4

1 回答 1

4

尝试这个

@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只存在于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

希望这有帮助

根据评论编辑

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

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

 
于 2013-08-01T13:30:06.670 回答