Class: StorageRoom::File

Inherits:
Embedded show all
Defined in:
lib/storage_room/embeddeds/file.rb

Overview

Any file

Direct Known Subclasses

Image

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accessors

#[], #as_json, #attributes, #attributes=, #eql?, #hash, #initialize, #inspect, #loaded?, #proxy?, #reset!, #response_data, #response_data=, #set_from_response_data, #to_hash

Class Method Details

.new_with_filename(path) ⇒ Object

Create a new File and set attributes from local file



14
15
16
# File 'lib/storage_room/embeddeds/file.rb', line 14

def new_with_filename(path)
  new.tap{|f| f.set_with_filename(path)}
end

Instance Method Details

#download_to_directory(path) ⇒ Object

Download the file to the local disk



34
35
36
37
38
# File 'lib/storage_room/embeddeds/file.rb', line 34

def download_to_directory(path)
  Dir.mkdir(path) unless ::File.directory?(path)
  download_file(self[:@url], ::File.join(path, local_filename))
  true
end

#file_typeObject

Returns the file type based on the extension



29
30
31
# File 'lib/storage_room/embeddeds/file.rb', line 29

def file_type
  self[:@url] ? ::File.extname(self[:@url])[1..-1] : nil
end

#local_filenameObject

The localized filename built out of the URL



41
42
43
# File 'lib/storage_room/embeddeds/file.rb', line 41

def local_filename
  self[:@url] ? localize_filename(self[:@url]) : nil
end

#set_with_filename(path) ⇒ Object

Sets the filename, content_type and data attributes from a local filename so that a File can be uploaded through the API



20
21
22
23
24
25
26
# File 'lib/storage_room/embeddeds/file.rb', line 20

def set_with_filename(path)
  return if path.blank?
  
  self.filename = ::File.basename(path)
  self.content_type = ::MIME::Types.type_for(path).first.content_type
  self.data = ::Base64.encode64(::File.read(path))
end