store.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. class Store < ApplicationModel
  3. PREFERENCES_SIZE_MAX = 2400
  4. belongs_to :store_object, class_name: 'Store::Object', optional: true
  5. belongs_to :store_file, class_name: 'Store::File', optional: true
  6. delegate :content, to: :store_file
  7. delegate :provider, to: :store_file
  8. validates :filename, presence: true
  9. store :preferences
  10. before_validation :set_object_id
  11. before_create :set_store_file, :oversized_preferences_check
  12. after_create :generate_previews
  13. before_update :oversized_preferences_check
  14. attr_accessor :object, :data
  15. def set_object_id
  16. return if object.blank?
  17. self.store_object_id = Store::Object.create_if_not_exists(name: object).id
  18. end
  19. def set_store_file
  20. file = Store::File.add(data)
  21. self.size = data.to_s.bytesize
  22. self.store_file_id = file.id
  23. end
  24. def self.add(...)
  25. ActiveSupport::Deprecation.warn("The method 'Store.add' is deprecated. Use 'Store.create!' instead.")
  26. create!(...)
  27. end
  28. =begin
  29. get attachment of object
  30. list = Store.list(
  31. object: 'Ticket::Article',
  32. o_id: 4711,
  33. )
  34. returns
  35. result = [store1, store2]
  36. store1 = {
  37. size: 94123,
  38. filename: 'image.png',
  39. preferences: {
  40. content_type: 'image/png',
  41. content_id: 234,
  42. }
  43. }
  44. store1.content # binary_string
  45. =end
  46. def self.list(data)
  47. # search
  48. store_object_id = Store::Object.lookup(name: data[:object])
  49. Store.where(store_object_id: store_object_id, o_id: data[:o_id].to_i)
  50. .order(created_at: :asc)
  51. end
  52. =begin
  53. remove attachments of object from storage
  54. result = Store.remove(
  55. object: 'Ticket::Article',
  56. o_id: 4711,
  57. )
  58. returns
  59. result = true
  60. =end
  61. def self.remove(data)
  62. # search
  63. store_object_id = Store::Object.lookup(name: data[:object])
  64. stores = Store.where(store_object_id: store_object_id)
  65. .where(o_id: data[:o_id])
  66. .order(created_at: :asc)
  67. stores.each do |store|
  68. # check backend for references
  69. Store.remove_item(store.id)
  70. end
  71. true
  72. end
  73. =begin
  74. remove one attachment from storage
  75. Store.remove_item(store_id)
  76. =end
  77. def self.remove_item(store_id)
  78. store = Store.find(store_id)
  79. file_id = store.store_file_id
  80. # check backend for references
  81. files = Store.where(store_file_id: file_id)
  82. if files.count > 1 || files.first.id != store.id
  83. store.destroy!
  84. return true
  85. end
  86. store.destroy!
  87. Store::File.find(file_id).destroy!
  88. end
  89. =begin
  90. get content of file in preview size
  91. store = Store.find(store_id)
  92. content_as_string = store.content_preview
  93. returns
  94. content_as_string
  95. =end
  96. def content_preview(options = {})
  97. file = Store::File.find_by(id: store_file_id)
  98. if !file
  99. raise "No such file #{store_file_id}!"
  100. end
  101. raise __('Content preview could not be generated.') if options[:silence] != true && preferences[:content_preview] != true
  102. image_resize(file.content, 200)
  103. end
  104. =begin
  105. get content of file in inline size
  106. store = Store.find(store_id)
  107. content_as_string = store.content_inline
  108. returns
  109. content_as_string
  110. =end
  111. def content_inline(options = {})
  112. file = Store::File.find_by(id: store_file_id)
  113. if !file
  114. raise "No such file #{store_file_id}!"
  115. end
  116. raise __('Inline content could not be generated.') if options[:silence] != true && preferences[:content_inline] != true
  117. image_resize(file.content, 1800)
  118. end
  119. =begin
  120. get content of file
  121. store = Store.find(store_id)
  122. location_of_file = store.save_to_file
  123. returns
  124. location_of_file
  125. =end
  126. def save_to_file(path = nil)
  127. content
  128. file = Store::File.find_by(id: store_file_id)
  129. if !file
  130. raise "No such file #{store_file_id}!"
  131. end
  132. if !path
  133. path = Rails.root.join('tmp', filename)
  134. end
  135. ::File.binwrite(path, file.content)
  136. path
  137. end
  138. def attributes_for_display
  139. slice :id, :store_file_id, :filename, :size, :preferences
  140. end
  141. RESIZABLE_MIME_REGEXP = %r{image/(jpeg|jpg|png)}i
  142. def self.resizable_mime?(input)
  143. input.match? RESIZABLE_MIME_REGEXP
  144. end
  145. private
  146. def generate_previews
  147. return true if Setting.get('import_mode')
  148. resizable = preferences
  149. .slice('Mime-Type', 'Content-Type', 'mime_type', 'content_type')
  150. .values
  151. .any? { |mime| self.class.resizable_mime?(mime) }
  152. begin
  153. if resizable
  154. if content_preview(silence: true)
  155. preferences[:resizable] = true
  156. preferences[:content_preview] = true
  157. end
  158. if content_inline(silence: true)
  159. preferences[:resizable] = true
  160. preferences[:content_inline] = true
  161. end
  162. if preferences[:resizable]
  163. save!
  164. end
  165. end
  166. rescue => e
  167. logger.error e
  168. preferences[:resizable] = false
  169. save!
  170. end
  171. end
  172. def image_resize(content, width)
  173. local_sha = Digest::SHA256.hexdigest(content)
  174. Rails.cache.fetch("#{self.class}/image-resize-#{local_sha}_#{width}", expires_in: 6.months) do
  175. temp_file = ::Tempfile.new
  176. temp_file.binmode
  177. temp_file.write(content)
  178. temp_file.close
  179. image = Rszr::Image.load(temp_file.path)
  180. # do not resize image if image is smaller or already same size
  181. return if image.width <= width
  182. # do not resize image if new height is smaller then 7px (images
  183. # with small height are usually useful to resize)
  184. ratio = image.width / width
  185. return if image.height / ratio <= 6
  186. image.resize!(width, :auto)
  187. temp_file_resize = ::Tempfile.new.path
  188. image.save(temp_file_resize)
  189. ::File.binread(temp_file_resize)
  190. end
  191. end
  192. def oversized_preferences_check
  193. [[600, 100], [300, 60], [150, 30], [75, 15]].each do |row|
  194. return true if oversized_preferences_removed_by_content?(row[0])
  195. return true if oversized_preferences_removed_by_key?(row[1])
  196. end
  197. true
  198. end
  199. def oversized_preferences_removed_by_content?(max_char)
  200. oversized_preferences_removed? do |_key, content|
  201. content.try(:size).to_i > max_char
  202. end
  203. end
  204. def oversized_preferences_removed_by_key?(max_char)
  205. oversized_preferences_removed? do |key, _content|
  206. key.try(:size).to_i > max_char
  207. end
  208. end
  209. def oversized_preferences_removed?
  210. return true if !oversized_preferences_present?
  211. preferences&.each do |key, content|
  212. next if !yield(key, content)
  213. preferences.delete(key)
  214. Rails.logger.info "Removed oversized #{self.class.name} preference: '#{key}', '#{content}'"
  215. break if !oversized_preferences_present?
  216. end
  217. !oversized_preferences_present?
  218. end
  219. def oversized_preferences_present?
  220. preferences.to_yaml.size > PREFERENCES_SIZE_MAX
  221. end
  222. end