diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/scripts/migrations/peertube-4.2.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/scripts/migrations/peertube-4.2.ts')
-rw-r--r-- | server/scripts/migrations/peertube-4.2.ts | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/server/scripts/migrations/peertube-4.2.ts b/server/scripts/migrations/peertube-4.2.ts new file mode 100644 index 000000000..6c89ee39e --- /dev/null +++ b/server/scripts/migrations/peertube-4.2.ts | |||
@@ -0,0 +1,123 @@ | |||
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 | |||
18 | run() | ||
19 | .then(() => process.exit(0)) | ||
20 | .catch(err => { | ||
21 | console.error(err) | ||
22 | process.exit(-1) | ||
23 | }) | ||
24 | |||
25 | async function run () { | ||
26 | console.log('Generate avatar miniatures from existing avatars.') | ||
27 | |||
28 | await initDatabaseModels(true) | ||
29 | JobQueue.Instance.init() | ||
30 | |||
31 | const accounts: AccountModel[] = await AccountModel.findAll({ | ||
32 | include: [ | ||
33 | { | ||
34 | model: ActorModel, | ||
35 | required: true, | ||
36 | where: { | ||
37 | serverId: null | ||
38 | } | ||
39 | }, | ||
40 | { | ||
41 | model: VideoChannelModel, | ||
42 | include: [ | ||
43 | { | ||
44 | model: AccountModel | ||
45 | } | ||
46 | ] | ||
47 | } | ||
48 | ] | ||
49 | }) | ||
50 | |||
51 | for (const account of accounts) { | ||
52 | try { | ||
53 | await fillAvatarSizeIfNeeded(account) | ||
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 fillAvatarSizeIfNeeded(videoChannel) | ||
62 | await generateSmallerAvatarIfNeeded(videoChannel) | ||
63 | } catch (err) { | ||
64 | console.error(`Cannot process channel avatar ${videoChannel.name}`, err) | ||
65 | } | ||
66 | } | ||
67 | } | ||
68 | |||
69 | console.log('Generation finished!') | ||
70 | } | ||
71 | |||
72 | async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
73 | const avatars = accountOrChannel.Actor.Avatars | ||
74 | |||
75 | for (const avatar of avatars) { | ||
76 | if (avatar.width && avatar.height) continue | ||
77 | |||
78 | console.log('Filling size of avatars of %s.', accountOrChannel.name) | ||
79 | |||
80 | const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, avatar.filename)) | ||
81 | avatar.width = width | ||
82 | avatar.height = height | ||
83 | |||
84 | await avatar.save() | ||
85 | } | ||
86 | } | ||
87 | |||
88 | async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
89 | const avatars = accountOrChannel.Actor.Avatars | ||
90 | if (avatars.length !== 1) { | ||
91 | return | ||
92 | } | ||
93 | |||
94 | console.log(`Processing ${accountOrChannel.name}.`) | ||
95 | |||
96 | await generateSmallerAvatar(accountOrChannel.Actor) | ||
97 | accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null }) | ||
98 | |||
99 | return sendUpdateActor(accountOrChannel, undefined) | ||
100 | } | ||
101 | |||
102 | async function generateSmallerAvatar (actor: MActorDefault) { | ||
103 | const bigAvatar = getBiggestActorImage(actor.Avatars) | ||
104 | |||
105 | const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width') | ||
106 | const sourceFilename = bigAvatar.filename | ||
107 | |||
108 | const newImageName = buildUUID() + getLowercaseExtension(sourceFilename) | ||
109 | const source = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, sourceFilename) | ||
110 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, newImageName) | ||
111 | |||
112 | await processImage({ path: source, destination, newSize: imageSize, keepOriginal: true }) | ||
113 | |||
114 | const actorImageInfo = { | ||
115 | name: newImageName, | ||
116 | fileUrl: null, | ||
117 | height: imageSize.height, | ||
118 | width: imageSize.width, | ||
119 | onDisk: true | ||
120 | } | ||
121 | |||
122 | await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined) | ||
123 | } | ||