Class: Riiif::File

Inherits:
Object
  • Object
show all
Defined in:
app/models/riiif/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input_path, tempfile = nil) ⇒ File

Returns a new instance of File.

Parameters:

  • input_path (String)

    The location of an image file



9
10
11
12
# File 'app/models/riiif/file.rb', line 9

def initialize(input_path, tempfile = nil)
  @path = input_path
  @tempfile = tempfile # ensures that the tempfile will stick around until this file is garbage collected.
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'app/models/riiif/file.rb', line 3

def path
  @path
end

Class Method Details

.create(ext = nil, _validate = true, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/riiif/file.rb', line 22

def self.create(ext = nil, _validate = true, &block)

  tempfile = Tempfile.new(['mini_magick', ext.to_s.downcase])
  tempfile.binmode
  block.call(tempfile)
  tempfile.close
  image = new(tempfile.path, tempfile)
ensure
  tempfile.close if tempfile

end

.read(stream, ext) ⇒ Object



14
15
16
17
18
19
20
# File 'app/models/riiif/file.rb', line 14

def self.read(stream, ext)
  create(ext) do |f|
    while chunk = stream.read(8192)
      f.write(chunk)
    end
  end
end

Instance Method Details

#extract(transformation) ⇒ Object

Parameters:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/riiif/file.rb', line 35

def extract(transformation)
  command = 'convert'
  command << " -crop #{transformation.crop}" if transformation.crop
  command << " -resize #{transformation.size}" if transformation.size
  if transformation.rotation
    command << " -virtual-pixel white +distort srt #{transformation.rotation}"
  end

  case transformation.quality
  when 'grey'
    command << ' -colorspace Gray'
  when 'bitonal'
    command << ' -colorspace Gray'
    command << ' -type Bilevel'
  end
  command << " #{path} #{transformation.format}:-"
  execute(command)
end

#infoObject



54
55
56
# File 'app/models/riiif/file.rb', line 54

def info
  @info ||= info_extractor_class.new(path).extract
end