2017-06-30 18:04:19 -04:00
|
|
|
require "active_file/site"
|
|
|
|
|
2017-06-30 18:02:30 -04:00
|
|
|
# Schema: id, key, filename, content_type, metadata, byte_size, digest, created_at
|
2017-06-30 13:12:58 -04:00
|
|
|
class ActiveFile::Blob < ActiveRecord::Base
|
2017-06-30 18:02:50 -04:00
|
|
|
self.table_name = "active_file_blobs"
|
2017-06-30 13:12:58 -04:00
|
|
|
|
2017-06-30 18:02:30 -04:00
|
|
|
has_secure_token :key
|
2017-06-30 13:12:58 -04:00
|
|
|
store :metadata, coder: JSON
|
|
|
|
|
|
|
|
class_attribute :verifier, default: -> { Rails.application.message_verifier('ActiveFile') }
|
2017-06-30 18:04:19 -04:00
|
|
|
class_attribute :site
|
2017-06-30 13:12:58 -04:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def find_verified(signed_id)
|
|
|
|
find(verifier.verify(signed_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_after_upload(data:, filename:, content_type: nil, metadata: nil)
|
|
|
|
new.tap do |blob|
|
|
|
|
blob.filename = name
|
|
|
|
blob.content_type = Marcel::MimeType.for(data, name: name, declared_type: content_type)
|
|
|
|
blob.data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_after_upload!(data:, filename:, content_type: nil, metadata: nil)
|
|
|
|
build_after_upload(data: data, filename: filename, content_type: content_type, metadata: metadata).tap(&:save!)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def filename
|
|
|
|
Filename.new(filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete
|
2017-06-30 18:05:04 -04:00
|
|
|
site.delete(key)
|
2017-06-30 13:12:58 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def purge
|
|
|
|
delete
|
|
|
|
destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def purge_later
|
|
|
|
ActiveFile::PurgeJob.perform_later(self)
|
|
|
|
end
|
|
|
|
end
|