Class: Visor::Image::Store::FileSystem
- Inherits:
-
Object
- Object
- Visor::Image::Store::FileSystem
- Includes:
- Common::Exception
- Defined in:
- lib/image/store/file_system.rb
Overview
The FileSystem backend store.
This class handles the management of image files located in the local FileSystem, based on a URI like file:///path/to/my_image.format.
Constant Summary collapse
- CHUNKSIZE =
Size of the chunk to stream the files out.
65536
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#fp ⇒ Object
Returns the value of attribute fp.
-
#uri ⇒ Object
Returns the value of attribute uri.
Instance Method Summary collapse
-
#delete ⇒ Object
Deletes the image file to from its location.
-
#file_exists? ⇒ Boolean
Check if the image file exists.
-
#get ⇒ Object
Returns the image file to clients, streamed in chunks.
-
#initialize(uri, config) ⇒ Object
constructor
Initializes a new FileSystem store client object.
-
#save(id, tmp_file, format) ⇒ String, Integer
Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.
Constructor Details
#initialize(uri, config) ⇒ Object
Initializes a new FileSystem store client object.
28 29 30 31 32 |
# File 'lib/image/store/file_system.rb', line 28 def initialize(uri, config) @uri = URI(uri) @fp = @uri.path @config = config[:file] end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
18 19 20 |
# File 'lib/image/store/file_system.rb', line 18 def config @config end |
#fp ⇒ Object
Returns the value of attribute fp.
18 19 20 |
# File 'lib/image/store/file_system.rb', line 18 def fp @fp end |
#uri ⇒ Object
Returns the value of attribute uri.
18 19 20 |
# File 'lib/image/store/file_system.rb', line 18 def uri @uri end |
Instance Method Details
#delete ⇒ Object
Deletes the image file to from its location.
83 84 85 86 87 88 89 90 |
# File 'lib/image/store/file_system.rb', line 83 def delete file_exists? begin File.delete(fp) rescue => e raise Forbidden, "Error while trying to delete image file #{fp}: #{e.}" end end |
#file_exists? ⇒ Boolean
Check if the image file exists.
96 97 98 |
# File 'lib/image/store/file_system.rb', line 96 def file_exists? raise NotFound, "No image file found at #{fp}" unless File.exists?(fp) end |
#get ⇒ Object
Returns the image file to clients, streamed in chunks.
38 39 40 41 42 43 44 |
# File 'lib/image/store/file_system.rb', line 38 def get file_exists? open(fp, "rb") do |file| yield file.read(CHUNKSIZE) until file.eof? yield nil end end |
#save(id, tmp_file, format) ⇒ String, Integer
Saves the image file to the its final destination, based on the temporary file created by the server at data reception time.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/image/store/file_system.rb', line 57 def save(id, tmp_file, format) dir = File. config[:directory] file = "#{id}.#{format}" fp = File.join(dir, file) uri = "file://#{fp}" size = tmp_file.size FileUtils.mkpath(dir) unless Dir.exists?(dir) raise Duplicated, "The image file #{fp} already exists" if File.exists?(fp) STDERR.puts "Copying image tempfile #{tmp_file.path} to definitive #{fp}" tmp = File.open(tmp_file.path, "rb") new = File.open(fp, "wb") each_chunk(tmp, CHUNKSIZE) do |chunk| new << chunk end [uri, size] end |