static_assets.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. module StaticAssets
  2. =begin
  3. file_attributes = StaticAssets.data_url_attributes(data_url)
  4. returns
  5. {
  6. mime_type: 'image/png',
  7. content: image_bin_content,
  8. file_extention: 'png',
  9. }
  10. =end
  11. def self.data_url_attributes(data_url)
  12. data = {}
  13. if data_url =~ /^data:(.+?);base64,(.+?)$/
  14. data[:mime_type] = $1
  15. data[:content] = Base64.decode64($2)
  16. if data[:mime_type] =~ %r{/(.+?)$}
  17. data[:file_extention] = $1
  18. end
  19. return data
  20. end
  21. raise "Unable to parse data url: #{data_url.substr(0, 100)}"
  22. end
  23. =begin
  24. store image 1:1 in backend and return filename
  25. filename = StaticAssets.store_raw(content, content_type)
  26. returns
  27. filename # hash.png
  28. =end
  29. def self.store_raw(content, content_type)
  30. Store.remove(object: 'System::Logo', o_id: 1)
  31. file = Store.add(
  32. object: 'System::Logo',
  33. o_id: 1,
  34. data: content,
  35. filename: 'logo_raw',
  36. preferences: {
  37. 'Content-Type' => content_type
  38. },
  39. created_by_id: 1,
  40. )
  41. filename(file)
  42. end
  43. =begin
  44. read image 1:1 size in backend and return file (Store model)
  45. store = StaticAssets.read_raw
  46. returns
  47. store # Store model, e.g. store.content or store.preferences
  48. =end
  49. def self.read_raw
  50. list = Store.list(object: 'System::Logo', o_id: 1)
  51. if list && list[0]
  52. return Store.find( list[0] )
  53. end
  54. raise 'No such raw logo!'
  55. end
  56. =begin
  57. store image in right size (resized) in backend and return filename
  58. filename = StaticAssets.store( content, content_type )
  59. returns
  60. filename # hash.png
  61. =end
  62. def self.store(content, content_type)
  63. Store.remove(object: 'System::Logo', o_id: 2)
  64. file = Store.add(
  65. object: 'System::Logo',
  66. o_id: 2,
  67. data: content,
  68. filename: 'logo',
  69. preferences: {
  70. 'Content-Type' => content_type
  71. },
  72. created_by_id: 1,
  73. )
  74. StaticAssets.sync
  75. filename(file)
  76. end
  77. =begin
  78. read image size from backend (if not exists, read 1:1 size) and return file (Store model)
  79. store = StaticAssets.read
  80. returns
  81. store # Store model, e.g. store.content or store.preferences
  82. =end
  83. def self.read
  84. # use reduced dimensions
  85. list = Store.list(object: 'System::Logo', o_id: 2)
  86. # as fallback use 1:1
  87. if !list || !list[0]
  88. list = Store.list(object: 'System::Logo', o_id: 1)
  89. end
  90. # store hash in config
  91. return if !list || !list[0]
  92. file = Store.find(list[0].id)
  93. filelocation = filename(file)
  94. Setting.set('product_logo', filelocation)
  95. file
  96. end
  97. =begin
  98. generate filename based on Store model
  99. filename = StaticAssets.filename(store)
  100. =end
  101. def self.filename(file)
  102. hash = Digest::MD5.hexdigest(file.content)
  103. extention = ''
  104. if file.preferences['Content-Type'].match?(/jpg|jpeg/i)
  105. extention = '.jpg'
  106. elsif file.preferences['Content-Type'].match?(/png/i)
  107. extention = '.png'
  108. elsif file.preferences['Content-Type'].match?(/gif/i)
  109. extention = '.gif'
  110. elsif file.preferences['Content-Type'].match?(/svg/i)
  111. extention = '.svg'
  112. end
  113. "#{hash}#{extention}"
  114. end
  115. =begin
  116. sync image to fs (public/assets/images/hash.png)
  117. StaticAssets.sync
  118. =end
  119. def self.sync
  120. file = read
  121. return if !file
  122. path = Rails.root.join('public', 'assets', 'images', filename(file))
  123. File.open(path, 'wb') do |f|
  124. f.puts file.content
  125. end
  126. end
  127. end