Module: Media

Defined in:
lib/media/temp_file.rb,
lib/media.rb,
lib/media/version.rb,
lib/media/mime_type.rb,
lib/media/transmogrifier.rb,
lib/media/transmogrifiers/inkscape.rb,
lib/media/transmogrifiers/libremagick.rb,
lib/media/transmogrifiers/libreoffice.rb,
lib/media/transmogrifiers/graphicsmagick.rb

Overview

media processing requires a different type of tempfile… because we use command line tools to process our temp files, these files can’t be open and closed by ruby.

instead, Media::TempFile is used to generate closed files from binary data (for files to be fed to command line tools), or to generate empty tmp files (for output filenames to be fed to command line tools).

We use the Tempfile class for generating these files, but then we always close them right away. By doing this, we ensure that the temp file will eventually get removed when the Tempfile gets garbage collected.

Defined Under Namespace

Modules: MimeType Classes: GraphicsMagickTransmogrifier, InkscapeTransmogrifier, LibreMagickTransmogrifier, LibreOfficeTransmogrifier, TempFile, Transmogrifier

Constant Summary collapse

VERSION =
"0.5.0"
INKSCAPE_COMMAND =
`which inkscape`.chomp
LIBREOFFICE_COMMAND =
false
GRAPHICSMAGICK_COMMAND =
`which gm`.chomp
GRAPHICSMAGICK_VERSION =
[version[0].to_i, version[1].to_i, version[2].to_i]

Class Method Summary collapse

Class Method Details

.dimensions(filepath) ⇒ Object



56
57
58
59
# File 'lib/media.rb', line 56

def self.dimensions(filepath)
  return unless Media::GraphicsMagickTransmogrifier.available?
  Media::GraphicsMagickTransmogrifier.dimensions filepath
end

.has_dimensions?(mime_type) ⇒ Boolean

special graphicsmagick hooks. we use graphicsmagick in order to parse the dimensions of image files.

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/media.rb', line 51

def self.has_dimensions?(mime_type)
  Media::GraphicsMagickTransmogrifier.available? &&
    Media::GraphicsMagickTransmogrifier.converts_from?(mime_type)
end

.may_produce?(src_type, dst_type) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/media.rb', line 42

def self.may_produce?(src_type, dst_type)
  Transmogrifier.find_class(src_type, dst_type)
end

.transmogrifier(options) ⇒ Object

creates a new instance of transmogrifier suitable for turning input into output.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/media.rb', line 15

def self.transmogrifier(options)
  if options[:input_type]
    input_type = Media::MimeType.simple options[:input_type]
  elsif options[:input_file]
    input_type = Media::MimeType.mime_type_from_extension options[:input_file]
  else
    raise ArgumentError.new
  end

  if options[:output_type]
    output_type = Media::MimeType.simple options[:output_type]
  elsif options[:output_file]
    output_type = Media::MimeType.mime_type_from_extension options[:output_file]
  else
    raise ArgumentError.new
  end

  unless input_type and output_type
    raise ArgumentError.new("Both input and output types are required (given %s -> %s)." % [input_type||'nil', output_type||'nil'])
  end

  transmog_class = Transmogrifier.find_class(input_type, output_type)
  if transmog_class
    transmog_class.new(options)
  end
end