diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2017-07-05 13:06:29 +0200 |
commit | 571509ad12bf3bcb3190efd7494a38c4796302b8 (patch) | |
tree | 083bcf7f1a56e6856c486de7accef2d3778826e7 /lib/active_vault/blob.rb | |
parent | e635dac88f0dfcc36a2313c10f860cb6e3a52cfa (diff) | |
download | rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.gz rails-571509ad12bf3bcb3190efd7494a38c4796302b8.tar.bz2 rails-571509ad12bf3bcb3190efd7494a38c4796302b8.zip |
Rename from ActiveFile to ActiveVault since activefile gem name was taken
Diffstat (limited to 'lib/active_vault/blob.rb')
-rw-r--r-- | lib/active_vault/blob.rb | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/active_vault/blob.rb b/lib/active_vault/blob.rb new file mode 100644 index 0000000000..4948d43ec7 --- /dev/null +++ b/lib/active_vault/blob.rb @@ -0,0 +1,66 @@ +require "active_vault/site" +require "active_vault/filename" + +# Schema: id, key, filename, content_type, metadata, byte_size, checksum, created_at +class ActiveVault::Blob < ActiveRecord::Base + self.table_name = "rails_blobs" + + has_secure_token :key + store :metadata, coder: JSON + + class_attribute :site + + class << self + def build_after_upload(io:, filename:, content_type: nil, metadata: nil) + new.tap do |blob| + blob.filename = filename + blob.content_type = content_type + + blob.upload io + end + end + + def create_after_upload!(io:, filename:, content_type: nil, metadata: nil) + build_after_upload(io: io, filename: filename, content_type: content_type, metadata: metadata).tap(&:save!) + end + end + + # We can't wait until the record is first saved to have a key for it + def key + self[:key] ||= self.class.generate_unique_secure_token + end + + def filename + ActiveVault::Filename.new(self[:filename]) + end + + def url(expires_in: 5.minutes, disposition: :inline) + site.url key, expires_in: expires_in, disposition: disposition, filename: filename + end + + + def upload(io) + site.upload(key, io) + + self.checksum = site.checksum(key) + self.byte_size = site.byte_size(key) + end + + def download + site.download key + end + + + def delete + site.delete key + end + + def purge + delete + destroy + end + + def purge_later + ActiveVault::PurgeJob.perform_later(self) + end +end |