Class: PopulateMe::GridFSAttachment
Instance Attribute Summary
Attributes inherited from Attachment
#document, #field
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Attachment
#attachee_prefix, #create, #create_variations, #delete, #delete_all, #ensure_local_path, #field_filename, #field_options, #field_value, inherited, #initialize, #location, #location_for_filename, set, #settings, #variations
Class Method Details
.ensure_db ⇒ Object
78
79
80
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 78
def ensure_db
raise MissingMongoDBError, "Attachment class #{self.name} does not have a Mongo database." if settings.db.nil?
end
|
.middleware ⇒ Object
82
83
84
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 82
def middleware
Rack::GridServe
end
|
.middleware_options ⇒ Object
86
87
88
89
90
91
92
93
94
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 86
def middleware_options
[
{
prefix: settings.url_prefix.dup.gsub(/^\/|\/$/,''),
db: settings.db,
}
]
end
|
Instance Method Details
#deletable?(variation_name = :original) ⇒ Boolean
66
67
68
69
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 66
def deletable? variation_name=:original
!WebUtils.blank? self.field_filename(variation_name)
end
|
#location_root ⇒ Object
Attachee_prefix is moved on field_value for gridfs
19
20
21
22
23
24
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 19
def location_root
File.join(
settings.root,
settings.url_prefix
)
end
|
#next_available_filename(filename) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 26
def next_available_filename filename
ext = File.extname(filename)
base = "#{attachee_prefix}/#{File.basename(filename,ext)}"
i = 0
loop do
suffix = i==0 ? '' : "-#{i}"
potential_filename = [base,suffix,ext].join
if settings.db.fs.find(filename: potential_filename).count>0
i += 1
else
filename = potential_filename
break
end
end
filename
end
|
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 43
def perform_create hash
if hash[:variation].nil?
fn = next_available_filename(hash[:filename])
file = hash[:tempfile]
type = hash[:type]
else
fn = WebUtils.filename_variation hash[:future_field_value], hash[:variation].name, hash[:variation].ext
file = File.open(hash[:variation_path])
type = Rack::Mime.mime_type ".#{hash[:variation].ext}"
end
settings.db.fs.upload_from_stream(
fn,
file, {
content_type: type,
metadata: {
parent_collection: (self.document.class.respond_to?(:collection) ? self.document.class.collection.name : self.attachee_prefix),
}
}
)
file.close unless hash[:variation].nil?
fn
end
|
71
72
73
74
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 71
def perform_delete variation_name=:original
gridfile = settings.db.fs.find(filename: self.field_filename(variation_name)).first
settings.db.fs.delete(gridfile['_id']) unless gridfile.nil?
end
|
#url(variation_name = :original) ⇒ Object
Attachee_prefix is moved on field_value for gridfs
14
15
16
17
|
# File 'lib/populate_me/grid_fs_attachment.rb', line 14
def url variation_name=:original
return nil if WebUtils.blank?(self.field_filename(variation_name))
"#{settings.url_prefix.sub(/\/$/,'')}/#{self.field_filename(variation_name)}"
end
|