Added file extension to logo in start page to show logo also in IE 11.

This commit is contained in:
Martin Edenhofer 2015-07-13 21:40:33 +02:00
parent 68b618359e
commit 70d9de90bd
2 changed files with 114 additions and 25 deletions

View file

@ -0,0 +1,8 @@
class UpdateLogo < ActiveRecord::Migration
def up
return if !Setting.find_by(name: 'product_logo')
StaticAssets.read
StaticAssets.sync
end
end

View file

@ -1,6 +1,19 @@
module StaticAssets module StaticAssets
def self.data_url_attributes( data_url ) =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)
data = {} data = {}
if data_url =~ /^data:(.+?);base64,(.+?)$/ if data_url =~ /^data:(.+?);base64,(.+?)$/
data[:mime_type] = $1 data[:mime_type] = $1
@ -10,73 +23,141 @@ module StaticAssets
fail "Unable to parse data url: #{data_url.substr(0, 100)}" fail "Unable to parse data url: #{data_url.substr(0, 100)}"
end end
# store image 1:1 =begin
def self.store_raw( content, content_type )
Store.remove( object: 'System::Logo', o_id: 1 ) store image 1:1 in backend and return filename
Store.add(
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(
object: 'System::Logo', object: 'System::Logo',
o_id: 1, o_id: 1,
data: content, data: content,
filename: 'image', filename: 'logo_raw',
preferences: { preferences: {
'Content-Type' => content_type 'Content-Type' => content_type
}, },
) )
Digest::MD5.hexdigest( content ) filename(file)
end end
# read raw 1:1 =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
def self.read_raw def self.read_raw
list = Store.list( object: 'System::Logo', o_id: 1 ) list = Store.list(object: 'System::Logo', o_id: 1)
if list && list[0] if list && list[0]
return Store.find( list[0] ) return Store.find( list[0] )
end end
fail 'No such raw logo!' fail 'No such raw logo!'
end end
# store image in right size =begin
def self.store( content, content_type )
Store.remove( object: 'System::Logo', o_id: 2 ) store image in right size (resized) in backend and return filename
Store.add(
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(
object: 'System::Logo', object: 'System::Logo',
o_id: 2, o_id: 2,
data: content, data: content,
filename: 'image', filename: 'logo',
preferences: { preferences: {
'Content-Type' => content_type 'Content-Type' => content_type
}, },
) )
StaticAssets.sync StaticAssets.sync
Digest::MD5.hexdigest( content ) filename(file)
end end
# read image =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
def self.read def self.read
# use reduced dimensions # use reduced dimensions
list = Store.list( object: 'System::Logo', o_id: 2 ) list = Store.list(object: 'System::Logo', o_id: 2)
# as fallback use 1:1 # as fallback use 1:1
if !list || !list[0] if !list || !list[0]
list = Store.list( object: 'System::Logo', o_id: 1 ) list = Store.list(object: 'System::Logo', o_id: 1)
end end
# store hash in config # store hash in config
if list && list[0] if list && list[0]
file = Store.find( list[0] ) file = Store.find(list[0])
hash = Digest::MD5.hexdigest( file.content ) filelocation = filename(file)
Setting.set('product_logo', hash) Setting.set('product_logo', filelocation)
return file return file
end end
end end
# sync image to fs =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
def self.sync def self.sync
file = read file = read
return if !file return if !file
path = "#{Rails.root}/public/assets/images/#{filename(file)}"
hash = Digest::MD5.hexdigest( file.content )
path = "#{Rails.root}/public/assets/images/#{hash}"
File.open( path, 'wb' ) do |f| File.open( path, 'wb' ) do |f|
f.puts file.content f.puts file.content
end end