46
47
48
49
50
51
52
53
54
55
56
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/balloon/up.rb', line 46
def set_keyword
if defined?(MongoMapper)
key :file_name, String
key :width, Integer
key :height, Integer
key :content_type, String
key :file_size, Integer
key :storage, String
key :metadata
key :created_at
elsif defined?(Mongoid)
field :file_name, type: String
field :width, type: Integer
field :height, type: Integer
field :content_type, type: String
field :file_size, type: String
field :storage, type: String
field :metadata
field :created_at
end
before_create :save_db
after_destroy :uploader_delete
class_eval <<-RUBY
def save_db
return if meta.nil?
self.file_name = meta[:filename]
self.content_type = meta[:mime_type]
self.file_size = meta[:size]
self.storage = store_storage.to_s
self.width = meta[:width]
self.height = meta[:height]
if self.respond_to?(:metadata)
self.metadata = meta[:data]
end
end
def url(size_name = nil)
return "" if !respond_to?(:file_name) || file_name.nil?
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
@meta = { basename: basename, extension: extension.to_s }
set_storage_engine
storage_engine.retrieve!(size_name)
end
def path(size_name = nil)
return "" if !respond_to?(:file_name) || file_name.nil?
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
@meta = { basename: basename, extension: extension.to_s }
set_storage_engine
storage_engine.path!(size_name)
end
def local_path(size_name = nil)
return "" if !respond_to?(:file_name) || file_name.nil?
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
@meta = { basename: basename, extension: extension.to_s }
set_storage_engine
storage_engine.local_path!(size_name)
end
def uploader_delete
return if !respond_to?(:file_name) || file_name.nil?
extension = self.file_name.to_s.match(%r"(?!\\.{1})\\w{2,}$")
basename = self.file_name.to_s.gsub(%r"\\.{1}\\w{2,}$",'')
@meta = { basename: basename, extension: extension.to_s }
set_storage_engine
storage_engine.delete!
end
RUBY
end
|