Class: Miasma::Models::Storage::Local
- Inherits:
-
Miasma::Models::Storage
- Object
- Miasma::Models::Storage
- Miasma::Models::Storage::Local
- Includes:
- Contrib::LocalApiCore::ApiCommon
- Defined in:
- lib/miasma/contrib/local/storage.rb
Instance Method Summary collapse
-
#bucket_all ⇒ Array<Models::Storage::Bucket>
Return all buckets.
-
#bucket_destroy(bucket) ⇒ TrueClass, FalseClass
Destroy bucket.
-
#bucket_path(bucket) ⇒ String
Escaped bucket name.
-
#bucket_reload(bucket) ⇒ Models::Storage::Bucket
Reload the bucket.
-
#bucket_save(bucket) ⇒ Models::Storage::Bucket
Save bucket.
-
#file_all(bucket) ⇒ Array<File>
Return all files within bucket.
-
#file_body(file) ⇒ IO, HTTP::Response::Body
Fetch the contents of the file.
-
#file_destroy(file) ⇒ TrueClass, FalseClass
Destroy file.
-
#file_filter(bucket, args) ⇒ Array<Models::Storage::File>
Return filtered files.
-
#file_path(file) ⇒ String
Escaped file path.
-
#file_reload(file) ⇒ Models::Storage::File
Reload the file.
-
#file_save(file) ⇒ Models::Storage::File
Save file.
-
#file_url(file, timeout_secs) ⇒ String
Create publicly accessible URL.
-
#full_path(file_or_bucket) ⇒ String
Provide full path for object.
-
#initialize(args = {}) ⇒ self
constructor
Create new instance.
-
#uri_escape(string) ⇒ String
URL string escape.
-
#uri_unescape(string) ⇒ String
Un-escape URL escaped string.
Methods included from Contrib::LocalApiCore::ApiCommon
Constructor Details
#initialize(args = {}) ⇒ self
Create new instance
17 18 19 20 21 22 |
# File 'lib/miasma/contrib/local/storage.rb', line 17 def initialize(args={}) super unless(::File.directory?(object_store_root)) FileUtils.mkdir_p(object_store_root) end end |
Instance Method Details
#bucket_all ⇒ Array<Models::Storage::Bucket>
Return all buckets
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/miasma/contrib/local/storage.rb', line 69 def bucket_all Dir.new(object_store_root).map do |item| if(::File.directory?(::File.join(object_store_root, item)) && !item.start_with?('.')) Bucket.new( self, :id => ::File.basename(uri_unescape(item)), :name => ::File.basename(uri_unescape(item)) ).valid_state end end.compact end |
#bucket_destroy(bucket) ⇒ TrueClass, FalseClass
Destroy bucket
41 42 43 44 45 46 47 48 |
# File 'lib/miasma/contrib/local/storage.rb', line 41 def bucket_destroy(bucket) if(bucket.persisted?) FileUtils.rmdir(full_path(bucket)) true else false end end |
#bucket_path(bucket) ⇒ String
Returns escaped bucket name.
213 214 215 |
# File 'lib/miasma/contrib/local/storage.rb', line 213 def bucket_path(bucket) ::File.join(object_store_root, uri_escape(bucket.name)) end |
#bucket_reload(bucket) ⇒ Models::Storage::Bucket
Reload the bucket
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/miasma/contrib/local/storage.rb', line 54 def bucket_reload(bucket) if(bucket.persisted?) unless(::File.directory?(full_path(bucket))) bucket.data.clear bucket.dirty.clear else bucket.valid_state end end bucket end |
#bucket_save(bucket) ⇒ Models::Storage::Bucket
Save bucket
28 29 30 31 32 33 34 35 |
# File 'lib/miasma/contrib/local/storage.rb', line 28 def bucket_save(bucket) unless(bucket.persisted?) FileUtils.mkdir_p(full_path(bucket)) bucket.id = bucket.name bucket.valid_state end bucket end |
#file_all(bucket) ⇒ Array<File>
pagination auto-follow
Return all files within bucket
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/miasma/contrib/local/storage.rb', line 106 def file_all(bucket) Dir.glob(::File.join(full_path(bucket), '*')).map do |item| if(::File.file?(item) && !item.start_with?('.')) item_name = item.sub("#{full_path(bucket)}/", '') item_name = uri_unescape(item_name) File.new( bucket, :id => ::File.join(bucket.name, item_name), :name => item_name, :updated => ::File.mtime(item), :size => ::File.size(item) ).valid_state end end.compact end |
#file_body(file) ⇒ IO, HTTP::Response::Body
Fetch the contents of the file
200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/miasma/contrib/local/storage.rb', line 200 def file_body(file) if(file.persisted?) tmp_file = Tempfile.new('miasma') tmp_path = tmp_file.path tmp_file.delete FileUtils.cp(full_path(file), tmp_path) File::Streamable.new(::File.open(tmp_path, 'rb')) else StringIO.new('') end end |
#file_destroy(file) ⇒ TrueClass, FalseClass
Destroy file
152 153 154 155 156 157 158 159 |
# File 'lib/miasma/contrib/local/storage.rb', line 152 def file_destroy(file) if(file.persisted?) FileUtils.rm(full_path(file)) true else false end end |
#file_filter(bucket, args) ⇒ Array<Models::Storage::File>
Return filtered files
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/miasma/contrib/local/storage.rb', line 85 def file_filter(bucket, args) Dir.glob(::File.join(full_path(bucket), uri_escape(args[:prefix]), '*')).map do |item| if(::File.file?(item) && !item.start_with?('.')) item_name = item.sub("#{full_path(bucket)}/", '') item_name = uri_unescape(item_name) File.new( bucket, :id => ::File.join(bucket.name, item_name), :name => item_name, :updated => File.mtime(item), :size => File.size(item) ).valid_state end end.compact end |
#file_path(file) ⇒ String
Returns escaped file path.
218 219 220 221 222 |
# File 'lib/miasma/contrib/local/storage.rb', line 218 def file_path(file) file.name.split('/').map do |part| uri_escape(part) end.join('/') end |
#file_reload(file) ⇒ Models::Storage::File
Reload the file
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/miasma/contrib/local/storage.rb', line 165 def file_reload(file) if(file.persisted?) new_info = Smash.new.tap do |data| data[:updated] = ::File.mtime(full_path(file)) data[:size] = ::File.size(full_path(file)) data[:etag] = Digest::MD5.hexdigest(::File.read(full_path(file))) # @todo Update for block building mime = MIME::Types.of(full_path(file)).first if(mime) data[:content_type] = mime.content_type end end file.load_data(file.attributes.deep_merge(new_info)) file.valid_state end file end |
#file_save(file) ⇒ Models::Storage::File
Save file
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/miasma/contrib/local/storage.rb', line 126 def file_save(file) if(file.dirty?) file.load_data(file.attributes) if(file.attributes[:body].respond_to?(:readpartial)) file.body.rewind tmp_file = Tempfile.new('miasma') begin while(content = file.body.read(Storage::READ_BODY_CHUNK_SIZE)) tmp_file.write content end rescue EOFError end tmp_file.flush tmp_file.close FileUtils.mv(tmp_file.path, full_path(file)) end file.id = ::File.join(file.bucket.name, file.name) file.reload end file end |
#file_url(file, timeout_secs) ⇒ String
where is this in swift?
Create publicly accessible URL
187 188 189 190 191 192 193 194 |
# File 'lib/miasma/contrib/local/storage.rb', line 187 def file_url(file, timeout_secs) if(file.persisted?) "file://#{full_path(file)}" full_path(file) else raise Error::ModelPersistError.new "#{file} has not been saved!" end end |
#full_path(file_or_bucket) ⇒ String
Provide full path for object
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/miasma/contrib/local/storage.rb', line 228 def full_path(file_or_bucket) if(file_or_bucket.is_a?(Bucket)) bucket_path(file_or_bucket) else ::File.join( bucket_path(file_or_bucket.bucket), file_path(file_or_bucket) ) end end |
#uri_escape(string) ⇒ String
move this to common module
URL string escape
244 245 246 247 248 |
# File 'lib/miasma/contrib/local/storage.rb', line 244 def uri_escape(string) string.to_s.gsub(/([^a-zA-Z0-9_.\-~])/) do '%' << $1.unpack('H2' * $1.bytesize).join('%').upcase end end |
#uri_unescape(string) ⇒ String
Un-escape URL escaped string
254 255 256 257 258 |
# File 'lib/miasma/contrib/local/storage.rb', line 254 def uri_unescape(string) string.to_s.gsub(/%([^%]{2})/) do [$1].pack('H2') end end |