123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- module StaticAssets
- def self.data_url_attributes(data_url)
- data = {}
- if data_url =~ %r{^data:(.+?);base64,(.+?)$}
- data[:mime_type] = $1
- data[:content] = Base64.decode64($2)
- if data[:mime_type] =~ %r{/(.+?)$}
- data[:file_extention] = $1
- end
- return data
- end
- raise "Unable to parse data url: #{data_url&.slice(0, 100)}"
- end
- def self.store_raw(content, content_type)
- Store.remove(object: 'System::Logo', o_id: 1)
- file = Store.create!(
- object: 'System::Logo',
- o_id: 1,
- data: content,
- filename: 'logo_raw',
- preferences: {
- 'Content-Type' => content_type
- },
- created_by_id: 1,
- )
- filename(file)
- end
- def self.read_raw
- list = Store.list(object: 'System::Logo', o_id: 1)
- if list && list[0]
- return Store.find(list[0])
- end
- raise __('The raw logo could not be read.')
- end
- def self.store(content, content_type)
- Store.remove(object: 'System::Logo', o_id: 2)
- file = Store.create!(
- object: 'System::Logo',
- o_id: 2,
- data: content,
- filename: 'logo',
- preferences: {
- 'Content-Type' => content_type
- },
- created_by_id: 1,
- )
- StaticAssets.sync
- filename(file)
- end
- def self.read
-
- list = Store.list(object: 'System::Logo', o_id: 2)
-
- if !list || !list[0]
- list = Store.list(object: 'System::Logo', o_id: 1)
- end
-
- return if !list || !list[0]
- file = Store.find(list[0].id)
- filelocation = filename(file)
- Setting.set('product_logo', filelocation)
- file
- end
- def self.filename(file)
- hash = Digest::MD5.hexdigest(file.content)
- extension = case file.preferences['Content-Type']
- when %r{jpg|jpeg}i
- '.jpg'
- when %r{png}i
- '.png'
- when %r{gif}i
- '.gif'
- when %r{svg}i
- '.svg'
- else
- ''
- end
- "#{hash}#{extension}"
- end
- =begin
- sync image to fs (public/assets/images/hash.png)
- StaticAssets.sync
- =end
- def self.sync
- file = read
- return if !file
- path = Rails.public_path.join('assets', 'images', filename(file))
- File.open(path, 'wb') do |f|
- f.puts file.content
- end
- end
- end
|