Class: Filemaker::Model::Types::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/filemaker/model/types/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, klass) ⇒ Attachment

Returns a new instance of Attachment.



10
11
12
13
# File 'lib/filemaker/model/types/attachment.rb', line 10

def initialize(value, klass)
  @value = value
  @klass = klass
end

Instance Attribute Details

#_bodyObject (readonly)

Returns the value of attribute _body.



8
9
10
# File 'lib/filemaker/model/types/attachment.rb', line 8

def _body
  @_body
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



8
9
10
# File 'lib/filemaker/model/types/attachment.rb', line 8

def content_type
  @content_type
end

#extensionObject (readonly)

Returns the value of attribute extension.



8
9
10
# File 'lib/filemaker/model/types/attachment.rb', line 8

def extension
  @extension
end

#klassObject (readonly)

Returns the value of attribute klass.



8
9
10
# File 'lib/filemaker/model/types/attachment.rb', line 8

def klass
  @klass
end

Instance Method Details

#bodyObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/filemaker/model/types/attachment.rb', line 29

def body
  return @_body if defined?(@_body)

  valid_files = %w[
    .pdf
    .jpeg .jpg .png .gif .tif .tiff
    .doc .docx .xls .xlsx .ppt .pptx .pptm
    .txt
    .rar .zip .webarchive
    .htm .html
    .java
  ]

  @_body = download_protected_file

  if !file_extension.blank? &&
     valid_files.include?(file_extension.try(:downcase))

    @content_type = MimeMagic.by_extension(file_extension).try(:type)
    @extension = file_extension
  else
    mime_type = MimeMagic.by_magic(@_body)

    unless mime_type.blank?
      case mime_type.type.downcase
      when 'application/msword'
        @content_type = 'application/msword'
        @extension = '.doc'
      when 'application/pdf'
        @content_type = 'application/pdf'
        @extension = '.pdf'
      when 'application/x-ole-storage'
        @content_type = 'application/vnd.ms-excel'
        @extension = '.xls'
      when 'application/vnd.ms-excel'
        @content_type = 'application/vnd.ms-excel'
        @extension = '.xls'
      when 'image/jpeg'
        @content_type = 'image/jpeg'
        @extension = '.jpg'
      when 'image/png'
        @content_type = 'image/png'
        @extension = '.png'
      when 'application/vnd.oasis.opendocument.text'
        @content_type = 'application/vnd.oasis.opendocument.text'
        @extension = '.odt'
      else
        # No choice, we have to assign it somehow
        @content_type = mime_type.type
        @extension = MIME::Types[@content_type].first
                                               .try(:extensions)
                                               .try(:first)
        @extension = ".#{@extension}" if @extension
      end
    end
  end

  @_body
end

#file_extensionObject



19
20
21
22
# File 'lib/filemaker/model/types/attachment.rb', line 19

def file_extension
  # We need to use .path to eliminate query string
  File.extname(URI.parse(url).path)
end

#filenameObject



24
25
26
27
# File 'lib/filemaker/model/types/attachment.rb', line 24

def filename
  return if url.blank?
  File.basename(URI.parse(url).path)
end

#urlObject



15
16
17
# File 'lib/filemaker/model/types/attachment.rb', line 15

def url
  @value.to_s
end