1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
require "active_file/site"
# Schema: id, key, filename, content_type, metadata, byte_size, checksum, created_at
class ActiveFile::Blob < ActiveRecord::Base
self.table_name = "active_file_blobs"
has_secure_token :key
store :metadata, coder: JSON
class_attribute :verifier, default: -> { Rails.application.message_verifier('ActiveFile') }
class_attribute :site
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 = content_type # Marcel::MimeType.for(data, name: name, declared_type: content_type)
blob.upload 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
# 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
Filename.new(filename)
end
def upload(data)
site.upload key, data
end
def download
site.download key
end
def delete
site.delete key
end
def purge
delete
destroy
end
def purge_later
ActiveFile::PurgeJob.perform_later(self)
end
end
|