Sha256: c6c22959720cea9939415599fa3c038a1eb29237c83f8840d57b6ad0e24ed09b

Contents?: true

Size: 1.32 KB

Versions: 9

Compression:

Stored size: 1.32 KB

Contents

require 'mime/types'

module Hydra::Derivatives
  class TempfileService

    def self.create(file, &block)
      new(file).tempfile(&block)
    end

    attr_reader :source_file

    def initialize(source_file)
      @source_file = source_file
    end

    def tempfile(&block)
      if source_file.respond_to? :to_tempfile
        source_file.send(:to_tempfile, &block)
      elsif source_file.has_content?
        default_tempfile(&block)
      end
    end

    def default_tempfile(&block)
      Tempfile.open(filename_for_characterization) do |f|
        f.binmode
        if source_file.content.respond_to? :read
          f.write(source_file.content.read)
        else
          f.write(source_file.content)
        end
        source_file.content.rewind if source_file.content.respond_to? :rewind
        f.rewind
        yield(f)
      end
    end

    def filename_for_characterization
      registered_mime_type = MIME::Types[source_file.mime_type].first
      Logger.warn "Unable to find a registered mime type for #{source_file.mime_type.inspect} on #{source_file.uri}" unless registered_mime_type
      extension = registered_mime_type ? ".#{registered_mime_type.extensions.first}" : ''
      version_id = 1 # TODO fixme
      m = /\/([^\/]*)$/.match(source_file.uri)
      ["#{m[1]}-#{version_id}", "#{extension}"]
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
hydra-derivatives-3.1.3 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-3.1.2 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-3.1.1 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-1.2.1 lib/hydra/derivatives/tempfile_service.rb
hydra-derivatives-3.1.0 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-3.0.2 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-3.0.1 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-3.0.0 lib/hydra/derivatives/services/tempfile_service.rb
hydra-derivatives-2.0.0 lib/hydra/derivatives/services/tempfile_service.rb