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 single query on a backend connection.
-
.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
90 91 92 |
# File 'lib/atech/object_store/file.rb', line 90 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.
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 82 83 84 85 86 |
# File 'lib/atech/object_store/file.rb', line 57 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 last_insert_id = ObjectStore::Connection.client do |client| columns = .keys.join('`,`') data = .values.map { |v| escape_and_quote(v) }.join(',') client.query("INSERT INTO files (`#{columns}`) VALUES (#{data})") client.last_id end ## Return a new File object self.new(.merge(:id => last_insert_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.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/atech/object_store/file.rb', line 44 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 single query on a backend connection. This should only be used when running a single query. If you need to do multiple things on the same connection (e.g. INSERT and then get LAST_INSERT_ID) you should checkot your own connection using ObejctStore::Connection.client
19 20 21 22 23 24 25 26 27 |
# File 'lib/atech/object_store/file.rb', line 19 def self.execute_query(query) tries ||= 3 ObjectStore::Connection.client do |client| client.query(query) end 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.
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/atech/object_store/file.rb', line 31 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.
141 142 143 144 145 |
# File 'lib/atech/object_store/file.rb', line 141 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
125 126 127 |
# File 'lib/atech/object_store/file.rb', line 125 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.
136 137 138 |
# File 'lib/atech/object_store/file.rb', line 136 def copy(path) ::File.open(path, 'w') { |f| f.write(blob) } end |
#created_at ⇒ Object
Returns the date the file was created
115 116 117 |
# File 'lib/atech/object_store/file.rb', line 115 def created_at @attributes['created_at'] end |
#delete ⇒ Object
Removes the file from the database
163 164 165 166 167 |
# File 'lib/atech/object_store/file.rb', line 163 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
130 131 132 |
# File 'lib/atech/object_store/file.rb', line 130 def frozen? !!@frozen end |
#id ⇒ Object
Returns the ID of the file
100 101 102 |
# File 'lib/atech/object_store/file.rb', line 100 def id @attributes['id'] end |
#inspect ⇒ Object
Returns details about the file
95 96 97 |
# File 'lib/atech/object_store/file.rb', line 95 def inspect "#<Atech::ObjectStore::File[#{id}] name=#{name}>" end |
#name ⇒ Object
Returns the name of the file
105 106 107 |
# File 'lib/atech/object_store/file.rb', line 105 def name @attributes['name'] end |
#overwrite(data) ⇒ Object
Overwrites any data which is stored in the file
148 149 150 151 152 153 |
# File 'lib/atech/object_store/file.rb', line 148 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
171 172 173 174 |
# File 'lib/atech/object_store/file.rb', line 171 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
156 157 158 159 160 |
# File 'lib/atech/object_store/file.rb', line 156 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
110 111 112 |
# File 'lib/atech/object_store/file.rb', line 110 def size @attributes['size'].to_i end |
#updated_at ⇒ Object
Returns the date the file was last updated
120 121 122 |
# File 'lib/atech/object_store/file.rb', line 120 def updated_at @attributes['updated_at'] end |