【问题标题】:Export paperclip images in a clean hierarchy以干净的层次结构导出回形针图像
【发布时间】:2015-04-09 17:52:41
【问题描述】:

我正在开发一个即将关闭的 rails 3.2.8 应用程序。

这个应用有一个帖子模型,它有_many 资产。

资产模型由回形针管理。

class Asset < ActiveRecord::Base
attr_accessible :post_id, :photo
  belongs_to :post                
  has_attached_file :photo, 
                    :styles => {
                      :thumb => '260x260#',
                      :normal => {
                        :geometry => '600x600>',
                      }
                    }
end

我需要登录生产环境并运行一个命令,将每张照片备份到文件系统中的另一个文件夹,如下所示:

~/backup-folder/post-name-foo/post-name-foo-1.png
~/backup-folder/post-name-foo/post-name-foo-2.png
~/backup-folder/post-name-bar/post-name-bar-1.png
~/backup-folder/post-name-bar/post-name-bar-2.png
~/backup-folder/post-name-bar/post-name-bar-3.png
~/backup-folder/post-name-baz/post-name-baz-1.jpg

图片可以是 png 或 jpg 格式,也可能是 JPG(大写),并且帖子可以有任意数量的附件。

我只需要运行一次命令,如果可能的话,我希望避免对应用程序进行更改(例如安装新的 gem)。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 paperclip backup


    【解决方案1】:

    这里是使用rake命令备份文件的例子,你可以增强它的文件夹备份逻辑,希望对你有帮助

    1. 在&lt;root_folder&gt;/lib/task/photo_backup.rake 中创建一个 rake 文件。

      task :photo_backup => :environment do
          require 'rubygems'
          require 'rake'
      
          # file path on production 
          filename = "post-name-foo-1.png"  
          path_to_file = "#{Rails.root.to_s}/images_folder/#{filename}"
      
          # backup folder on production
          backup_folder_path = "#{Rails.root.to_s}/backup_folder"
          backup_dir = FileUtils.makedirs(backup_folder_path).first
      
          begin
             f = File.new("#{backup_dir}/#{filename}", "w+")
             data = open(path_to_file).read
             f.write(data)
          rescue Exception => exp
             puts("Error message : #{exp.message}")
          ensure
             f.close unless f == nil
          end
      end
      
    2. 从控制台在您的项目根目录中执行以下命令。

      rake photo_backup
      

    【讨论】:

      【解决方案2】:

      我会使用 shell cp 命令将资产复制到备份目录;这应该比在 Ruby 中打开和读取每个资产要快得多:

      require 'shellwords'
      
      backup_destination = '/home/app/backup_directory'
      
      # create the backup base directory
      FileUtils.mkdir_p backup_destination
      
      Post.find_each do |post|
        # change this if you generate your 'nice' post URLs in a different manner
        post_name       = post.to_param
        post_backup_dir = File.join(backup_destination, post_name)
      
        FileUtils.mkdir_p post_backup_dir
      
        post.assets.each_with_index do |asset, index|
          file_extension = File.extname(asset.photo_file_name)
          new_file_name  = "#{post_name}-#{index}#{file_extension}"
      
          # escape spaces in paths
          source_path = Shellwords.shellescape asset.photo.path(:original)
          target_path = Shellwords.shellescape File.join(post_backup_dir, new_file_name)
      
          output = `cp #{source_path} #{target_path} 2>&1`
      
          # make sure cp worked correctly
          unless $?.exitstatus == 0
            puts "WARNING: failed to copy asset #{asset.id} from #{source_path} to #{target_path}: #{output}"
          end
        end
      end
      

      【讨论】:

      • 太棒了!!谢谢,我在不到 2 分钟的时间内完成了完整的资产备份,你让我很开心!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-03
      • 2022-01-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多