diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-01 08:50:28 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-01 13:37:40 +0100 |
commit | 7bde625050cb661f51db20992f9f3912a582fcee (patch) | |
tree | 974bbb37e4e435dd5df0fbd1253ed300c12b85db | |
parent | 8d07888728bc5aabc7d0cd6211bc49fc45fd0353 (diff) | |
download | PeerTube-7bde625050cb661f51db20992f9f3912a582fcee.tar.gz PeerTube-7bde625050cb661f51db20992f9f3912a582fcee.tar.zst PeerTube-7bde625050cb661f51db20992f9f3912a582fcee.zip |
Fill width of local avatars
-rw-r--r-- | scripts/migrations/peertube-4.2.ts | 19 | ||||
-rw-r--r-- | server/helpers/image-utils.ts | 22 |
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 @@ | |||
1 | import { minBy } from 'lodash' | 1 | import { minBy } from 'lodash' |
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | import { processImage } from '@server/helpers/image-utils' | 3 | import { getImageSize, processImage } from '@server/helpers/image-utils' |
4 | import { CONFIG } from '@server/initializers/config' | 4 | import { CONFIG } from '@server/initializers/config' |
5 | import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' | 5 | import { ACTOR_IMAGES_SIZE } from '@server/initializers/constants' |
6 | import { updateActorImages } from '@server/lib/activitypub/actors' | 6 | import { 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 | ||
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, avatar.filename)) | ||
81 | avatar.width = width | ||
82 | avatar.height = height | ||
83 | |||
84 | await avatar.save() | ||
85 | } | ||
86 | } | ||
87 | |||
71 | async function generateSmallerAvatarIfNeeded (accountOrChannel: MAccountDefault | MChannelDefault) { | 88 | async 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 @@ | |||
1 | import { copy, readFile, remove, rename } from 'fs-extra' | 1 | import { copy, readFile, remove, rename } from 'fs-extra' |
2 | import Jimp, { read } from 'jimp' | 2 | import Jimp, { read as jimpRead } from 'jimp' |
3 | import { join } from 'path' | 3 | import { join } from 'path' |
4 | import { getLowercaseExtension } from '@shared/core-utils' | 4 | import { getLowercaseExtension } from '@shared/core-utils' |
5 | import { buildUUID } from '@shared/extra-utils' | 5 | import { buildUUID } from '@shared/extra-utils' |
@@ -56,12 +56,26 @@ async function generateImageFromVideoFile (fromPath: string, folder: string, ima | |||
56 | } | 56 | } |
57 | } | 57 | } |
58 | 58 | ||
59 | async 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 | ||
61 | export { | 72 | export { |
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) |