Class: Atech::ObjectStore::File
- Inherits:
-
Object
- Object
- Atech::ObjectStore::File
- Defined in:
- lib/atech/object_store/file.rb
Defined Under Namespace
Classes: CannotEditFrozenFile, FileDataTooBig, FileNotFound, ValidationError
Class Method Summary collapse
-
.add_file(filename, data = '', options = {}) ⇒ Object
Inserts a new File into the database.
-
.add_local_file(path) ⇒ Object
Imports a new file by passing a path and returning a new File object once it has been added to the database.
-
.execute_query(query) ⇒ Object
Run a query on the backend database, attempt to run the query up to 3 times.
-
.find_by_id(id) ⇒ Object
Returns a new file object for the given ID.
Instance Method Summary collapse
-
#append(data) ⇒ Object
Appends data to the end of the current blob and updates the size and update time as appropriate.
-
#blob ⇒ Object
Returns the blob data.
-
#copy(path) ⇒ Object
Downloads the current file to a path on your local server.
-
#created_at ⇒ Object
Returns the date the file was created.
-
#delete ⇒ Object
Removes the file from the database.
-
#frozen? ⇒ Boolean
Returns whether this objec tis frozen or not.
-
#id ⇒ Object
Returns the ID of the file.
-
#initialize(attributes) ⇒ File
constructor
Initialises a new File object with the hash of attributes from a MySQL query ensuring that all symbols are strings.
-
#inspect ⇒ Object
Returns details about the file.
-
#name ⇒ Object
Returns the name of the file.
-
#overwrite(data) ⇒ Object
Overwrites any data which is stored in the file.
-
#reload(include_blob = false) ⇒ Object
Reload properties from the database.
-
#rename(name) ⇒ Object
Changes the name for a file.
-
#size ⇒ Object
Returns the size of the file as an integer.
-
#updated_at ⇒ Object
Returns the date the file was last updated.
Constructor Details
#initialize(attributes) ⇒ File
Initialises a new File object with the hash of attributes from a MySQL query ensuring that all symbols are strings
85 86 87 |
# File 'lib/atech/object_store/file.rb', line 85 def initialize(attributes) @attributes = parsed_attributes(attributes) end |
Class Method Details
.add_file(filename, data = '', options = {}) ⇒ Object
Inserts a new File into the database. Returns a new object if successfully inserted or raises an error. Filename & data must be provided, options options will be added automatically unless specified.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/atech/object_store/file.rb', line 54 def self.add_file(filename, data = '', = {}) if data.bytesize > Atech::ObjectStore.maximum_file_size raise FileDataTooBig, "Data provided was #{data.bytesize} and the maximum size is #{Atech::ObjectStore.maximum_file_size}" end ## Create a hash of properties to be for this class [:name] = filename [:size] ||= data.bytesize [:blob] = data [:created_at] ||= Time.now [:updated_at] ||= Time.now ## Ensure that new files have a filename & data raise ValidationError, "A 'name' must be provided to add a new file" if [:name].nil? ## Encode timestamps [:created_at] = [:created_at].utc [:updated_at] = [:updated_at].utc ##Create an insert query columns = .keys.join('`,`') data = .values.map { |data| escape_and_quote(data) }.join(',') File.execute_query("INSERT INTO files (`#{columns}`) VALUES (#{data})") ## Return a new File object self.new(.merge({:id => ObjectStore.backend.last_id})) end |
.add_local_file(path) ⇒ Object
Imports a new file by passing a path and returning a new File object once it has been added to the database. If the file does not exist a ValidationError will be raised.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/atech/object_store/file.rb', line 41 def self.add_local_file(path) if ::File.exist?(path) file_stat = ::File.stat(path) file_data = ::File.read(path) file_name = path.split('/').last add_file(file_name, file_data, :created_at => file_stat.ctime, :updated_at => file_stat.mtime) else raise ValidationError, "File does not exist at '#{path}' to add" end end |
.execute_query(query) ⇒ Object
Run a query on the backend database, attempt to run the query up to 3 times
18 19 20 21 22 23 24 |
# File 'lib/atech/object_store/file.rb', line 18 def self.execute_query(query) tries ||= 3 ObjectStore.backend.query(query) rescue Mysql2::Error => e retry unless (tries -= 1).zero? raise end |
.find_by_id(id) ⇒ Object
Returns a new file object for the given ID. If no file is found a FileNotFound exception will be raised otherwise the File object will be returned.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/atech/object_store/file.rb', line 28 def self.find_by_id(id) result = File.execute_query("SELECT * FROM files WHERE id = #{id.to_i}") result = result.first if result if result self.new(result) else raise(FileNotFound, "File not found with id '#{id.to_i}'") end end |
Instance Method Details
#append(data) ⇒ Object
Appends data to the end of the current blob and updates the size and update time as appropriate.
136 137 138 139 140 |
# File 'lib/atech/object_store/file.rb', line 136 def append(data) raise CannotEditFrozenFile, "This file has been frozen and cannot be appended to" if frozen? File.execute_query("UPDATE files SET `blob` = CONCAT(`blob`, #{self.class.escape_and_quote(data)}), `size` = `size` + #{data.bytesize}, `updated_at` = '#{self.class.time_now}' WHERE id = #{@attributes['id']}") reload(true) end |
#blob ⇒ Object
Returns the blob data
120 121 122 |
# File 'lib/atech/object_store/file.rb', line 120 def blob @attributes['blob'] end |
#copy(path) ⇒ Object
Downloads the current file to a path on your local server. If a file already exists at the path entered, it will be overriden.
131 132 133 |
# File 'lib/atech/object_store/file.rb', line 131 def copy(path) ::File.open(path, 'w') { |f| f.write(blob) } end |
#created_at ⇒ Object
Returns the date the file was created
110 111 112 |
# File 'lib/atech/object_store/file.rb', line 110 def created_at @attributes['created_at'] end |
#delete ⇒ Object
Removes the file from the database
158 159 160 161 162 |
# File 'lib/atech/object_store/file.rb', line 158 def delete raise CannotEditFrozenFile, "This file has been frozen and cannot be deleted" if frozen? File.execute_query("DELETE FROM files WHERE id = #{@attributes['id']}") @frozen = true end |
#frozen? ⇒ Boolean
Returns whether this objec tis frozen or not
125 126 127 |
# File 'lib/atech/object_store/file.rb', line 125 def frozen? !!@frozen end |
#id ⇒ Object
Returns the ID of the file
95 96 97 |
# File 'lib/atech/object_store/file.rb', line 95 def id @attributes['id'] end |
#inspect ⇒ Object
Returns details about the file
90 91 92 |
# File 'lib/atech/object_store/file.rb', line 90 def inspect "#<Atech::ObjectStore::File[#{id}] name=#{name}>" end |
#name ⇒ Object
Returns the name of the file
100 101 102 |
# File 'lib/atech/object_store/file.rb', line 100 def name @attributes['name'] end |
#overwrite(data) ⇒ Object
Overwrites any data which is stored in the file
143 144 145 146 147 148 |
# File 'lib/atech/object_store/file.rb', line 143 def overwrite(data) raise CannotEditFrozenFile, "This file has been frozen and cannot be overwriten" if frozen? File.execute_query("UPDATE files SET `blob` = #{self.class.escape_and_quote(data)}, `size` = #{data.bytesize}, `updated_at` = '#{self.class.time_now}' WHERE id = #{@attributes['id']}") @attributes['blob'] = data reload end |
#reload(include_blob = false) ⇒ Object
Reload properties from the database. Optionally, pass true to include the blob in the update
166 167 168 169 |
# File 'lib/atech/object_store/file.rb', line 166 def reload(include_blob = false) query = File.execute_query("SELECT #{include_blob ? '*' : '`id`, `name`, `size`, `created_at`, `updated_at`'} FROM files WHERE id = #{@attributes['id']}").first @attributes.merge!(parsed_attributes(query)) end |
#rename(name) ⇒ Object
Changes the name for a file
151 152 153 154 155 |
# File 'lib/atech/object_store/file.rb', line 151 def rename(name) raise CannotEditFrozenFile, "This file has been frozen and cannot be renamed" if frozen? File.execute_query("UPDATE files SET `name` = #{self.class.escape_and_quote(name)}, `updated_at` = '#{self.class.time_now}' WHERE id = #{@attributes['id']}") reload end |
#size ⇒ Object
Returns the size of the file as an integer
105 106 107 |
# File 'lib/atech/object_store/file.rb', line 105 def size @attributes['size'].to_i end |
#updated_at ⇒ Object
Returns the date the file was last updated
115 116 117 |
# File 'lib/atech/object_store/file.rb', line 115 def updated_at @attributes['updated_at'] end |