Class: Progstr::Filer::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/filer/attachment.rb

Direct Known Subclasses

EmptyAttachment

Defined Under Namespace

Classes: EmptyAttachment

Constant Summary collapse

@@id_generator =
::UUID.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



22
23
24
# File 'lib/filer/attachment.rb', line 22

def attribute
  @attribute
end

#fileObject

Returns the value of attribute file.



22
23
24
# File 'lib/filer/attachment.rb', line 22

def file
  @file
end

#idObject

Returns the value of attribute id.



22
23
24
# File 'lib/filer/attachment.rb', line 22

def id
  @id
end

#pre_validatedObject

Returns the value of attribute pre_validated.



22
23
24
# File 'lib/filer/attachment.rb', line 22

def pre_validated
  @pre_validated
end

#uploader_classObject

Returns the value of attribute uploader_class.



22
23
24
# File 'lib/filer/attachment.rb', line 22

def uploader_class
  @uploader_class
end

Class Method Details

.empty(uploader_class) ⇒ Object



50
51
52
53
54
# File 'lib/filer/attachment.rb', line 50

def self.empty(uploader_class)
  result = EmptyAttachment.new
  result.uploader_class = uploader_class
  result
end

.file_id(file) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/filer/attachment.rb', line 74

def self.file_id(file)
  if file.respond_to?(:id)
    file.id
  else
    generate_id
  end
end

.from_file(uploader_class, attribute, file) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/filer/attachment.rb', line 63

def self.from_file(uploader_class, attribute, file)
  result = Attachment.new
  result.id = file_id(file)
  result.attribute = attribute
  result.uploader_class = uploader_class

  result.file = file
  result.pre_validated = false
  result
end

.from_id(uploader_class, attribute, id) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'lib/filer/attachment.rb', line 101

def self.from_id(uploader_class, attribute, id)
  result = Attachment.new
  result.id = id
  result.attribute = attribute
  result.uploader_class = uploader_class

  result.pre_validated = true
  result
end

.from_json(uploader_class, attribute, json) ⇒ Object



56
57
58
59
60
61
# File 'lib/filer/attachment.rb', line 56

def self.from_json(uploader_class, attribute, json)
  file = FileLike.new(json)
  result = from_file(uploader_class, attribute, file)
  result.pre_validated = true
  return result
end

.generate_idObject



82
83
84
85
# File 'lib/filer/attachment.rb', line 82

def self.generate_id
  uuid = @@id_generator.generate
  uuid.gsub("-", "")
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/filer/attachment.rb', line 111

def blank?
  false
end

#display_hashObject



136
137
138
139
140
141
142
# File 'lib/filer/attachment.rb', line 136

def display_hash
  {
    "name" => path,
    "size" => size,
    "id" => id
  }
end

#display_jsonObject



132
133
134
# File 'lib/filer/attachment.rb', line 132

def display_json
  MultiJson.encode(display_hash)
end

#extensionObject



96
97
98
99
# File 'lib/filer/attachment.rb', line 96

def extension
  from_file = File.extname(path) || ""
  from_file.sub(".", "")
end

#mark_uploaded!Object



147
148
149
# File 'lib/filer/attachment.rb', line 147

def mark_uploaded!
  @uploaded = true
end

#need_upload?Boolean

Returns:

  • (Boolean)


151
152
153
154
155
156
157
158
159
160
161
# File 'lib/filer/attachment.rb', line 151

def need_upload?
  if uploaded?
    false
  elsif file.nil?
    false
  elsif file.kind_of?(FileLike)
    false
  else
    true
  end
end

#pathObject



91
92
93
94
# File 'lib/filer/attachment.rb', line 91

def path
  (file.original_filename if file.respond_to?(:original_filename)) ||
    (file_path = file.path if file.respond_to?(:path))
end

#public_urlObject



124
125
126
127
128
129
130
# File 'lib/filer/attachment.rb', line 124

def public_url
  if !blank?
    "#{Progstr::Filer.url_prefix}files/data/#{Progstr::Filer.access_key}/#{id}"
  else
    ""
  end
end

#sizeObject



87
88
89
# File 'lib/filer/attachment.rb', line 87

def size
  file.size
end

#uploaded?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'lib/filer/attachment.rb', line 144

def uploaded?
  @uploaded == true
end

#urlObject



115
116
117
118
119
120
121
122
# File 'lib/filer/attachment.rb', line 115

def url
  if !blank?
    token = Progstr::Filer.generate_download_auth_token(id)
    "#{Progstr::Filer.url_prefix}files/data/#{Progstr::Filer.access_key}/#{id}?auth=#{token}"
  else
    ""
  end
end