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/helpers/image-utils.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/helpers/image-utils.ts')
-rw-r--r-- | server/helpers/image-utils.ts | 179 |
1 files changed, 0 insertions, 179 deletions
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts deleted file mode 100644 index 2a8bb6e6e..000000000 --- a/server/helpers/image-utils.ts +++ /dev/null | |||
@@ -1,179 +0,0 @@ | |||
1 | import { copy, readFile, remove, rename } from 'fs-extra' | ||
2 | import Jimp, { read as jimpRead } from 'jimp' | ||
3 | import { join } from 'path' | ||
4 | import { ColorActionName } from '@jimp/plugin-color' | ||
5 | import { getLowercaseExtension } from '@shared/core-utils' | ||
6 | import { buildUUID } from '@shared/extra-utils' | ||
7 | import { convertWebPToJPG, generateThumbnailFromVideo, processGIF } from './ffmpeg' | ||
8 | import { logger, loggerTagsFactory } from './logger' | ||
9 | |||
10 | const lTags = loggerTagsFactory('image-utils') | ||
11 | |||
12 | function generateImageFilename (extension = '.jpg') { | ||
13 | return buildUUID() + extension | ||
14 | } | ||
15 | |||
16 | async function processImage (options: { | ||
17 | path: string | ||
18 | destination: string | ||
19 | newSize: { width: number, height: number } | ||
20 | keepOriginal?: boolean // default false | ||
21 | }) { | ||
22 | const { path, destination, newSize, keepOriginal = false } = options | ||
23 | |||
24 | const extension = getLowercaseExtension(path) | ||
25 | |||
26 | if (path === destination) { | ||
27 | throw new Error('Jimp/FFmpeg needs an input path different that the output path.') | ||
28 | } | ||
29 | |||
30 | logger.debug('Processing image %s to %s.', path, destination) | ||
31 | |||
32 | // Use FFmpeg to process GIF | ||
33 | if (extension === '.gif') { | ||
34 | await processGIF({ path, destination, newSize }) | ||
35 | } else { | ||
36 | await jimpProcessor(path, destination, newSize, extension) | ||
37 | } | ||
38 | |||
39 | if (keepOriginal !== true) await remove(path) | ||
40 | } | ||
41 | |||
42 | async function generateImageFromVideoFile (options: { | ||
43 | fromPath: string | ||
44 | folder: string | ||
45 | imageName: string | ||
46 | size: { width: number, height: number } | ||
47 | }) { | ||
48 | const { fromPath, folder, imageName, size } = options | ||
49 | |||
50 | const pendingImageName = 'pending-' + imageName | ||
51 | const pendingImagePath = join(folder, pendingImageName) | ||
52 | |||
53 | try { | ||
54 | await generateThumbnailFromVideo({ fromPath, output: pendingImagePath }) | ||
55 | |||
56 | const destination = join(folder, imageName) | ||
57 | await processImage({ path: pendingImagePath, destination, newSize: size }) | ||
58 | } catch (err) { | ||
59 | logger.error('Cannot generate image from video %s.', fromPath, { err, ...lTags() }) | ||
60 | |||
61 | try { | ||
62 | await remove(pendingImagePath) | ||
63 | } catch (err) { | ||
64 | logger.debug('Cannot remove pending image path after generation error.', { err, ...lTags() }) | ||
65 | } | ||
66 | |||
67 | throw err | ||
68 | } | ||
69 | } | ||
70 | |||
71 | async function getImageSize (path: string) { | ||
72 | const inputBuffer = await readFile(path) | ||
73 | |||
74 | const image = await jimpRead(inputBuffer) | ||
75 | |||
76 | return { | ||
77 | width: image.getWidth(), | ||
78 | height: image.getHeight() | ||
79 | } | ||
80 | } | ||
81 | |||
82 | // --------------------------------------------------------------------------- | ||
83 | |||
84 | export { | ||
85 | generateImageFilename, | ||
86 | generateImageFromVideoFile, | ||
87 | |||
88 | processImage, | ||
89 | |||
90 | getImageSize | ||
91 | } | ||
92 | |||
93 | // --------------------------------------------------------------------------- | ||
94 | |||
95 | async function jimpProcessor (path: string, destination: string, newSize: { width: number, height: number }, inputExt: string) { | ||
96 | let sourceImage: Jimp | ||
97 | const inputBuffer = await readFile(path) | ||
98 | |||
99 | try { | ||
100 | sourceImage = await jimpRead(inputBuffer) | ||
101 | } catch (err) { | ||
102 | logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) | ||
103 | |||
104 | const newName = path + '.jpg' | ||
105 | await convertWebPToJPG({ path, destination: newName }) | ||
106 | await rename(newName, path) | ||
107 | |||
108 | sourceImage = await jimpRead(path) | ||
109 | } | ||
110 | |||
111 | await remove(destination) | ||
112 | |||
113 | // Optimization if the source file has the appropriate size | ||
114 | const outputExt = getLowercaseExtension(destination) | ||
115 | if (skipProcessing({ sourceImage, newSize, imageBytes: inputBuffer.byteLength, inputExt, outputExt })) { | ||
116 | return copy(path, destination) | ||
117 | } | ||
118 | |||
119 | await autoResize({ sourceImage, newSize, destination }) | ||
120 | } | ||
121 | |||
122 | async function autoResize (options: { | ||
123 | sourceImage: Jimp | ||
124 | newSize: { width: number, height: number } | ||
125 | destination: string | ||
126 | }) { | ||
127 | const { sourceImage, newSize, destination } = options | ||
128 | |||
129 | // Portrait mode targeting a landscape, apply some effect on the image | ||
130 | const sourceIsPortrait = sourceImage.getWidth() < sourceImage.getHeight() | ||
131 | const destIsPortraitOrSquare = newSize.width <= newSize.height | ||
132 | |||
133 | removeExif(sourceImage) | ||
134 | |||
135 | if (sourceIsPortrait && !destIsPortraitOrSquare) { | ||
136 | const baseImage = sourceImage.cloneQuiet().cover(newSize.width, newSize.height) | ||
137 | .color([ { apply: ColorActionName.SHADE, params: [ 50 ] } ]) | ||
138 | |||
139 | const topImage = sourceImage.cloneQuiet().contain(newSize.width, newSize.height) | ||
140 | |||
141 | return write(baseImage.blit(topImage, 0, 0), destination) | ||
142 | } | ||
143 | |||
144 | return write(sourceImage.cover(newSize.width, newSize.height), destination) | ||
145 | } | ||
146 | |||
147 | function write (image: Jimp, destination: string) { | ||
148 | return image.quality(80).writeAsync(destination) | ||
149 | } | ||
150 | |||
151 | function skipProcessing (options: { | ||
152 | sourceImage: Jimp | ||
153 | newSize: { width: number, height: number } | ||
154 | imageBytes: number | ||
155 | inputExt: string | ||
156 | outputExt: string | ||
157 | }) { | ||
158 | const { sourceImage, newSize, imageBytes, inputExt, outputExt } = options | ||
159 | const { width, height } = newSize | ||
160 | |||
161 | if (hasExif(sourceImage)) return false | ||
162 | if (sourceImage.getWidth() > width || sourceImage.getHeight() > height) return false | ||
163 | if (inputExt !== outputExt) return false | ||
164 | |||
165 | const kB = 1000 | ||
166 | |||
167 | if (height >= 1000) return imageBytes <= 200 * kB | ||
168 | if (height >= 500) return imageBytes <= 100 * kB | ||
169 | |||
170 | return imageBytes <= 15 * kB | ||
171 | } | ||
172 | |||
173 | function hasExif (image: Jimp) { | ||
174 | return !!(image.bitmap as any).exifBuffer | ||
175 | } | ||
176 | |||
177 | function removeExif (image: Jimp) { | ||
178 | (image.bitmap as any).exifBuffer = null | ||
179 | } | ||