ruby 在Rails中从CarrierWave过渡到Dragonfly

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 在Rails中从CarrierWave过渡到Dragonfly相关的知识,希望对你有一定的参考价值。

require 'csv'
require 'fileutils'
require 'open-uri'

desc 'Store references to CarrierWave files'
task :store_cw_refs => :environment do

  # This example assumes an Image model with an attached "image" uploader. Add
  # your models and their attributes here.
  # 
  # If your model has more than one uploader, then reuse the model, but add each
  # in their own hash here.
  # 
  config = [
    {:model => Image, :attr => :image}
  ]

  # Create storage directories
  # 
  # If you want to rename this directory, do so below:
  # 
  storage_dir = "#{Rails.root}/lib/cw_refs"
  FileUtils.mkdir_p(storage_dir) unless Dir.exists?(storage_dir)

  # Write to CSV files based on config and download files
  # 
  config.each do |c|

    # create references to this group of files
    # 
    config_file = "#{c[:model].to_s.underscore}_#{c[:attr].to_s}"
    dir = "#{storage_dir}/#{config_file}"
    csv_file = "#{dir}/#{config_file}.csv"
    FileUtils.mkdir_p(dir) unless Dir.exists?(dir)

    # Create CSV file
    # 
    File.open(csv_file, 'w+') do |f| 
      f << "id,#{c[:attr].to_s}\n"
    end
    puts "CREATED CSV FILE >> #{csv_file.split('/').last}"

    # Step through each record, store the reference and download the file
    # 
    File.open(csv_file, 'a') do |f| 

      # Step through each record in the database for this config
      # 
      c[:model].all.each do |obj|

        # We don't need to waste our time if we don't have any data
        # 
        unless obj.send(c[:attr].to_s).to_s.blank?
          
          # The url reference to the file (using your CarrierWave uploader). You
          # may have to change this based on your config.
          # 
          url = obj.send(c[:attr].to_s).url

          # Safely parse and encode the url if using fog storage.
          # 
          # NOTE: ONLY UNCOMMENT THIS LINE IF YOU ARE USING FOG STORAGE
          # 
          # url = URI.parse(URI.encode(url.strip))

          # Create directory for storing file, then download the file.
          # 
          img_dir = "#{dir}/#{obj.id}"
          img_file = "#{img_dir}/#{url.to_s.split('/').last}"
          img_filename = img_file.split('/').last
          FileUtils.mkdir_p(img_dir) unless Dir.exists?(img_dir)
          puts "DOWNLOADING IMAGE >> #{img_filename} ..."
          open(img_file, 'wb') do |file|

            # If using fog storage ...
            # 
            # file << url.open.read

            # If using local storage ...
            # 
            file << File.read(url)

          end
          puts "STORED IMAGE >> #{img_filename}"

          f << "#{obj.id},\"#{img_file}\"\n"
          puts "RECORDED REFERENCE TO >> #{img_filename}"

        end
      end
    end

  end

end
require 'csv'

desc 'Import local files (with csv references) using Dragonfly'
task :dragonfly_import => :environment do

  config = [
    {:model => Attachment, :attr => :document}
  ]

  storage_dir = "#{Rails.root}/lib/cw_refs"

  config.each do |c|

    config_file = "#{c[:model].to_s.underscore}_#{c[:attr].to_s}"
    dir = "#{storage_dir}/#{config_file}"
    csv_file = "#{dir}/#{config_file}.csv"

    CSV.foreach(csv_file, :headers => true) do |row|
      obj = c[:model].find_by_id(row['id'].to_i)
      unless obj.nil?
        file = File.open(row[c[:attr].to_s])
        unless file.nil?
          if obj.update(c[:attr].to_sym => file)
            puts "IMPORTED FILE >> #{row[c[:attr].to_s].split('/').last}"
          else
            puts "-- COULD NOT IMPORT FILE >> #{row[c[:attr].to_s].split('/').last}"
          end
        end
      end
    end
  end
end

以上是关于ruby 在Rails中从CarrierWave过渡到Dragonfly的主要内容,如果未能解决你的问题,请参考以下文章

Ruby on Rails - Heroku上的Carrierwave错误

使用 React、Ruby on rails 5 和 CarrierWave 多次上传文件

Rails Carrierwave NoMethodError

ruby on rails nginx 如何上传大文件?

Image Heavy App 的最佳 Ruby on Rails 架构

在rails和carrierwave中上传之前显示图像预览的最佳方式