1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-06 03:50:26 +02:00

Implement avatar miniatures (#4639)

* client: remove unused file

* refactor(client/my-actor-avatar): size from input

Read size from component input instead of scss, to make it possible to
use smaller avatar images when implemented.

* implement avatar miniatures

close #4560

* fix(test): max file size

* fix(search-index): normalize res acc to avatarMini

* refactor avatars to an array

* client/search: resize channel avatar to 120

* refactor(client/videos): remove unused function

* client(actor-avatar): set default size

* fix tests and avatars full result

When findOne is used only an array containting one avatar is returned.

* update migration version and version notations

* server/search: harmonize normalizing

* Cleanup avatar miniature PR

Co-authored-by: Chocobozzz <me@florianbigard.com>
This commit is contained in:
kontrollanten 2022-02-28 08:34:43 +01:00 committed by GitHub
parent 5cad2ca9db
commit d0800f7661
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
150 changed files with 2027 additions and 1276 deletions

View file

@ -1,5 +1,5 @@
import 'multer'
import { queue } from 'async'
import { remove } from 'fs-extra'
import LRUCache from 'lru-cache'
import { join } from 'path'
import { ActorModel } from '@server/models/actor/actor'
@ -13,7 +13,7 @@ import { CONFIG } from '../initializers/config'
import { ACTOR_IMAGES_SIZE, LRU_CACHE, QUEUE_CONCURRENCY, WEBSERVER } from '../initializers/constants'
import { sequelizeTypescript } from '../initializers/database'
import { MAccountDefault, MActor, MChannelDefault } from '../types/models'
import { deleteActorImageInstance, updateActorImageInstance } from './activitypub/actors'
import { deleteActorImages, updateActorImages } from './activitypub/actors'
import { sendUpdateActor } from './activitypub/send'
function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string) {
@ -33,64 +33,69 @@ function buildActorInstance (type: ActivityPubActorType, url: string, preferredU
}) as MActor
}
async function updateLocalActorImageFile (
async function updateLocalActorImageFiles (
accountOrChannel: MAccountDefault | MChannelDefault,
imagePhysicalFile: Express.Multer.File,
type: ActorImageType
) {
const imageSize = type === ActorImageType.AVATAR
? ACTOR_IMAGES_SIZE.AVATARS
: ACTOR_IMAGES_SIZE.BANNERS
const processImageSize = async (imageSize: { width: number, height: number }) => {
const extension = getLowercaseExtension(imagePhysicalFile.filename)
const extension = getLowercaseExtension(imagePhysicalFile.filename)
const imageName = buildUUID() + extension
const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
await processImage(imagePhysicalFile.path, destination, imageSize, true)
const imageName = buildUUID() + extension
const destination = join(CONFIG.STORAGE.ACTOR_IMAGES, imageName)
await processImage(imagePhysicalFile.path, destination, imageSize)
return {
imageName,
imageSize
}
}
return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
const actorImageInfo = {
name: imageName,
fileUrl: null,
height: imageSize.height,
width: imageSize.width,
onDisk: true
}
const processedImages = await Promise.all(ACTOR_IMAGES_SIZE[type].map(processImageSize))
await remove(imagePhysicalFile.path)
const updatedActor = await updateActorImageInstance(accountOrChannel.Actor, type, actorImageInfo, t)
await updatedActor.save({ transaction: t })
return retryTransactionWrapper(() => sequelizeTypescript.transaction(async t => {
const actorImagesInfo = processedImages.map(({ imageName, imageSize }) => ({
name: imageName,
fileUrl: null,
height: imageSize.height,
width: imageSize.width,
onDisk: true
}))
await sendUpdateActor(accountOrChannel, t)
const updatedActor = await updateActorImages(accountOrChannel.Actor, type, actorImagesInfo, t)
await updatedActor.save({ transaction: t })
return type === ActorImageType.AVATAR
? updatedActor.Avatar
: updatedActor.Banner
})
})
await sendUpdateActor(accountOrChannel, t)
return type === ActorImageType.AVATAR
? updatedActor.Avatars
: updatedActor.Banners
}))
}
async function deleteLocalActorImageFile (accountOrChannel: MAccountDefault | MChannelDefault, type: ActorImageType) {
return retryTransactionWrapper(() => {
return sequelizeTypescript.transaction(async t => {
const updatedActor = await deleteActorImageInstance(accountOrChannel.Actor, type, t)
const updatedActor = await deleteActorImages(accountOrChannel.Actor, type, t)
await updatedActor.save({ transaction: t })
await sendUpdateActor(accountOrChannel, t)
return updatedActor.Avatar
return updatedActor.Avatars
})
})
}
type DownloadImageQueueTask = { fileUrl: string, filename: string, type: ActorImageType }
type DownloadImageQueueTask = {
fileUrl: string
filename: string
type: ActorImageType
size: typeof ACTOR_IMAGES_SIZE[ActorImageType][0]
}
const downloadImageQueue = queue<DownloadImageQueueTask, Error>((task, cb) => {
const size = task.type === ActorImageType.AVATAR
? ACTOR_IMAGES_SIZE.AVATARS
: ACTOR_IMAGES_SIZE.BANNERS
downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, size)
downloadImage(task.fileUrl, CONFIG.STORAGE.ACTOR_IMAGES, task.filename, task.size)
.then(() => cb())
.catch(err => cb(err))
}, QUEUE_CONCURRENCY.ACTOR_PROCESS_IMAGE)
@ -110,7 +115,7 @@ const actorImagePathUnsafeCache = new LRUCache<string, string>({ max: LRU_CACHE.
export {
actorImagePathUnsafeCache,
updateLocalActorImageFile,
updateLocalActorImageFiles,
deleteLocalActorImageFile,
pushActorImageProcessInQueue,
buildActorInstance