feat(services): add PUT /guilds/[guildId]/icon
This commit is contained in:
@ -5,7 +5,7 @@ import { OAuthStrategy } from '../OAuthStrategy.js'
|
||||
|
||||
const oauthStrategy = new OAuthStrategy('discord')
|
||||
|
||||
describe('/utils/OAuthStrategy - callbackSignin', () => {
|
||||
describe('/tools/utils/OAuthStrategy - callbackSignin', () => {
|
||||
it('should signup the user', async () => {
|
||||
const name = 'Martin'
|
||||
const id = '12345'
|
||||
@ -52,7 +52,7 @@ describe('/utils/OAuthStrategy - callbackSignin', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('/utils/OAuthStrategy - callbackAddStrategy', () => {
|
||||
describe('/tools/utils/OAuthStrategy - callbackAddStrategy', () => {
|
||||
it('should add the strategy to the user', async () => {
|
||||
const name = userExample.name
|
||||
const id = '12345'
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { buildQueryURL } from '../buildQueryURL.js'
|
||||
|
||||
test('controllers/users/utils/buildQueryUrl', () => {
|
||||
test('/tools/utils/buildQueryUrl', () => {
|
||||
expect(
|
||||
buildQueryURL('http://localhost:8080', {
|
||||
test: 'query'
|
||||
|
17
src/tools/utils/__test__/parseStringNullish.test.ts
Normal file
17
src/tools/utils/__test__/parseStringNullish.test.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { parseStringNullish } from '../parseStringNullish'
|
||||
|
||||
const defaultString = 'defaultString'
|
||||
|
||||
describe('/tools/utils/parseStringNullish', () => {
|
||||
it('returns `null` if `string.length === 0`', () => {
|
||||
expect(parseStringNullish(defaultString, '')).toEqual(null)
|
||||
})
|
||||
|
||||
it('returns `defaultString` if `string == null`', () => {
|
||||
expect(parseStringNullish(defaultString)).toEqual(defaultString)
|
||||
})
|
||||
|
||||
it('returns `string` if `string.length > 0`', () => {
|
||||
expect(parseStringNullish(defaultString, 'string')).toEqual('string')
|
||||
})
|
||||
})
|
21
src/tools/utils/parseStringNullish.ts
Normal file
21
src/tools/utils/parseStringNullish.ts
Normal file
@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Parse a nullish string:
|
||||
* - if `string.length === 0`, it returns `null`
|
||||
* - if `string == null`, it returns `defaultString`
|
||||
* - if `string.length > 0`, it returns `string`
|
||||
* @param defaultString
|
||||
* @param string
|
||||
* @returns
|
||||
*/
|
||||
export const parseStringNullish = (
|
||||
defaultString: string | null,
|
||||
string?: string
|
||||
): string | null => {
|
||||
if (string != null) {
|
||||
if (string.length > 0) {
|
||||
return string
|
||||
}
|
||||
return null
|
||||
}
|
||||
return defaultString
|
||||
}
|
54
src/tools/utils/uploadImage.ts
Normal file
54
src/tools/utils/uploadImage.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import fs from 'node:fs'
|
||||
import { URL } from 'node:url'
|
||||
import { randomUUID } from 'node:crypto'
|
||||
|
||||
import { FastifyInstance, FastifyRequest } from 'fastify'
|
||||
|
||||
import { Multipart } from 'fastify-multipart'
|
||||
|
||||
import {
|
||||
maximumImageSize,
|
||||
supportedImageMimetype,
|
||||
ROOT_URL
|
||||
} from '../configurations'
|
||||
|
||||
export interface UploadImageOptions {
|
||||
folderInUploadsFolder: 'guilds' | 'messages' | 'users'
|
||||
request: FastifyRequest
|
||||
fastify: FastifyInstance
|
||||
}
|
||||
|
||||
export const uploadImage = async (
|
||||
options: UploadImageOptions
|
||||
): Promise<string> => {
|
||||
const { fastify, request, folderInUploadsFolder } = options
|
||||
let files: Multipart[] = []
|
||||
try {
|
||||
files = await request.saveRequestFiles({
|
||||
limits: {
|
||||
files: 1,
|
||||
fileSize: maximumImageSize * 1024 * 1024
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
throw fastify.httpErrors.requestHeaderFieldsTooLarge(
|
||||
`Image 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 imagePath = `uploads/${folderInUploadsFolder}/${randomUUID()}.${imageExtension}`
|
||||
const imageURL = new URL(imagePath, ROOT_URL)
|
||||
const imagePathToStoreInDatabase = `/${imagePath}`
|
||||
await fs.promises.copyFile(image.filepath, imageURL)
|
||||
return imagePathToStoreInDatabase
|
||||
}
|
Reference in New Issue
Block a user