Rails 4とペーパークリップ-:originalスタイルのファイルのアップロードを停止して、S3リモートディレクトリからコピーします

私はPaperclip4.0.2を使用し、アプリで写真をアップロードしています。

したがって、私のドキュメントモデルにはattached_fileがあります。 attachment

添付ファイルにはいくつかのスタイルがあります、 :medium, :thumb, :facebook

モデルでは、スタイルの処理を停止し、バックグラウンドジョブ内で抽出しました。

class Document < ActiveRecord::Base
# stop paperclip styles generation    
before_post_process
  false
end

しかし、:originalスタイルのファイルはまだアップロードされています!

この動作を停止して、リモートディレクトリから:original / filename.jpg内のファイルをコピーできるかどうかを知りたいです。私の目標は、jQueryファイルアップロードでS3 / temp /ディレクトリにアップロードされたファイルを使用することです。 、そしてそれをPaperclipが他のスタイルを生成するために必要とするディレクトリにコピーします。

よろしくお願いします!

Laertiades

新しい答え

ペーパークリップの添付ファイルflush_writesは、Paperclip :: Storage :: S3モジュールの一部であるメソッドでアップロードされます。アップロードを担当する行は次のとおりです。

s3_object(style).write(file, write_options)

したがって、monkey_patchを使用して、これを次のように変更できます。

s3_object(style).write(file, write_options) unless style.to_s == "original" and @queued_for_write[:your_processed_style].present?

編集:これは、次のファイルを作成することで実現されます。config/initializers/decorators/paperclip.rb

Paperclip::Storage::S3.class_eval do
  def flush_writes #:nodoc:
    @queued_for_write.each do |style, file|
    retries = 0
      begin
        log("saving #{path(style)}")
        acl = @s3_permissions[style] || @s3_permissions[:default]
        acl = acl.call(self, style) if acl.respond_to?(:call)
        write_options = {
          :content_type => file.content_type,
          :acl => acl
        }

        # add storage class for this style if defined
        storage_class = s3_storage_class(style)
        write_options.merge!(:storage_class => storage_class) if storage_class

        if @s3_server_side_encryption
          write_options[:server_side_encryption] = @s3_server_side_encryption
        end

        style_specific_options = styles[style]

        if style_specific_options
          merge_s3_headers( style_specific_options[:s3_headers], @s3_headers, @s3_metadata) if style_specific_options[:s3_headers]
          @s3_metadata.merge!(style_specific_options[:s3_metadata]) if style_specific_options[:s3_metadata]
        end

        write_options[:metadata] = @s3_metadata unless @s3_metadata.empty?
        write_options.merge!(@s3_headers)

        s3_object(style).write(file, write_options) unless style.to_s == "original" and @queued_for_write[:your_processed_style].present?
      rescue AWS::S3::Errors::NoSuchBucket
        create_bucket
        retry
      rescue AWS::S3::Errors::SlowDown
        retries += 1
        if retries <= 5
          sleep((2 ** retries) * 0.5)
          retry
        else
          raise
        end
      ensure
        file.rewind
      end
    end
    after_flush_writes # allows attachment to clean up temp files
    @queued_for_write = {}
  end
end

現在、オリジナルはアップロードされません。次に、s3に直接アップロードされた場合にオリジナルを適切な最終的な場所に転送する場合は、以下の私の元の回答のようないくつかの行をモデルに追加できます。

元の回答

おそらく、after_createコールバックで実行されたモデルに配置された次のようなものです。

    paperclip_file_path = "relative/final/destination/file.jpg"
    s3.buckets[BUCKET_NAME].objects[paperclip_file_path].copy_from(relative/temp/location/file.jpg)

https://github.com/uberllamaに感謝します

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ