Sha256: 2fb21dc849389075a329d0479db2026719f0f8a146a29f54c3d47461f99d3df5

Contents?: true

Size: 1.66 KB

Versions: 9

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    class MaterialGeneratePdfJob < Lcms::Engine::ApplicationJob
      include ResqueJob

      include RetrySimple

      queue_as :default

      def perform(material, document)
        material_links = material.pdf? ? links_from_metadata(material) : links_from_upload(material, document)

        new_links = {
          'materials' => {
            material.id.to_s => material_links
          }
        }

        document.with_lock do
          links = document.reload.links
          document.update links: links.deep_merge(new_links)
        end

        Lcms::Engine::DocumentGenerateJob.perform_later(document, check_queue: true)
      end

      private

      def links_from_upload(material, document)
        material = material_presenter(material, document)

        basename = "#{::DocumentExporter::Pdf::Base.s3_folder}/#{material.pdf_filename}"
        pdf_filename = "#{basename}#{ContentPresenter::PDF_EXT}"
        thumb_filename = "#{basename}#{ContentPresenter::THUMB_EXT}"

        pdf = ::DocumentExporter::Pdf::Material.new(material).export
        thumb = ::DocumentExporter::Thumbnail.new(pdf).export

        pdf_url = S3Service.upload pdf_filename, pdf
        thumb_url = S3Service.upload thumb_filename, thumb

        { 'url' => pdf_url, 'thumb' => thumb_url }
      end

      def material_presenter(material, document)
        DocumentGenerator.material_presenter.new material, lesson: DocumentGenerator.document_presenter.new(document)
      end

      def links_from_metadata(material)
        { 'url' => material.metadata['pdf_url'], 'thumb' => material.metadata['thumb_url'] }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.5.4 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.5.3 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.5.2 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.5.1 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.5.0 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.4.2 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.4.1 app/jobs/lcms/engine/material_generate_pdf_job.rb
lcms-engine-0.4.0 app/jobs/lcms/engine/material_generate_pdf_job.rb