aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--scripts/migrations/peertube-4.2.ts19
-rw-r--r--server/helpers/image-utils.ts22
2 files changed, 36 insertions, 5 deletions
diff --git a/scripts/migrations/peertube-4.2.ts b/scripts/migrations/peertube-4.2.ts
index 045c3e511..22f9ff9f0 100644
--- a/scripts/migrations/peertube-4.2.ts
+++ b/scripts/migrations/peertube-4.2.ts
@@ -1,6 +1,6 @@
1import { minBy } from 'lodash' 1import { minBy } from 'lodash'
2import { join } from 'path' 2import { join } from 'path'
3import { processImage } from '@server/helpers/image-utils' 3import { getImageSize, processImage } from '@server/helpers/image-utils'
4import { CONFIG } from '@server/initializers/config' 4import { CONFIG } from '@server/initializers/config'
5import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' 5import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants'
6import { updateActorImages } from '@server/lib/activitypub/actors' 6import { updateActorImages } from '@server/lib/activitypub/actors'
@@ -51,6 +51,7 @@ async function run () {
51 51
52 for (const account of accounts) { 52 for (const account of accounts) {
53 try { 53 try {
54 await fillAvatarSizeIfNeeded(account)
54 await generateSmallerAvatarIfNeeded(account) 55 await generateSmallerAvatarIfNeeded(account)
55 } catch (err) { 56 } catch (err) {
56 console.error(`Cannot process account avatar ${account.name}`, err) 57 console.error(`Cannot process account avatar ${account.name}`, err)
@@ -68,6 +69,22 @@ async function run () {
68 console.log('Generation finished!') 69 console.log('Generation finished!')
69} 70}
70 71
72async 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, avatar.filename))
81 avatar.width = width
82 avatar.height = height
83
84 await avatar.save()
85 }
86}
87
71async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { 88async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) {
72 const avatars = accountOrChannel.Actor.Avatars 89 const avatars = accountOrChannel.Actor.Avatars
73 if (avatars.length !== 1) { 90 if (avatars.length !== 1) {
diff --git a/server/helpers/image-utils.ts b/server/helpers/image-utils.ts
index 6e4a2b000..9d0c09051 100644
--- a/server/helpers/image-utils.ts
+++ b/server/helpers/image-utils.ts
@@ -1,5 +1,5 @@
1import { copy, readFile, remove, rename } from 'fs-extra' 1import { copy, readFile, remove, rename } from 'fs-extra'
2import Jimp, { read } from 'jimp' 2import Jimp, { read as jimpRead } from 'jimp'
3import { join } from 'path' 3import { join } from 'path'
4import { getLowercaseExtension } from '@shared/core-utils' 4import { getLowercaseExtension } from '@shared/core-utils'
5import { buildUUID } from '@shared/extra-utils' 5import { buildUUID } from '@shared/extra-utils'
@@ -56,12 +56,26 @@ async function generateImageFromVideoFile (fromPath: string, folder: string, ima
56 } 56 }
57} 57}
58 58
59async function getImageSize (path: string) {
60 const inputBuffer = await readFile(path)
61
62 const image = await jimpRead(inputBuffer)
63
64 return {
65 width: image.getWidth(),
66 height: image.getHeight()
67 }
68}
69
59// --------------------------------------------------------------------------- 70// ---------------------------------------------------------------------------
60 71
61export { 72export {
62 generateImageFilename, 73 generateImageFilename,
63 generateImageFromVideoFile, 74 generateImageFromVideoFile,
64 processImage 75
76 processImage,
77
78 getImageSize
65} 79}
66 80
67// --------------------------------------------------------------------------- 81// ---------------------------------------------------------------------------
@@ -71,7 +85,7 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
71 const inputBuffer = await readFile(path) 85 const inputBuffer = await readFile(path)
72 86
73 try { 87 try {
74 sourceImage = await read(inputBuffer) 88 sourceImage = await jimpRead(inputBuffer)
75 } catch (err) { 89 } catch (err) {
76 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err }) 90 logger.debug('Cannot read %s with jimp. Try to convert the image using ffmpeg first.', path, { err })
77 91
@@ -79,7 +93,7 @@ async function jimpProcessor (path: string, destination: string, newSize: { widt
79 await convertWebPToJPG(path, newName) 93 await convertWebPToJPG(path, newName)
80 await rename(newName, path) 94 await rename(newName, path)
81 95
82 sourceImage = await read(path) 96 sourceImage = await jimpRead(path)
83 } 97 }
84 98
85 await remove(destination) 99 await remove(destination)