2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
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,
|
2018-06-25 09:25:30 +00:00
|
|
|
file_extention: 'png',
|
2015-07-13 19:40:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.data_url_attributes(data_url)
|
2014-11-19 22:22:13 +00:00
|
|
|
data = {}
|
2021-05-12 11:37:44 +00:00
|
|
|
if data_url =~ %r{^data:(.+?);base64,(.+?)$}
|
2014-12-01 07:32:35 +00:00
|
|
|
data[:mime_type] = $1
|
|
|
|
data[:content] = Base64.decode64($2)
|
2018-06-25 09:25:30 +00:00
|
|
|
if data[:mime_type] =~ %r{/(.+?)$}
|
|
|
|
data[:file_extention] = $1
|
|
|
|
end
|
2014-11-19 22:22:13 +00:00
|
|
|
return data
|
|
|
|
end
|
2021-04-30 13:36:23 +00:00
|
|
|
raise "Unable to parse data url: #{data_url&.slice(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
|
|
|
|
|
2016-05-10 13:06:51 +00:00
|
|
|
filename = StaticAssets.store_raw(content, content_type)
|
2015-07-13 19:40:33 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
filename # hash.png
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.store_raw(content, content_type)
|
|
|
|
Store.remove(object: 'System::Logo', o_id: 1)
|
|
|
|
file = Store.add(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 1,
|
|
|
|
data: content,
|
|
|
|
filename: 'logo_raw',
|
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
2015-08-04 18:57:11 +00:00
|
|
|
created_by_id: 1,
|
2014-11-19 22:22:13 +00:00
|
|
|
)
|
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]
|
2021-07-16 13:38:01 +00:00
|
|
|
return Store.find(list[0])
|
2014-11-19 22:22:13 +00:00
|
|
|
end
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-03-01 14:26:46 +00:00
|
|
|
raise '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(
|
2018-12-19 17:31:51 +00:00
|
|
|
object: 'System::Logo',
|
|
|
|
o_id: 2,
|
|
|
|
data: content,
|
|
|
|
filename: 'logo',
|
|
|
|
preferences: {
|
2014-11-19 22:22:13 +00:00
|
|
|
'Content-Type' => content_type
|
|
|
|
},
|
2015-08-04 18:57:11 +00:00
|
|
|
created_by_id: 1,
|
2014-11-19 22:22:13 +00:00
|
|
|
)
|
|
|
|
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
|
2020-06-22 09:57:45 +00:00
|
|
|
return if !list || !list[0]
|
2017-11-23 08:09:44 +00:00
|
|
|
|
|
|
|
file = Store.find(list[0].id)
|
|
|
|
filelocation = filename(file)
|
|
|
|
Setting.set('product_logo', filelocation)
|
|
|
|
file
|
2014-11-19 22:22:13 +00:00
|
|
|
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 = ''
|
2020-07-13 12:46:08 +00:00
|
|
|
case file.preferences['Content-Type']
|
2021-05-12 11:37:44 +00:00
|
|
|
when %r{jpg|jpeg}i
|
2015-07-13 19:40:33 +00:00
|
|
|
extention = '.jpg'
|
2021-05-12 11:37:44 +00:00
|
|
|
when %r{png}i
|
2015-07-13 19:40:33 +00:00
|
|
|
extention = '.png'
|
2021-05-12 11:37:44 +00:00
|
|
|
when %r{gif}i
|
2015-08-04 18:57:11 +00:00
|
|
|
extention = '.gif'
|
2021-05-12 11:37:44 +00:00
|
|
|
when %r{svg}i
|
2015-08-04 18:57:11 +00:00
|
|
|
extention = '.svg'
|
2015-07-13 19:40:33 +00:00
|
|
|
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
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
path = Rails.root.join('public', 'assets', 'images', filename(file))
|
|
|
|
File.open(path, 'wb') do |f|
|
2014-11-19 22:22:13 +00:00
|
|
|
f.puts file.content
|
|
|
|
end
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|