Sha256: 8753ae4efb9582163d6a8556dd8237f03843f4ad50e3d7c60dcd2447df78cf03

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Lcms
  module Engine
    class Download < ActiveRecord::Base
      CONTENT_TYPES = {
        zip: 'application/zip',
        pdf: 'application/pdf',
        excel: %w(application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet),
        powerpoint: %w(application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation), # rubocop:disable Metrics/LineLength
        doc: %w(application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document)
      }.freeze
      S3_URL = 'http://k12-content.s3-website-us-east-1.amazonaws.com/'
      URL_PUBLIC_PREFIX = 'public://'

      mount_uploader :filename, DownloadUploader
      alias_attribute :file, :filename

      validates :title, presence: true
      validates :file, presence: true, if: 'url.nil?'
      validates :url, presence: true, if: 'file.nil?'

      before_save :update_metadata

      def attachment_url
        if url.present?
          url.sub(URL_PUBLIC_PREFIX, S3_URL)
        else
          file.url
        end
      end

      def s3_filename
        File.basename(attachment_url)
      end

      def attachment_content_type
        type = content_type
        CONTENT_TYPES.each do |key, types|
          if Array.wrap(types).include?(content_type)
            type = key
            break
          end
        end
        type
      end

      private

      def update_metadata
        if file.present?
          self.content_type = file.file.content_type
          self.filesize = file.file.size
        end

        true
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lcms-engine-0.1.4 app/models/lcms/engine/download.rb
lcms-engine-0.1.3 app/models/lcms/engine/download.rb
lcms-engine-0.1.2 app/models/lcms/engine/download.rb
lcms-engine-0.1.0 app/models/lcms/engine/download.rb