【问题标题】:Manually upload and save files in Carrierwave在 Carrierwave 中手动上传和保存文件
【发布时间】:2013-07-31 19:11:31
【问题描述】:

我有一个现有文件的目录,作为旧版迁移的一部分,我需要将其迁移到我的 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 手动上传的正确方法是什么?

感谢任何帮助。

【问题讨论】:

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


    【解决方案1】:

    试试这个

    @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   
     
    
     
    

    【讨论】:

    • 感谢您的回复。实际上,我看了,我不再需要“图像?”根本没有方法,所以我删除了它。即使在实施您的更改后,我仍然会收到上述错误。我还尝试删除 'process set_content_type' 仍然是同样的错误。如何防止 'doc' 和文本文件被 rmagick 处理?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多