Sha256: 37948d13c6383c0282de75ab0b7fb1fdd8e8a1f6de5b97948497f677befd2a0b

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 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"
    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

13 entries across 13 versions & 1 rubygems

Version Path
ddr-models-3.0.0.beta.10 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.9 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.8 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.7 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.6 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.4 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.3 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.2 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.beta.1 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.alpha.4 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.alpha.3 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.alpha.2 lib/ddr/derivatives/multires_image.rb
ddr-models-3.0.0.alpha.1 lib/ddr/derivatives/multires_image.rb