Sha256: 203c5f69f7ef2448b250ac5d6e977bc24d51bb7671f8057446f06c9f901045e7

Contents?: true

Size: 1.98 KB

Versions: 36

Compression:

Stored size: 1.98 KB

Contents

module Pageflow
  module EntryExportImport
    # Download/upload files during export/import
    module AttachmentFiles
      extend self

      def download_and_archive_originals(revision, archive)
        revision.file_usages.each do |file_usage|
          reusable_file = file_usage.file

          reusable_file.attachments_for_export.each do |attachment|
            download_and_archive_attachment_file(reusable_file, attachment, archive)
          end
        end
      end

      def extract_and_upload_originals(entry, archive_file_name, file_mappings)
        each_reusable_file(entry) do |reusable_file|
          exported_id = file_mappings.exported_id_for(reusable_file.class.name,
                                                      reusable_file.id)

          UploadAndPublishFileJob.perform_later(reusable_file,
                                                exported_id,
                                                archive_file_name)
        end
      end

      def archive_path(reusable_file, attachment, exported_id: reusable_file.id)
        File.join(reusable_file.file_type.collection_name,
                  exported_id.to_s,
                  attachment.name.to_s,
                  attachment.original_filename)
      end

      private

      def each_reusable_file(entry, &block)
        Pageflow.config.file_types.each do |file_type|
          file_type.model.where(entry: entry).each(&block)
        end
      end

      def download_and_archive_attachment_file(reusable_file, attachment, archive)
        archive_path = archive_path(reusable_file, attachment)

        return if archive.include?(archive_path)

        download_attachment_file(attachment) do |tempfile|
          archive.add(archive_path, tempfile)
        end
      end

      def download_attachment_file(attachment)
        tempfile = Paperclip.io_adapters.for(attachment)

        begin
          yield(tempfile)
        ensure
          tempfile.close
          tempfile.unlink
        end
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
pageflow-17.0.4 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-17.0.3 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-17.0.2 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-17.0.1 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-17.0.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-16.2.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-16.1.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-16.0.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.8.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.7.1 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.7.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.6.1 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.6.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.5.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.4.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.3.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.2.2 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.2.1 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.2.0 lib/pageflow/entry_export_import/attachment_files.rb
pageflow-15.1.2 lib/pageflow/entry_export_import/attachment_files.rb