Class: MountableFileServer::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/mountable_file_server/metadata.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size:, content_type:, width: nil, height: nil) ⇒ Metadata

Returns a new instance of Metadata.



7
8
9
10
11
12
# File 'lib/mountable_file_server/metadata.rb', line 7

def initialize(size:, content_type:, width: nil, height: nil)
  @size = size
  @content_type = content_type
  @width = width
  @height = height
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



5
6
7
# File 'lib/mountable_file_server/metadata.rb', line 5

def content_type
  @content_type
end

#heightObject (readonly)

Returns the value of attribute height.



5
6
7
# File 'lib/mountable_file_server/metadata.rb', line 5

def height
  @height
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'lib/mountable_file_server/metadata.rb', line 5

def size
  @size
end

#widthObject (readonly)

Returns the value of attribute width.



5
6
7
# File 'lib/mountable_file_server/metadata.rb', line 5

def width
  @width
end

Class Method Details

.for_path(path) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mountable_file_server/metadata.rb', line 30

def self.for_path(path)
  parameters = {}

  parameters[:content_type] = `file --brief --mime-type #{path}`.strip
  parameters[:size] = File.size(path)

  if ['image/png', 'image/jpeg', 'image/gif', 'image/tiff'].include?(parameters[:content_type])
    dimensions = Dimensions.dimensions(path)
    parameters[:width] = dimensions[0]
    parameters[:height] = dimensions[1]
  end

  new(**parameters)
end

Instance Method Details

#to_hObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/mountable_file_server/metadata.rb', line 14

def to_h
  hash = {
    size: size,
    content_type: content_type
  }

  if width && height
    hash = hash.merge({
      height: height,
      width: width
    })
  end

  hash
end