Sha256: 19c7b6d22f0a6f0dec23d7d8a59232c10a7905e932ef08704d9ba37243093809

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 KB

Contents

module Ddr::Derivatives
  class MultiresImage < Derivative

    def self.generatable?(object)
      object.can_have_multires_image? &&
          object.has_content? &&
          (object.content_type == "image/tiff" || object.content_type == "image/jpeg")
    end

    def self.has_derivative?(object)
      object.has_multires_image?
    end

    def delete!(object)
      object.multires_image_file_path = nil
      object.save
    end

    protected

    def output_path(object)
      File.join(create_external_file_path!, output_file_name(object))
    end

    def output_file_name(object)
      basename = object.content.original_name.present? ? File.basename(object.content.original_name, '.*') : "multires_image"
      "#{basename}.#{generator.class.output_extension}"
    end

    def store(object, output_path)
      object.multires_image_file_path = output_path
      object.save!
    end

    private

    def create_external_file_path!
      file_path = generate_external_file_path
      FileUtils.mkdir_p(file_path)
      file_path
    end

    #
    # Generates a new external file storage location
    #
    # => {external_file_store}/1/e/69/1e691815-0631-4f9b-8e23-2dfb2eec9c70
    #
    def generate_external_file_path
      File.join(Ddr::Models.multires_image_external_file_store, generate_external_directory_subpath)
    end

    def generate_external_directory_subpath
      subdir = SecureRandom.uuid
      m = Ddr::Models.external_file_subpath_regexp.match(subdir)
      raise "File name does not match external file subpath pattern: #{file_name}" unless m
      subpath_segments = m.to_a[1..-1]
      File.join *subpath_segments, subdir
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ddr-models-3.0.0.rc6 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.rc5 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.rc4 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.rc3 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.rc2 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.rc1 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.22 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.21 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.20 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.18 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.17 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.16 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.15 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.14 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.13 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.12 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.11 lib/ddr/derivatives/multires_image.rb