peertube-4.2.ts 3.9 KB

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