2014-11-19 22:22:13 +00:00
|
|
|
module StaticAssets
|
|
|
|
|
|
|
|
def self.data_url_attributes( data_url )
|
|
|
|
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
|
|
|
|
|
|
|
|
# store image 1:1
|
|
|
|
def self.store_raw( content, content_type )
|
2015-04-27 13:42:53 +00:00
|
|
|
Store.remove( object: 'System::Logo', o_id: 1 )
|
2014-11-19 22:22:13 +00:00
|
|
|
Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 1,
|
|
|
|
data: content,
|
|
|
|
filename: 'image',
|
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
|
|
|
)
|
|
|
|
Digest::MD5.hexdigest( content )
|
|
|
|
end
|
|
|
|
|
|
|
|
# read raw 1:1
|
|
|
|
def self.read_raw
|
2015-04-27 13:42:53 +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
|
|
|
|
|
|
|
|
# store image in right size
|
|
|
|
def self.store( content, content_type )
|
2015-04-27 13:42:53 +00:00
|
|
|
Store.remove( object: 'System::Logo', o_id: 2 )
|
2014-11-19 22:22:13 +00:00
|
|
|
Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 2,
|
|
|
|
data: content,
|
|
|
|
filename: 'image',
|
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
|
|
|
)
|
|
|
|
StaticAssets.sync
|
|
|
|
Digest::MD5.hexdigest( content )
|
|
|
|
end
|
|
|
|
|
|
|
|
# read image
|
|
|
|
def self.read
|
2014-11-20 07:44:50 +00:00
|
|
|
|
|
|
|
# use reduced dimensions
|
2015-04-27 13:42:53 +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-04-27 13:42:53 +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]
|
|
|
|
file = Store.find( list[0] )
|
|
|
|
hash = Digest::MD5.hexdigest( file.content )
|
|
|
|
Setting.set('product_logo', hash)
|
|
|
|
return file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# sync image to fs
|
|
|
|
def self.sync
|
|
|
|
file = read
|
|
|
|
return if !file
|
|
|
|
|
|
|
|
hash = Digest::MD5.hexdigest( file.content )
|
2015-05-06 09:30:39 +00:00
|
|
|
path = "#{Rails.root}/public/assets/images/#{hash}"
|
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
|