Class: Pageflow::EntryExportImport::UploadAndPublishFileJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/pageflow/entry_export_import/upload_and_publish_file_job.rb

Overview

Uploads a file attachment associated with an ReusableFile and publishes the file afterwards

Instance Method Summary collapse

Instance Method Details

#perform(reusable_file, exported_id, archive_file_name) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/jobs/pageflow/entry_export_import/upload_and_publish_file_job.rb', line 8

def perform(reusable_file, exported_id, archive_file_name)
  archive = ZipArchive.new(archive_file_name)

  reusable_file.attachments_for_export.each do |attachment|
    archive_path = AttachmentFiles.archive_path(reusable_file,
                                                attachment,
                                                exported_id: exported_id)

    archive.extract_to_tempfile(archive_path) do |tempfile|
      # Paperclip skips post processing anyway since the name of
      # the tempfile does not pass the validation defined in
      # UploadableFile. To be explicit, we disable post
      # processing manually as well.
      attachment.post_processing = false
      attachment.assign(tempfile)

      # Calling `attachment.assign` changes the
      # `<attachment-name>_file_name` attribute based on the
      # name of the tempfile. To prevent `flush_writes` from
      # using these new values when constructing the
      # destionation path, we need to undo the attribute
      # changes. Restoring the attributes does not reset the
      # list of files queued for write  in the attachment.
      reusable_file.restore_attributes

      attachment.flush_writes
    end
  end

  reusable_file.publish!
end