static_assets.rb 3.2 KB

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