peertube-4.2.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { minBy } from 'lodash'
  2. import { join } from 'path'
  3. import { getImageSize, processImage } from '@server/helpers/image-utils'
  4. import { CONFIG } from '@server/initializers/config'
  5. import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
  6. import { updateActorImages } from '@server/lib/activitypub/actors'
  7. import { sendUpdateActor } from '@server/lib/activitypub/send'
  8. import { getBiggestActorImage } from '@server/lib/actor-image'
  9. import { JobQueue } from '@server/lib/job-queue'
  10. import { AccountModel } from '@server/models/account/account'
  11. import { ActorModel } from '@server/models/actor/actor'
  12. import { VideoChannelModel } from '@server/models/video/video-channel'
  13. import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models'
  14. import { getLowercaseExtension } from '@shared/core-utils'
  15. import { buildUUID } from '@shared/extra-utils'
  16. import { ActorImageType } from '@shared/models'
  17. import { initDatabaseModels } from '../../server/initializers/database'
  18. run()
  19. .then(() => process.exit(0))
  20. .catch(err => {
  21. console.error(err)
  22. process.exit(-1)
  23. })
  24. async function run () {
  25. console.log('Generate avatar miniatures from existing avatars.')
  26. await initDatabaseModels(true)
  27. JobQueue.Instance.init()
  28. const accounts: AccountModel[] = await AccountModel.findAll({
  29. include: [
  30. {
  31. model: ActorModel,
  32. required: true,
  33. where: {
  34. serverId: null
  35. }
  36. },
  37. {
  38. model: VideoChannelModel,
  39. include: [
  40. {
  41. model: AccountModel
  42. }
  43. ]
  44. }
  45. ]
  46. })
  47. for (const account of accounts) {
  48. try {
  49. await fillAvatarSizeIfNeeded(account)
  50. await generateSmallerAvatarIfNeeded(account)
  51. } catch (err) {
  52. console.error(`Cannot process account avatar ${account.name}`, err)
  53. }
  54. for (const videoChannel of account.VideoChannels) {
  55. try {
  56. await fillAvatarSizeIfNeeded(videoChannel)
  57. await generateSmallerAvatarIfNeeded(videoChannel)
  58. } catch (err) {
  59. console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
  60. }
  61. }
  62. }
  63. console.log('Generation finished!')
  64. }
  65. async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
  66. const avatars = accountOrChannel.Actor.Avatars
  67. for (const avatar of avatars) {
  68. if (avatar.width && avatar.height) continue
  69. console.log('Filling size of avatars of %s.', accountOrChannel.name)
  70. const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES, avatar.filename))
  71. avatar.width = width
  72. avatar.height = height
  73. await avatar.save()
  74. }
  75. }
  76. async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
  77. const avatars = accountOrChannel.Actor.Avatars
  78. if (avatars.length !== 1) {
  79. return
  80. }
  81. console.log(`Processing ${accountOrChannel.name}.`)
  82. await generateSmallerAvatar(accountOrChannel.Actor)
  83. accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
  84. return sendUpdateActor(accountOrChannel, undefined)
  85. }
  86. async function generateSmallerAvatar (actor: MActorDefault) {
  87. const bigAvatar = getBiggestActorImage(actor.Avatars)
  88. const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
  89. const sourceFilename = bigAvatar.filename
  90. const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
  91. const source = join(CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename)
  92. const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
  93. await processImage({ path: source, destination, newSize: imageSize, keepOriginal: true })
  94. const actorImageInfo = {
  95. name: newImageName,
  96. fileUrl: null,
  97. height: imageSize.height,
  98. width: imageSize.width,
  99. onDisk: true
  100. }
  101. await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)
  102. }