aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/migrations
diff options
context:
space:
mode:
authorkontrollanten <6680299+kontrollanten@users.noreply.github.com>2022-02-28 08:34:43 +0100
committerGitHub <noreply@github.com>2022-02-28 08:34:43 +0100
commitd0800f7661f13fabe7bb6f4aa0ea50764f106405 (patch)
treed43e6b0b6f4a5a32e03487e6464edbcaf288be2a /scripts/migrations
parent5cad2ca9db9b9d138f8a33058d10b94a9fd50c69 (diff)
downloadPeerTube-d0800f7661f13fabe7bb6f4aa0ea50764f106405.tar.gz
PeerTube-d0800f7661f13fabe7bb6f4aa0ea50764f106405.tar.zst
PeerTube-d0800f7661f13fabe7bb6f4aa0ea50764f106405.zip
Implement avatar miniatures (#4639)
* client: remove unused file * refactor(client/my-actor-avatar): size from input Read size from component input instead of scss, to make it possible to use smaller avatar images when implemented. * implement avatar miniatures close #4560 * fix(test): max file size * fix(search-index): normalize res acc to avatarMini * refactor avatars to an array * client/search: resize channel avatar to 120 * refactor(client/videos): remove unused function * client(actor-avatar): set default size * fix tests and avatars full result When findOne is used only an array containting one avatar is returned. * update migration version and version notations * server/search: harmonize normalizing * Cleanup avatar miniature PR Co-authored-by: Chocobozzz <me@florianbigard.com>
Diffstat (limited to 'scripts/migrations')
-rw-r--r--scripts/migrations/peertube-4.2.ts106
1 files changed, 106 insertions, 0 deletions
diff --git a/scripts/migrations/peertube-4.2.ts b/scripts/migrations/peertube-4.2.ts
new file mode 100644
index 000000000..045c3e511
--- /dev/null
+++ b/scripts/migrations/peertube-4.2.ts
@@ -0,0 +1,106 @@
1import { minBy } from 'lodash'
2import { join } from 'path'
3import { processImage } from '@server/helpers/image-utils'
4import { CONFIG } from '@server/initializers/config'
5import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
6import { updateActorImages } from '@server/lib/activitypub/actors'
7import { sendUpdateActor } from '@server/lib/activitypub/send'
8import { getBiggestActorImage } from '@server/lib/actor-image'
9import { JobQueue } from '@server/lib/job-queue'
10import { AccountModel } from '@server/models/account/account'
11import { ActorModel } from '@server/models/actor/actor'
12import { VideoChannelModel } from '@server/models/video/video-channel'
13import { MAccountDefault, MActorDefault, MChannelDefault } from '@server/types/models'
14import { getLowercaseExtension } from '@shared/core-utils'
15import { buildUUID } from '@shared/extra-utils'
16import { ActorImageType } from '@shared/models'
17import { initDatabaseModels } from '../../server/initializers/database'
18
19run()
20 .then(() => process.exit(0))
21 .catch(err => {
22 console.error(err)
23 process.exit(-1)
24 })
25
26async function run () {
27 console.log('Generate avatar miniatures from existing avatars.')
28
29 await initDatabaseModels(true)
30 JobQueue.Instance.init(true)
31
32 const accounts: AccountModel[] = await AccountModel.findAll({
33 include: [
34 {
35 model: ActorModel,
36 required: true,
37 where: {
38 serverId: null
39 }
40 },
41 {
42 model: VideoChannelModel,
43 include: [
44 {
45 model: AccountModel
46 }
47 ]
48 }
49 ]
50 })
51
52 for (const account of accounts) {
53 try {
54 await generateSmallerAvatarIfNeeded(account)
55 } catch (err) {
56 console.error(`Cannot process account avatar ${account.name}`, err)
57 }
58
59 for (const videoChannel of account.VideoChannels) {
60 try {
61 await generateSmallerAvatarIfNeeded(videoChannel)
62 } catch (err) {
63 console.error(`Cannot process channel avatar ${videoChannel.name}`, err)
64 }
65 }
66 }
67
68 console.log('Generation finished!')
69}
70
71async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
72 const avatars = accountOrChannel.Actor.Avatars
73 if (avatars.length !== 1) {
74 return
75 }
76
77 console.log(`Processing ${accountOrChannel.name}.`)
78
79 await generateSmallerAvatar(accountOrChannel.Actor)
80 accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null })
81
82 return sendUpdateActor(accountOrChannel, undefined)
83}
84
85async function generateSmallerAvatar (actor: MActorDefault) {
86 const bigAvatar = getBiggestActorImage(actor.Avatars)
87
88 const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width')
89 const sourceFilename = bigAvatar.filename
90
91 const newImageName = buildUUID() + getLowercaseExtension(sourceFilename)
92 const source = join(CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename)
93 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, newImageName)
94
95 await processImage(source, destination, imageSize, true)
96
97 const actorImageInfo = {
98 name: newImageName,
99 fileUrl: null,
100 height: imageSize.height,
101 width: imageSize.width,
102 onDisk: true
103 }
104
105 await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined)
106}