Class: Aploader::TmpFile
- Inherits:
-
Object
- Object
- Aploader::TmpFile
- Defined in:
- lib/aploader/tmp_file.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #file ⇒ Object
-
#flush! ⇒ Object
If a file is created, it must be deleted to empty space.
- #info ⇒ Object
-
#initialize(filename, type = nil) ⇒ TmpFile
constructor
A new instance of TmpFile.
- #process!(url_or_file, options = {}) ⇒ Object
Constructor Details
#initialize(filename, type = nil) ⇒ TmpFile
Returns a new instance of TmpFile.
4 5 6 7 8 |
# File 'lib/aploader/tmp_file.rb', line 4 def initialize(filename, type=nil) @type = type @filename = filename @path = Aploader.generate_path(@filename) end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
2 3 4 |
# File 'lib/aploader/tmp_file.rb', line 2 def filename @filename end |
#type ⇒ Object
Returns the value of attribute type.
2 3 4 |
# File 'lib/aploader/tmp_file.rb', line 2 def type @type end |
Instance Method Details
#file ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/aploader/tmp_file.rb', line 64 def file begin return File.open(@path) rescue => e return nil end end |
#flush! ⇒ Object
If a file is created, it must be deleted to empty space
57 58 59 60 61 62 |
# File 'lib/aploader/tmp_file.rb', line 57 def flush! begin File.delete(@path) rescue => e end end |
#info ⇒ Object
72 73 74 |
# File 'lib/aploader/tmp_file.rb', line 72 def info "Filename: #{@filename}\nType: #{@type}\nPath: #{@path}\nExternal URL: #{@url}" end |
#process!(url_or_file, options = {}) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 |
# File 'lib/aploader/tmp_file.rb', line 10 def process!(url_or_file, ={}) # Defining default options and merging received options = { decode: true }.merge(.symbolize_keys).symbolize_keys if url_or_file.empty? || url_or_file.nil? return nil else @filename ||= SecureRandom.urlsafe_base64(16) @path = Aploader.generate_path(@filename) if url_or_file =~ URI::regexp @type = :url @url = url_or_file uri = URI.parse(url_or_file) response = Net::HTTP.get_response(uri) if [:decode] == true payload = Base64.decode64(response.body) else payload = response.body end else @type = :file if [:decode] == true payload = Base64.decode64(url_or_file) else payload = url_or_file end end begin Dir.mkdir(Aploader::TMP_DIR) rescue => e end begin File.open(@path, 'wb'){|f| f.write(payload) } rescue => e return nil end return true end end |