avatar.rb 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Avatar < ApplicationModel
  3. belongs_to :object_lookup, class_name: 'ObjectLookup'
  4. =begin
  5. add an avatar based on auto detection (email address)
  6. Avatar.auto_detection(
  7. object: 'User',
  8. o_id: user.id,
  9. url: 'somebody@example.com',
  10. updated_by_id: 1,
  11. created_by_id: 1,
  12. )
  13. =end
  14. def self.auto_detection(data)
  15. # return if we run import mode
  16. return if Setting.get('import_mode')
  17. return if data[:url].blank?
  18. Avatar.add(
  19. object: data[:object],
  20. o_id: data[:o_id],
  21. url: data[:url],
  22. source: 'zammad.com',
  23. deletable: false,
  24. updated_by_id: 1,
  25. created_by_id: 1,
  26. )
  27. end
  28. =begin
  29. add avatar by upload
  30. Avatar.add(
  31. object: 'User',
  32. o_id: user.id,
  33. default: true,
  34. full: {
  35. content: '...',
  36. mime_type: 'image/png',
  37. },
  38. resize: {
  39. content: '...',
  40. mime_type: 'image/png',
  41. },
  42. source: 'web',
  43. deletable: true,
  44. updated_by_id: 1,
  45. created_by_id: 1,
  46. )
  47. add avatar by url
  48. Avatar.add(
  49. object: 'User',
  50. o_id: user.id,
  51. default: true,
  52. url: ...,
  53. source: 'web',
  54. deletable: true,
  55. updated_by_id: 1,
  56. created_by_id: 1,
  57. )
  58. =end
  59. def self.add(data)
  60. # lookups
  61. if data[:object]
  62. object_id = ObjectLookup.by_name(data[:object])
  63. end
  64. # add initial avatar
  65. add_init_avatar(object_id, data[:o_id])
  66. record = {
  67. o_id: data[:o_id],
  68. object_lookup_id: object_id,
  69. default: true,
  70. deletable: data[:deletable],
  71. initial: false,
  72. source: data[:source],
  73. source_url: data[:url],
  74. updated_by_id: data[:updated_by_id],
  75. created_by_id: data[:created_by_id],
  76. }
  77. # check if avatar with url already exists
  78. avatar_already_exists = nil
  79. if data[:source].present?
  80. avatar_already_exists = Avatar.find_by(
  81. object_lookup_id: object_id,
  82. o_id: data[:o_id],
  83. source: data[:source],
  84. )
  85. end
  86. # fetch image based on http url
  87. if data[:url].present?
  88. if data[:url].class == Tempfile
  89. logger.info "Reading image from tempfile '#{data[:url].inspect}'"
  90. content = data[:url].read
  91. filename = data[:url].path
  92. mime_type = 'image'
  93. if filename.match?(/\.png/i)
  94. mime_type = 'image/png'
  95. end
  96. if filename.match?(/\.(jpg|jpeg)/i)
  97. mime_type = 'image/jpeg'
  98. end
  99. data[:resize] ||= {}
  100. data[:resize][:content] = content
  101. data[:resize][:mime_type] = mime_type
  102. data[:full] ||= {}
  103. data[:full][:content] = content
  104. data[:full][:mime_type] = mime_type
  105. elsif data[:url].to_s.match?(/^http/)
  106. url = data[:url].to_s
  107. # check if source ist already updated within last 2 minutes
  108. if avatar_already_exists&.source_url == url
  109. return if avatar_already_exists.updated_at > 2.minutes.ago
  110. end
  111. # twitter workaround to get bigger avatar images
  112. # see also https://dev.twitter.com/overview/general/user-profile-images-and-banners
  113. if url.match?(%r{//pbs.twimg.com/}i)
  114. url.sub!(/normal\.(png|jpg|gif)$/, 'bigger.\1')
  115. end
  116. # fetch image
  117. response = UserAgent.get(
  118. url,
  119. {},
  120. {
  121. open_timeout: 4,
  122. read_timeout: 6,
  123. total_timeout: 6,
  124. },
  125. )
  126. if !response.success?
  127. logger.info "Can't fetch '#{url}' (maybe no avatar available), http code: #{response.code}"
  128. return
  129. end
  130. logger.info "Fetchd image '#{url}', http code: #{response.code}"
  131. mime_type = 'image'
  132. if url.match?(/\.png/i)
  133. mime_type = 'image/png'
  134. end
  135. if url.match?(/\.(jpg|jpeg)/i)
  136. mime_type = 'image/jpeg'
  137. end
  138. data[:resize] ||= {}
  139. data[:resize][:content] = response.body
  140. data[:resize][:mime_type] = mime_type
  141. data[:full] ||= {}
  142. data[:full][:content] = response.body
  143. data[:full][:mime_type] = mime_type
  144. # try zammad backend to find image based on email
  145. elsif data[:url].to_s.match?(/@/)
  146. url = data[:url].to_s
  147. # check if source ist already updated within last 3 minutes
  148. if avatar_already_exists&.source_url == url
  149. return if avatar_already_exists.updated_at > 2.minutes.ago
  150. end
  151. # fetch image
  152. image = Service::Image.user(url)
  153. return if !image
  154. data[:resize] ||= {}
  155. data[:resize] = image
  156. data[:full] ||= {}
  157. data[:full] = image
  158. end
  159. end
  160. # check if avatar need to be updated
  161. if data[:resize].present? && data[:resize][:content].present?
  162. record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content])
  163. if avatar_already_exists&.store_hash == record[:store_hash]
  164. avatar_already_exists.touch # rubocop:disable Rails/SkipsModelValidations
  165. return avatar_already_exists
  166. end
  167. end
  168. # store images
  169. object_name = "Avatar::#{data[:object]}"
  170. if data[:full].present?
  171. store_full = Store.add(
  172. object: "#{object_name}::Full",
  173. o_id: data[:o_id],
  174. data: data[:full][:content],
  175. filename: 'avatar_full',
  176. preferences: {
  177. 'Mime-Type' => data[:full][:mime_type]
  178. },
  179. created_by_id: data[:created_by_id],
  180. )
  181. record[:store_full_id] = store_full.id
  182. record[:store_hash] = Digest::MD5.hexdigest(data[:full][:content])
  183. end
  184. if data[:resize].present?
  185. store_resize = Store.add(
  186. object: "#{object_name}::Resize",
  187. o_id: data[:o_id],
  188. data: data[:resize][:content],
  189. filename: 'avatar',
  190. preferences: {
  191. 'Mime-Type' => data[:resize][:mime_type]
  192. },
  193. created_by_id: data[:created_by_id],
  194. )
  195. record[:store_resize_id] = store_resize.id
  196. record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content])
  197. end
  198. return if record[:store_resize_id].blank? || record[:store_hash].blank?
  199. # update existing
  200. if avatar_already_exists
  201. avatar_already_exists.update!(record)
  202. avatar = avatar_already_exists
  203. # add new one and set it as default
  204. else
  205. avatar = Avatar.create(record)
  206. set_default_items(object_id, data[:o_id], avatar.id)
  207. end
  208. avatar
  209. end
  210. =begin
  211. set avatars as default
  212. Avatar.set_default('User', 123, avatar_id)
  213. =end
  214. def self.set_default(object_name, o_id, avatar_id)
  215. object_id = ObjectLookup.by_name(object_name)
  216. avatar = Avatar.find_by(
  217. object_lookup_id: object_id,
  218. o_id: o_id,
  219. id: avatar_id,
  220. )
  221. avatar.default = true
  222. avatar.save!
  223. # set all other to default false
  224. set_default_items(object_id, o_id, avatar_id)
  225. avatar
  226. end
  227. =begin
  228. remove all avatars of an object
  229. Avatar.remove('User', 123)
  230. =end
  231. def self.remove(object_name, o_id)
  232. object_id = ObjectLookup.by_name(object_name)
  233. Avatar.where(
  234. object_lookup_id: object_id,
  235. o_id: o_id,
  236. ).destroy_all
  237. object_name_store = "Avatar::#{object_name}"
  238. Store.remove(
  239. object: "#{object_name_store}::Full",
  240. o_id: o_id,
  241. )
  242. Store.remove(
  243. object: "#{object_name_store}::Resize",
  244. o_id: o_id,
  245. )
  246. end
  247. =begin
  248. remove one avatars of an object
  249. Avatar.remove_one('User', 123, avatar_id)
  250. =end
  251. def self.remove_one(object_name, o_id, avatar_id)
  252. object_id = ObjectLookup.by_name(object_name)
  253. Avatar.where(
  254. object_lookup_id: object_id,
  255. o_id: o_id,
  256. id: avatar_id,
  257. ).destroy_all
  258. end
  259. =begin
  260. return all avatars of an user
  261. avatars = Avatar.list('User', 123)
  262. =end
  263. def self.list(object_name, o_id)
  264. object_id = ObjectLookup.by_name(object_name)
  265. avatars = Avatar.where(
  266. object_lookup_id: object_id,
  267. o_id: o_id,
  268. ).order('initial DESC, deletable ASC, created_at ASC, id DESC')
  269. # add initial avatar
  270. add_init_avatar(object_id, o_id)
  271. avatar_list = []
  272. avatars.each do |avatar|
  273. data = avatar.attributes
  274. if avatar.store_resize_id
  275. file = Store.find(avatar.store_resize_id)
  276. data['content'] = "data:#{file.preferences['Mime-Type']};base64,#{Base64.strict_encode64(file.content)}"
  277. end
  278. avatar_list.push data
  279. end
  280. avatar_list
  281. end
  282. =begin
  283. get default avatar image of user by hash
  284. store = Avatar.get_by_hash(hash)
  285. returns:
  286. store object
  287. =end
  288. def self.get_by_hash(hash)
  289. avatar = Avatar.find_by(
  290. store_hash: hash,
  291. )
  292. return if !avatar
  293. Store.find(avatar.store_resize_id)
  294. end
  295. =begin
  296. get default avatar of user by user id
  297. avatar = Avatar.get_default('User', user_id)
  298. returns:
  299. avatar object
  300. =end
  301. def self.get_default(object_name, o_id)
  302. object_id = ObjectLookup.by_name(object_name)
  303. Avatar.find_by(
  304. object_lookup_id: object_id,
  305. o_id: o_id,
  306. default: true,
  307. )
  308. end
  309. def self.set_default_items(object_id, o_id, avatar_id)
  310. avatars = Avatar.where(
  311. object_lookup_id: object_id,
  312. o_id: o_id,
  313. ).order('created_at ASC, id DESC')
  314. avatars.each do |avatar|
  315. next if avatar.id == avatar_id
  316. avatar.default = false
  317. avatar.save!
  318. end
  319. end
  320. def self.add_init_avatar(object_id, o_id)
  321. count = Avatar.where(
  322. object_lookup_id: object_id,
  323. o_id: o_id,
  324. ).count
  325. return if count.positive?
  326. Avatar.create(
  327. o_id: o_id,
  328. object_lookup_id: object_id,
  329. default: true,
  330. source: 'init',
  331. initial: true,
  332. deletable: false,
  333. updated_by_id: 1,
  334. created_by_id: 1,
  335. )
  336. end
  337. end