aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2022-06-27 11:53:12 +0200
committerChocobozzz <me@florianbigard.com>2022-06-27 11:53:12 +0200
commit3a54605d4e7ec5b4f47131e8d23255be35b7beac (patch)
treefce9d34812a7638d4a0253b076f05aabd15a2ce9 /server/lib
parent88edc66edadcab1b0372679e23bf2a7a6ff50131 (diff)
downloadPeerTube-3a54605d4e7ec5b4f47131e8d23255be35b7beac.tar.gz
PeerTube-3a54605d4e7ec5b4f47131e8d23255be35b7beac.tar.zst
PeerTube-3a54605d4e7ec5b4f47131e8d23255be35b7beac.zip
Process images in a dedicated worker
Diffstat (limited to 'server/lib')
-rw-r--r--server/lib/local-actor.ts5
-rw-r--r--server/lib/thumbnail.ts27
-rw-r--r--server/lib/worker/parent-process.ts16
-rw-r--r--server/lib/worker/workers/image-downloader.ts2
-rw-r--r--server/lib/worker/workers/image-processor.ts7
5 files changed, 47 insertions, 10 deletions
diff --git a/server/lib/local-actor.ts b/server/lib/local-actor.ts
index e3b04c094..1d9be76e2 100644
--- a/server/lib/local-actor.ts
+++ b/server/lib/local-actor.ts
@@ -6,14 +6,13 @@ import { getLowercaseExtension } from '@shared/core-utils'
6import { buildUUID } from '@shared/extra-utils' 6import { buildUUID } from '@shared/extra-utils'
7import { ActivityPubActorType, ActorImageType } from '@shared/models' 7import { ActivityPubActorType, ActorImageType } from '@shared/models'
8import { retryTransactionWrapper } from '../helpers/database-utils' 8import { retryTransactionWrapper } from '../helpers/database-utils'
9import { processImage } from '../helpers/image-utils'
10import { CONFIG } from '../initializers/config' 9import { CONFIG } from '../initializers/config'
11import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants' 10import { ACTOR_IMAGES_SIZE, LRU_CACHE, WEBSERVER } from '../initializers/constants'
12import { sequelizeTypescript } from '../initializers/database' 11import { sequelizeTypescript } from '../initializers/database'
13import { MAccountDefault, MActor, MChannelDefault } from '../types/models' 12import { MAccountDefault, MActor, MChannelDefault } from '../types/models'
14import { deleteActorImages, updateActorImages } from './activitypub/actors' 13import { deleteActorImages, updateActorImages } from './activitypub/actors'
15import { sendUpdateActor } from './activitypub/send' 14import { sendUpdateActor } from './activitypub/send'
16import { downloadImageFromWorker } from './worker/parent-process' 15import { downloadImageFromWorker, processImageFromWorker } from './worker/parent-process'
17 16
18function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) { 17function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) {
19 return new ActorModel({ 18 return new ActorModel({
@@ -42,7 +41,7 @@ async function updateLocalActorImageFiles (
42 41
43 const imageName = buildUUID() + extension 42 const imageName = buildUUID() + extension
44 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName) 43 const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
45 await processImage(imagePhysicalFile.path, destination, imageSize, true) 44 await processImageFromWorker({ path: imagePhysicalFile.path, destination, newSize: imageSize, keepOriginal: true })
46 45
47 return { 46 return {
48 imageName, 47 imageName,
diff --git a/server/lib/thumbnail.ts b/server/lib/thumbnail.ts
index f00c87623..02b867a91 100644
--- a/server/lib/thumbnail.ts
+++ b/server/lib/thumbnail.ts
@@ -1,6 +1,6 @@
1import { join } from 'path' 1import { join } from 'path'
2import { ThumbnailType } from '@shared/models' 2import { ThumbnailType } from '@shared/models'
3import { generateImageFilename, generateImageFromVideoFile, processImage } from '../helpers/image-utils' 3import { generateImageFilename, generateImageFromVideoFile } from '../helpers/image-utils'
4import { CONFIG } from '../initializers/config' 4import { CONFIG } from '../initializers/config'
5import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants' 5import { ASSETS_PATH, PREVIEWS_SIZE, THUMBNAILS_SIZE } from '../initializers/constants'
6import { ThumbnailModel } from '../models/video/thumbnail' 6import { ThumbnailModel } from '../models/video/thumbnail'
@@ -9,6 +9,7 @@ import { MThumbnail } from '../types/models/video/thumbnail'
9import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist' 9import { MVideoPlaylistThumbnail } from '../types/models/video/video-playlist'
10import { downloadImageFromWorker } from './local-actor' 10import { downloadImageFromWorker } from './local-actor'
11import { VideoPathManager } from './video-path-manager' 11import { VideoPathManager } from './video-path-manager'
12import { processImageFromWorker } from './worker/parent-process'
12 13
13type ImageSize = { height?: number, width?: number } 14type ImageSize = { height?: number, width?: number }
14 15
@@ -23,7 +24,10 @@ function updatePlaylistMiniatureFromExisting (options: {
23 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size) 24 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size)
24 const type = ThumbnailType.MINIATURE 25 const type = ThumbnailType.MINIATURE
25 26
26 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) 27 const thumbnailCreator = () => {
28 return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
29 }
30
27 return updateThumbnailFromFunction({ 31 return updateThumbnailFromFunction({
28 thumbnailCreator, 32 thumbnailCreator,
29 filename, 33 filename,
@@ -99,7 +103,10 @@ function updateVideoMiniatureFromExisting (options: {
99 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options 103 const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options
100 104
101 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size) 105 const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size)
102 const thumbnailCreator = () => processImage(inputPath, outputPath, { width, height }, keepOriginal) 106
107 const thumbnailCreator = () => {
108 return processImageFromWorker({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal })
109 }
103 110
104 return updateThumbnailFromFunction({ 111 return updateThumbnailFromFunction({
105 thumbnailCreator, 112 thumbnailCreator,
@@ -123,8 +130,18 @@ function generateVideoMiniature (options: {
123 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type) 130 const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type)
124 131
125 const thumbnailCreator = videoFile.isAudio() 132 const thumbnailCreator = videoFile.isAudio()
126 ? () => processImage(ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND, outputPath, { width, height }, true) 133 ? () => processImageFromWorker({
127 : () => generateImageFromVideoFile(input, basePath, filename, { height, width }) 134 path: ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
135 destination: outputPath,
136 newSize: { width, height },
137 keepOriginal: true
138 })
139 : () => generateImageFromVideoFile({
140 fromPath: input,
141 folder: basePath,
142 imageName: filename,
143 size: { height, width }
144 })
128 145
129 return updateThumbnailFromFunction({ 146 return updateThumbnailFromFunction({
130 thumbnailCreator, 147 thumbnailCreator,
diff --git a/server/lib/worker/parent-process.ts b/server/lib/worker/parent-process.ts
index 18dabd97f..188001677 100644
--- a/server/lib/worker/parent-process.ts
+++ b/server/lib/worker/parent-process.ts
@@ -2,6 +2,7 @@ import { join } from 'path'
2import Piscina from 'piscina' 2import Piscina from 'piscina'
3import { WORKER_THREADS } from '@server/initializers/constants' 3import { WORKER_THREADS } from '@server/initializers/constants'
4import { downloadImage } from './workers/image-downloader' 4import { downloadImage } from './workers/image-downloader'
5import { processImage } from '@server/helpers/image-utils'
5 6
6const downloadImagerWorker = new Piscina({ 7const downloadImagerWorker = new Piscina({
7 filename: join(__dirname, 'workers', 'image-downloader.js'), 8 filename: join(__dirname, 'workers', 'image-downloader.js'),
@@ -13,6 +14,19 @@ function downloadImageFromWorker (options: Parameters<typeof downloadImage>[0]):
13 return downloadImagerWorker.run(options) 14 return downloadImagerWorker.run(options)
14} 15}
15 16
17// ---------------------------------------------------------------------------
18
19const processImageWorker = new Piscina({
20 filename: join(__dirname, 'workers', 'image-processor.js'),
21 concurrentTasksPerWorker: WORKER_THREADS.DOWNLOAD_IMAGE.CONCURRENCY,
22 maxThreads: WORKER_THREADS.DOWNLOAD_IMAGE.MAX_THREADS
23})
24
25function processImageFromWorker (options: Parameters<typeof processImage>[0]): Promise<ReturnType<typeof processImage>> {
26 return processImageWorker.run(options)
27}
28
16export { 29export {
17 downloadImageFromWorker 30 downloadImageFromWorker,
31 processImageFromWorker
18} 32}
diff --git a/server/lib/worker/workers/image-downloader.ts b/server/lib/worker/workers/image-downloader.ts
index 8d4a6b37e..4b32f723e 100644
--- a/server/lib/worker/workers/image-downloader.ts
+++ b/server/lib/worker/workers/image-downloader.ts
@@ -18,7 +18,7 @@ async function downloadImage (options: {
18 const destPath = join(destDir, destName) 18 const destPath = join(destDir, destName)
19 19
20 try { 20 try {
21 await processImage(tmpPath, destPath, size) 21 await processImage({ path: tmpPath, destination: destPath, newSize: size })
22 } catch (err) { 22 } catch (err) {
23 await remove(tmpPath) 23 await remove(tmpPath)
24 24
diff --git a/server/lib/worker/workers/image-processor.ts b/server/lib/worker/workers/image-processor.ts
new file mode 100644
index 000000000..0ab41a5a0
--- /dev/null
+++ b/server/lib/worker/workers/image-processor.ts
@@ -0,0 +1,7 @@
1import { processImage } from '@server/helpers/image-utils'
2
3module.exports = processImage
4
5export {
6 processImage
7}