2014-11-19 22:22:13 +00:00
|
|
|
module StaticAssets
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
file_attributes = StaticAssets.data_url_attributes(data_url)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
mime_type: 'image/png',
|
|
|
|
content: image_bin_content,
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.data_url_attributes(data_url)
|
2014-11-19 22:22:13 +00:00
|
|
|
data = {}
|
|
|
|
if data_url =~ /^data:(.+?);base64,(.+?)$/
|
2014-12-01 07:32:35 +00:00
|
|
|
data[:mime_type] = $1
|
|
|
|
data[:content] = Base64.decode64($2)
|
2014-11-19 22:22:13 +00:00
|
|
|
return data
|
|
|
|
end
|
2015-05-07 11:27:07 +00:00
|
|
|
fail "Unable to parse data url: #{data_url.substr(0, 100)}"
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
store image 1:1 in backend and return filename
|
|
|
|
|
|
|
|
filename = StaticAssets.store_raw( content, content_type )
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
filename # hash.png
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.store_raw(content, content_type)
|
|
|
|
Store.remove(object: 'System::Logo', o_id: 1)
|
|
|
|
file = Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 1,
|
|
|
|
data: content,
|
2015-07-13 19:40:33 +00:00
|
|
|
filename: 'logo_raw',
|
2015-04-27 13:42:53 +00:00
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
|
|
|
)
|
2015-07-13 19:40:33 +00:00
|
|
|
filename(file)
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
read image 1:1 size in backend and return file (Store model)
|
|
|
|
|
|
|
|
store = StaticAssets.read_raw
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
store # Store model, e.g. store.content or store.preferences
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-11-19 22:22:13 +00:00
|
|
|
def self.read_raw
|
2015-07-13 19:40:33 +00:00
|
|
|
list = Store.list(object: 'System::Logo', o_id: 1)
|
2014-11-19 22:22:13 +00:00
|
|
|
if list && list[0]
|
|
|
|
return Store.find( list[0] )
|
|
|
|
end
|
2015-05-07 11:27:07 +00:00
|
|
|
fail 'No such raw logo!'
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
store image in right size (resized) in backend and return filename
|
|
|
|
|
|
|
|
filename = StaticAssets.store( content, content_type )
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
filename # hash.png
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.store(content, content_type)
|
|
|
|
Store.remove(object: 'System::Logo', o_id: 2)
|
|
|
|
file = Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 2,
|
|
|
|
data: content,
|
2015-07-13 19:40:33 +00:00
|
|
|
filename: 'logo',
|
2015-04-27 13:42:53 +00:00
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
|
|
|
)
|
|
|
|
StaticAssets.sync
|
2015-07-13 19:40:33 +00:00
|
|
|
filename(file)
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
read image size from backend (if not exists, read 1:1 size) and return file (Store model)
|
|
|
|
|
|
|
|
store = StaticAssets.read
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
store # Store model, e.g. store.content or store.preferences
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-11-19 22:22:13 +00:00
|
|
|
def self.read
|
2014-11-20 07:44:50 +00:00
|
|
|
|
|
|
|
# use reduced dimensions
|
2015-07-13 19:40:33 +00:00
|
|
|
list = Store.list(object: 'System::Logo', o_id: 2)
|
2014-11-20 07:44:50 +00:00
|
|
|
|
|
|
|
# as fallback use 1:1
|
|
|
|
if !list || !list[0]
|
2015-07-13 19:40:33 +00:00
|
|
|
list = Store.list(object: 'System::Logo', o_id: 1)
|
2014-11-20 07:44:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# store hash in config
|
2014-11-19 22:22:13 +00:00
|
|
|
if list && list[0]
|
2015-07-13 19:40:33 +00:00
|
|
|
file = Store.find(list[0])
|
|
|
|
filelocation = filename(file)
|
|
|
|
Setting.set('product_logo', filelocation)
|
2014-11-19 22:22:13 +00:00
|
|
|
return file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-13 19:40:33 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
generate filename based on Store model
|
|
|
|
|
|
|
|
filename = StaticAssets.filename(store)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.filename(file)
|
|
|
|
hash = Digest::MD5.hexdigest(file.content)
|
|
|
|
extention = ''
|
|
|
|
if file.preferences['Content-Type'] =~ /jpg|jpeg/i
|
|
|
|
extention = '.jpg'
|
|
|
|
elsif file.preferences['Content-Type'] =~ /png/i
|
|
|
|
extention = '.png'
|
|
|
|
end
|
|
|
|
"#{hash}#{extention}"
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
sync image to fs (public/assets/images/hash.png)
|
|
|
|
|
|
|
|
StaticAssets.sync
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-11-19 22:22:13 +00:00
|
|
|
def self.sync
|
|
|
|
file = read
|
|
|
|
return if !file
|
2015-07-13 19:40:33 +00:00
|
|
|
path = "#{Rails.root}/public/assets/images/#{filename(file)}"
|
2014-11-19 22:22:13 +00:00
|
|
|
File.open( path, 'wb' ) do |f|
|
|
|
|
f.puts file.content
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|