feat(services): add PUT /guilds/[guildId]/icon

This commit is contained in:
Divlo
2021-10-26 14:01:49 +00:00
parent 14eac3cadb
commit 56c613b5cf
21 changed files with 233 additions and 79 deletions

View File

@ -10,7 +10,7 @@ const parametersGetUserSchema = Type.Object({
userId: userPublicSchema.id
})
export type ParametersGetUser = Static<typeof parametersGetUserSchema>
type ParametersGetUser = Static<typeof parametersGetUserSchema>
const getServiceSchema: FastifySchema = {
description: 'GET the public user informations with its id',

View File

@ -1,19 +1,11 @@
import fs from 'node:fs'
import { URL } from 'node:url'
import { randomUUID } from 'node:crypto'
import { Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import authenticateUser from '../../../../tools/plugins/authenticateUser.js'
import { fastifyErrors } from '../../../../models/utils.js'
import fastifyMultipart, { Multipart } from 'fastify-multipart'
import {
maximumImageSize,
supportedImageMimetype,
ROOT_URL
} from '../../../../tools/configurations'
import fastifyMultipart from 'fastify-multipart'
import prisma from '../../../../tools/database/prisma.js'
import { uploadImage } from '../../../../tools/utils/uploadImage.js'
const putServiceSchema: FastifySchema = {
description: 'Edit the current connected user logo',
@ -52,36 +44,11 @@ export const putCurrentUserLogo: FastifyPluginAsync = async (fastify) => {
if (request.user == null) {
throw fastify.httpErrors.forbidden()
}
let files: Multipart[] = []
try {
files = await request.saveRequestFiles({
limits: {
files: 1,
fileSize: maximumImageSize * 1024 * 1024
}
})
} catch (error) {
throw fastify.httpErrors.requestHeaderFieldsTooLarge(
`body.logo should be less than ${maximumImageSize}mb.`
)
}
if (files.length !== 1) {
throw fastify.httpErrors.badRequest('You must upload at most one file.')
}
const image = files[0]
if (!supportedImageMimetype.includes(image.mimetype)) {
throw fastify.httpErrors.badRequest(
`The file must have a valid type (${supportedImageMimetype.join(
', '
)}).`
)
}
const splitedMimetype = image.mimetype.split('/')
const imageExtension = splitedMimetype[1]
const logoPath = `uploads/users/${randomUUID()}.${imageExtension}`
const logoURL = new URL(logoPath, ROOT_URL)
const logo = `/${logoPath}`
await fs.promises.copyFile(image.filepath, logoURL)
const logo = await uploadImage({
fastify,
request,
folderInUploadsFolder: 'users'
})
await prisma.user.update({
where: { id: request.user.current.id },
data: { logo }

View File

@ -10,6 +10,7 @@ import { userCurrentSchema, userSchema } from '../../../models/User.js'
import { sendEmail } from '../../../tools/email/sendEmail.js'
import { HOST, PORT } from '../../../tools/configurations/index.js'
import { Language, Theme } from '../../../models/UserSettings.js'
import { parseStringNullish } from '../../../tools/utils/parseStringNullish.js'
const bodyPutServiceSchema = Type.Object({
name: Type.Optional(userSchema.name),
@ -117,9 +118,12 @@ export const putCurrentUser: FastifyPluginAsync = async (fastify) => {
where: { id: request.user.current.id },
data: {
name: name ?? request.user.current.name,
status: status ?? request.user.current.status,
biography: biography ?? request.user.current.biography,
website: website ?? request.user.current.website
status: parseStringNullish(request.user.current.status, status),
biography: parseStringNullish(
request.user.current.biography,
biography
),
website: parseStringNullish(request.user.current.website, website)
}
})
reply.statusCode = 200