avatar.rb 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. class Avatar < ApplicationModel
  3. belongs_to :object_lookup
  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?(%r{^https?://})
  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?(URI::MailTo::EMAIL_REGEXP)
  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] = image
  155. data[:full] = image
  156. end
  157. end
  158. # check if avatar need to be updated
  159. if data[:resize].present? && data[:resize][:content].present?
  160. record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content])
  161. if avatar_already_exists&.store_hash == record[:store_hash]
  162. avatar_already_exists.touch # rubocop:disable Rails/SkipsModelValidations
  163. return avatar_already_exists
  164. end
  165. end
  166. # store images
  167. object_name = "Avatar::#{data[:object]}"
  168. if data[:full].present?
  169. store_full = Store.add(
  170. object: "#{object_name}::Full",
  171. o_id: data[:o_id],
  172. data: data[:full][:content],
  173. filename: 'avatar_full',
  174. preferences: {
  175. 'Mime-Type' => data[:full][:mime_type]
  176. },
  177. created_by_id: data[:created_by_id],
  178. )
  179. record[:store_full_id] = store_full.id
  180. record[:store_hash] = Digest::MD5.hexdigest(data[:full][:content])
  181. end
  182. if data[:resize].present?
  183. store_resize = Store.add(
  184. object: "#{object_name}::Resize",
  185. o_id: data[:o_id],
  186. data: data[:resize][:content],
  187. filename: 'avatar',
  188. preferences: {
  189. 'Mime-Type' => data[:resize][:mime_type]
  190. },
  191. created_by_id: data[:created_by_id],
  192. )
  193. record[:store_resize_id] = store_resize.id
  194. record[:store_hash] = Digest::MD5.hexdigest(data[:resize][:content])
  195. end
  196. return if record[:store_resize_id].blank? || record[:store_hash].blank?
  197. # update existing
  198. if avatar_already_exists
  199. avatar_already_exists.update!(record)
  200. avatar = avatar_already_exists
  201. # add new one and set it as default
  202. else
  203. avatar = Avatar.create(record)
  204. set_default_items(object_id, data[:o_id], avatar.id)
  205. end
  206. avatar
  207. end
  208. =begin
  209. set avatars as default
  210. Avatar.set_default('User', 123, avatar_id)
  211. =end
  212. def self.set_default(object_name, o_id, avatar_id)
  213. object_id = ObjectLookup.by_name(object_name)
  214. avatar = Avatar.find_by(
  215. object_lookup_id: object_id,
  216. o_id: o_id,
  217. id: avatar_id,
  218. )
  219. avatar.default = true
  220. avatar.save!
  221. # set all other to default false
  222. set_default_items(object_id, o_id, avatar_id)
  223. avatar
  224. end
  225. =begin
  226. remove all avatars of an object
  227. Avatar.remove('User', 123)
  228. =end
  229. def self.remove(object_name, o_id)
  230. object_id = ObjectLookup.by_name(object_name)
  231. Avatar.where(
  232. object_lookup_id: object_id,
  233. o_id: o_id,
  234. ).destroy_all
  235. object_name_store = "Avatar::#{object_name}"
  236. Store.remove(
  237. object: "#{object_name_store}::Full",
  238. o_id: o_id,
  239. )
  240. Store.remove(
  241. object: "#{object_name_store}::Resize",
  242. o_id: o_id,
  243. )
  244. end
  245. =begin
  246. remove one avatars of an object
  247. Avatar.remove_one('User', 123, avatar_id)
  248. =end
  249. def self.remove_one(object_name, o_id, avatar_id)
  250. object_id = ObjectLookup.by_name(object_name)
  251. Avatar.where(
  252. object_lookup_id: object_id,
  253. o_id: o_id,
  254. id: avatar_id,
  255. ).destroy_all
  256. end
  257. =begin
  258. return all avatars of an user
  259. avatars = Avatar.list('User', 123)
  260. avatars = Avatar.list('User', 123, no_init_add_as_boolean) # per default true
  261. =end
  262. def self.list(object_name, o_id, no_init_add_as_boolean = true)
  263. object_id = ObjectLookup.by_name(object_name)
  264. avatars = Avatar.where(
  265. object_lookup_id: object_id,
  266. o_id: o_id,
  267. ).order('initial DESC, deletable ASC, created_at ASC, id DESC')
  268. # add initial avatar
  269. if no_init_add_as_boolean
  270. _add_init_avatar(object_id, o_id)
  271. end
  272. avatar_list = []
  273. avatars.each do |avatar|
  274. data = avatar.attributes
  275. if avatar.store_resize_id
  276. file = Store.find(avatar.store_resize_id)
  277. data['content'] = "data:#{file.preferences['Mime-Type']};base64,#{Base64.strict_encode64(file.content)}"
  278. end
  279. avatar_list.push data
  280. end
  281. avatar_list
  282. end
  283. =begin
  284. get default avatar image of user by hash
  285. store = Avatar.get_by_hash(hash)
  286. returns:
  287. store object
  288. =end
  289. def self.get_by_hash(hash)
  290. avatar = Avatar.find_by(
  291. store_hash: hash,
  292. )
  293. return if !avatar
  294. Store.find(avatar.store_resize_id)
  295. end
  296. =begin
  297. get default avatar of user by user id
  298. avatar = Avatar.get_default('User', user_id)
  299. returns:
  300. avatar object
  301. =end
  302. def self.get_default(object_name, o_id)
  303. object_id = ObjectLookup.by_name(object_name)
  304. Avatar.find_by(
  305. object_lookup_id: object_id,
  306. o_id: o_id,
  307. default: true,
  308. )
  309. end
  310. def self.set_default_items(object_id, o_id, avatar_id)
  311. avatars = Avatar.where(
  312. object_lookup_id: object_id,
  313. o_id: o_id,
  314. ).order('created_at ASC, id DESC')
  315. avatars.each do |avatar|
  316. next if avatar.id == avatar_id
  317. avatar.default = false
  318. avatar.save!
  319. end
  320. end
  321. def self._add_init_avatar(object_id, o_id)
  322. count = Avatar.where(
  323. object_lookup_id: object_id,
  324. o_id: o_id,
  325. ).count
  326. return if count.positive?
  327. object_name = ObjectLookup.by_id(object_id)
  328. return if !object_name.constantize.exists?(id: o_id)
  329. Avatar.create!(
  330. o_id: o_id,
  331. object_lookup_id: object_id,
  332. default: true,
  333. source: 'init',
  334. initial: true,
  335. deletable: false,
  336. updated_by_id: 1,
  337. created_by_id: 1,
  338. )
  339. end
  340. end