ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

123 lines
3.8 KiB

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