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 /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 'scripts/migrations/peertube-4.2.ts')
-rw-r--r-- | scripts/migrations/peertube-4.2.ts | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/scripts/migrations/peertube-4.2.ts b/scripts/migrations/peertube-4.2.ts deleted file mode 100644 index d8929692b..000000000 --- a/scripts/migrations/peertube-4.2.ts +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
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 | |||
19 | run() | ||
20 | .then(() => process.exit(0)) | ||
21 | .catch(err => { | ||
22 | console.error(err) | ||
23 | process.exit(-1) | ||
24 | }) | ||
25 | |||
26 | async function run () { | ||
27 | console.log('Generate avatar miniatures from existing avatars.') | ||
28 | |||
29 | await initDatabaseModels(true) | ||
30 | JobQueue.Instance.init() | ||
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 fillAvatarSizeIfNeeded(account) | ||
55 | await generateSmallerAvatarIfNeeded(account) | ||
56 | } catch (err) { | ||
57 | console.error(`Cannot process account avatar ${account.name}`, err) | ||
58 | } | ||
59 | |||
60 | for (const videoChannel of account.VideoChannels) { | ||
61 | try { | ||
62 | await fillAvatarSizeIfNeeded(videoChannel) | ||
63 | await generateSmallerAvatarIfNeeded(videoChannel) | ||
64 | } catch (err) { | ||
65 | console.error(`Cannot process channel avatar ${videoChannel.name}`, err) | ||
66 | } | ||
67 | } | ||
68 | } | ||
69 | |||
70 | console.log('Generation finished!') | ||
71 | } | ||
72 | |||
73 | async function fillAvatarSizeIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
74 | const avatars = accountOrChannel.Actor.Avatars | ||
75 | |||
76 | for (const avatar of avatars) { | ||
77 | if (avatar.width && avatar.height) continue | ||
78 | |||
79 | console.log('Filling size of avatars of %s.', accountOrChannel.name) | ||
80 | |||
81 | const { width, height } = await getImageSize(join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, avatar.filename)) | ||
82 | avatar.width = width | ||
83 | avatar.height = height | ||
84 | |||
85 | await avatar.save() | ||
86 | } | ||
87 | } | ||
88 | |||
89 | async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | ||
90 | const avatars = accountOrChannel.Actor.Avatars | ||
91 | if (avatars.length !== 1) { | ||
92 | return | ||
93 | } | ||
94 | |||
95 | console.log(`Processing ${accountOrChannel.name}.`) | ||
96 | |||
97 | await generateSmallerAvatar(accountOrChannel.Actor) | ||
98 | accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null }) | ||
99 | |||
100 | return sendUpdateActor(accountOrChannel, undefined) | ||
101 | } | ||
102 | |||
103 | async function generateSmallerAvatar (actor: MActorDefault) { | ||
104 | const bigAvatar = getBiggestActorImage(actor.Avatars) | ||
105 | |||
106 | const imageSize = minBy(ACTOR_IMAGES_SIZE[ActorImageType.AVATAR], 'width') | ||
107 | const sourceFilename = bigAvatar.filename | ||
108 | |||
109 | const newImageName = buildUUID() + getLowercaseExtension(sourceFilename) | ||
110 | const source = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, sourceFilename) | ||
111 | const destination = join(CONFIG.STORAGE.ACTOR_IMAGES_DIR, newImageName) | ||
112 | |||
113 | await processImage({ path: source, destination, newSize: imageSize, keepOriginal: true }) | ||
114 | |||
115 | const actorImageInfo = { | ||
116 | name: newImageName, | ||
117 | fileUrl: null, | ||
118 | height: imageSize.height, | ||
119 | width: imageSize.width, | ||
120 | onDisk: true | ||
121 | } | ||
122 | |||
123 | await updateActorImages(actor, ActorImageType.AVATAR, [ actorImageInfo ], undefined) | ||
124 | } | ||