4 Commits

17 changed files with 3103 additions and 2330 deletions

View File

@ -30,6 +30,7 @@
[ [
"@saithodev/semantic-release-backmerge", "@saithodev/semantic-release-backmerge",
{ {
"branches": [{ "from": "master", "to": "develop" }],
"backmergeStrategy": "merge" "backmergeStrategy": "merge"
} }
] ]

5136
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{ {
"name": "@thream/file-uploads-api", "name": "@thream/file-uploads-api",
"version": "1.0.0", "version": "1.1.0",
"description": "Thream's application programming interface to upload files.", "description": "Thream's application programming interface to upload files.",
"private": true, "private": true,
"type": "module", "type": "module",
@ -27,48 +27,48 @@
"postinstall": "husky install" "postinstall": "husky install"
}, },
"dependencies": { "dependencies": {
"@sinclair/typebox": "0.23.4", "@sinclair/typebox": "0.23.5",
"dotenv": "16.0.0", "dotenv": "16.0.1",
"fastify": "3.28.0", "fastify": "4.2.0",
"fastify-cors": "6.0.3", "@fastify/cors": "8.0.0",
"fastify-helmet": "7.0.1", "@fastify/helmet": "9.1.0",
"fastify-multipart": "5.3.1", "@fastify/multipart": "7.1.0",
"fastify-plugin": "3.0.1", "fastify-plugin": "3.0.1",
"fastify-rate-limit": "5.8.0", "@fastify/rate-limit": "7.0.0",
"fastify-sensible": "3.1.2", "@fastify/sensible": "5.1.0",
"fastify-static": "4.6.1", "@fastify/static": "6.4.0",
"fastify-swagger": "5.1.0", "@fastify/swagger": "7.4.0",
"http-errors": "2.0.0", "http-errors": "2.0.0",
"read-pkg": "7.1.0" "read-pkg": "7.1.0"
}, },
"devDependencies": { "devDependencies": {
"@commitlint/cli": "16.2.3", "@commitlint/cli": "17.0.3",
"@commitlint/config-conventional": "16.2.1", "@commitlint/config-conventional": "17.0.3",
"@saithodev/semantic-release-backmerge": "2.1.2", "@saithodev/semantic-release-backmerge": "2.1.2",
"@semantic-release/git": "10.0.1", "@semantic-release/git": "10.0.1",
"@swc/cli": "0.1.57", "@swc/cli": "0.1.57",
"@swc/core": "1.2.164", "@swc/core": "1.2.207",
"@types/busboy": "1.5.0", "@types/busboy": "1.5.0",
"@types/http-errors": "1.8.2", "@types/http-errors": "1.8.2",
"@types/node": "17.0.23", "@types/node": "18.0.0",
"@typescript-eslint/eslint-plugin": "5.18.0", "@typescript-eslint/eslint-plugin": "5.30.0",
"concurrently": "7.1.0", "concurrently": "7.2.2",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"editorconfig-checker": "4.0.2", "editorconfig-checker": "4.0.2",
"eslint": "8.12.0", "eslint": "8.18.0",
"eslint-config-conventions": "2.0.0", "eslint-config-conventions": "2.0.0",
"eslint-config-prettier": "8.5.0", "eslint-config-prettier": "8.5.0",
"eslint-plugin-import": "2.26.0", "eslint-plugin-import": "2.26.0",
"eslint-plugin-prettier": "4.0.0", "eslint-plugin-prettier": "4.1.0",
"eslint-plugin-promise": "6.0.0", "eslint-plugin-promise": "6.0.0",
"eslint-plugin-unicorn": "42.0.0", "eslint-plugin-unicorn": "42.0.0",
"husky": "7.0.4", "husky": "8.0.1",
"lint-staged": "12.3.7", "lint-staged": "13.0.3",
"markdownlint-cli": "0.31.1", "markdownlint-cli": "0.31.1",
"nodemon": "2.0.15", "nodemon": "2.0.18",
"prettier": "2.6.2", "prettier": "2.7.1",
"rimraf": "3.0.2", "rimraf": "3.0.2",
"semantic-release": "19.0.2", "semantic-release": "19.0.3",
"typescript": "4.6.3" "typescript": "4.7.4"
} }
} }

View File

@ -2,12 +2,12 @@ import { fileURLToPath } from 'node:url'
import dotenv from 'dotenv' import dotenv from 'dotenv'
import fastify from 'fastify' import fastify from 'fastify'
import fastifyCors from 'fastify-cors' import fastifyCors from '@fastify/cors'
import fastifySwagger from 'fastify-swagger' import fastifySwagger from '@fastify/swagger'
import fastifyHelmet from 'fastify-helmet' import fastifyHelmet from '@fastify/helmet'
import fastifyRateLimit from 'fastify-rate-limit' import fastifyRateLimit from '@fastify/rate-limit'
import fastifySensible from 'fastify-sensible' import fastifySensible from '@fastify/sensible'
import fastifyStatic from 'fastify-static' import fastifyStatic from '@fastify/static'
import { services } from './services/index.js' import { services } from './services/index.js'
import { swaggerOptions } from './tools/configurations/swaggerOptions.js' import { swaggerOptions } from './tools/configurations/swaggerOptions.js'
@ -18,7 +18,11 @@ export const application = fastify({
logger: process.env.NODE_ENV === 'development', logger: process.env.NODE_ENV === 'development',
ajv: { ajv: {
customOptions: { customOptions: {
format: 'full' strict: 'log',
keywords: ['kind', 'modifier'],
formats: {
full: true
}
} }
} }
}) })

View File

@ -1,5 +1,8 @@
import { application } from './application.js' import { application } from './application.js'
import { HOST, PORT } from './tools/configurations/index.js' import { HOST, PORT } from './tools/configurations/index.js'
const address = await application.listen(PORT, HOST) const address = await application.listen({
port: PORT,
host: HOST
})
console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`) console.log('\u001B[36m%s\u001B[0m', `🚀 Server listening at ${address}`)

View File

@ -0,0 +1,48 @@
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { Type } from '@sinclair/typebox'
import { fastifyErrors } from '../../../models/utils.js'
import verifyAPIKey from '../../../tools/plugins/verifyAPIKey.js'
import {
DeleteParameters,
deleteParameters,
deleteUploadedFile
} from '../../../tools/utils/deleteUploadedFile.js'
export const deleteServiceSchema: FastifySchema = {
tags: ['guilds'] as string[],
security: [
{
apiKeyAuth: []
}
] as Array<{ [key: string]: [] }>,
params: deleteParameters,
response: {
200: Type.String(),
400: fastifyErrors[400],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const deleteGuildsUploadsService: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(verifyAPIKey)
await fastify.route<{
Params: DeleteParameters
}>({
method: 'DELETE',
url: '/uploads/guilds/:file',
schema: deleteServiceSchema,
handler: async (request, reply) => {
return await deleteUploadedFile({
fastify,
request,
reply,
folder: 'guilds'
})
}
})
}

View File

@ -1,6 +1,6 @@
import { Type } from '@sinclair/typebox' import { Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify' import { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from 'fastify-multipart' import fastifyMultipart from '@fastify/multipart'
import { fastifyErrors } from '../../../models/utils.js' import { fastifyErrors } from '../../../models/utils.js'
import { uploadFile } from '../../../tools/utils/uploadFile.js' import { uploadFile } from '../../../tools/utils/uploadFile.js'

View File

@ -1,19 +1,25 @@
import { FastifyPluginAsync } from 'fastify' import { FastifyPluginAsync } from 'fastify'
import { deleteGuildsUploadsService } from './guilds/delete.js'
import { getGuildsUploadsService } from './guilds/get.js' import { getGuildsUploadsService } from './guilds/get.js'
import { postGuildsUploadsIconService } from './guilds/post.js' import { postGuildsUploadsIconService } from './guilds/post.js'
import { deleteMessagesUploadsService } from './messages/delete.js'
import { getMessagesUploadsService } from './messages/get.js' import { getMessagesUploadsService } from './messages/get.js'
import { postMessagesUploadsService } from './messages/post.js' import { postMessagesUploadsService } from './messages/post.js'
import { deleteUsersUploadsService } from './users/delete.js'
import { getUsersUploadsService } from './users/get.js' import { getUsersUploadsService } from './users/get.js'
import { postUsersUploadsLogoService } from './users/post.js' import { postUsersUploadsLogoService } from './users/post.js'
export const uploadsService: FastifyPluginAsync = async (fastify) => { export const uploadsService: FastifyPluginAsync = async (fastify) => {
await fastify.register(deleteGuildsUploadsService)
await fastify.register(getGuildsUploadsService) await fastify.register(getGuildsUploadsService)
await fastify.register(postGuildsUploadsIconService) await fastify.register(postGuildsUploadsIconService)
await fastify.register(deleteMessagesUploadsService)
await fastify.register(getMessagesUploadsService) await fastify.register(getMessagesUploadsService)
await fastify.register(postMessagesUploadsService) await fastify.register(postMessagesUploadsService)
await fastify.register(deleteUsersUploadsService)
await fastify.register(getUsersUploadsService) await fastify.register(getUsersUploadsService)
await fastify.register(postUsersUploadsLogoService) await fastify.register(postUsersUploadsLogoService)
} }

View File

@ -0,0 +1,48 @@
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { Type } from '@sinclair/typebox'
import { fastifyErrors } from '../../../models/utils.js'
import verifyAPIKey from '../../../tools/plugins/verifyAPIKey.js'
import {
DeleteParameters,
deleteParameters,
deleteUploadedFile
} from '../../../tools/utils/deleteUploadedFile.js'
export const deleteServiceSchema: FastifySchema = {
tags: ['messages'] as string[],
security: [
{
apiKeyAuth: []
}
] as Array<{ [key: string]: [] }>,
params: deleteParameters,
response: {
200: Type.String(),
400: fastifyErrors[400],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const deleteMessagesUploadsService: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(verifyAPIKey)
await fastify.route<{
Params: DeleteParameters
}>({
method: 'DELETE',
url: '/uploads/messages/:file',
schema: deleteServiceSchema,
handler: async (request, reply) => {
return await deleteUploadedFile({
fastify,
request,
reply,
folder: 'messages'
})
}
})
}

View File

@ -1,6 +1,6 @@
import { Type } from '@sinclair/typebox' import { Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify' import { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from 'fastify-multipart' import fastifyMultipart from '@fastify/multipart'
import { fastifyErrors } from '../../../models/utils.js' import { fastifyErrors } from '../../../models/utils.js'
import { uploadFile } from '../../../tools/utils/uploadFile.js' import { uploadFile } from '../../../tools/utils/uploadFile.js'

View File

@ -0,0 +1,48 @@
import { FastifyPluginAsync, FastifySchema } from 'fastify'
import { Type } from '@sinclair/typebox'
import { fastifyErrors } from '../../../models/utils.js'
import verifyAPIKey from '../../../tools/plugins/verifyAPIKey.js'
import {
DeleteParameters,
deleteParameters,
deleteUploadedFile
} from '../../../tools/utils/deleteUploadedFile.js'
export const deleteServiceSchema: FastifySchema = {
tags: ['users'] as string[],
security: [
{
apiKeyAuth: []
}
] as Array<{ [key: string]: [] }>,
params: deleteParameters,
response: {
200: Type.String(),
400: fastifyErrors[400],
404: fastifyErrors[404],
500: fastifyErrors[500]
}
} as const
export const deleteUsersUploadsService: FastifyPluginAsync = async (
fastify
) => {
await fastify.register(verifyAPIKey)
await fastify.route<{
Params: DeleteParameters
}>({
method: 'DELETE',
url: '/uploads/users/:file',
schema: deleteServiceSchema,
handler: async (request, reply) => {
return await deleteUploadedFile({
fastify,
request,
reply,
folder: 'users'
})
}
})
}

View File

@ -1,6 +1,6 @@
import { Type } from '@sinclair/typebox' import { Type } from '@sinclair/typebox'
import { FastifyPluginAsync, FastifySchema } from 'fastify' import { FastifyPluginAsync, FastifySchema } from 'fastify'
import fastifyMultipart from 'fastify-multipart' import fastifyMultipart from '@fastify/multipart'
import { fastifyErrors } from '../../../models/utils.js' import { fastifyErrors } from '../../../models/utils.js'
import { uploadFile } from '../../../tools/utils/uploadFile.js' import { uploadFile } from '../../../tools/utils/uploadFile.js'

View File

@ -1,6 +1,6 @@
import dotenv from 'dotenv' import dotenv from 'dotenv'
import { readPackage } from 'read-pkg' import { readPackage } from 'read-pkg'
import { FastifyDynamicSwaggerOptions } from 'fastify-swagger' import { FastifyDynamicSwaggerOptions } from '@fastify/swagger'
dotenv.config() dotenv.config()

View File

@ -25,5 +25,5 @@ export default fastifyPlugin(
request.apiKey = apiKey request.apiKey = apiKey
}) })
}, },
{ fastify: '3.x' } { fastify: '4.x' }
) )

View File

@ -0,0 +1,51 @@
import { IncomingMessage, Server, ServerResponse } from 'node:http'
import { fileURLToPath } from 'node:url'
import fs from 'node:fs'
import { Static, Type } from '@sinclair/typebox'
import {
FastifyInstance,
FastifyLoggerInstance,
FastifyReply,
FastifyRequest,
FastifyTypeProviderDefault
} from 'fastify'
import { isExistingFile } from './isExistingFile.js'
export const deleteParameters = Type.Object({
file: Type.String()
})
export type DeleteParameters = Static<typeof deleteParameters>
export interface DeleteUploadedFileOptions {
folder: 'guilds' | 'messages' | 'users'
fastify: FastifyInstance<
Server,
IncomingMessage,
ServerResponse,
FastifyLoggerInstance,
FastifyTypeProviderDefault
>
request: FastifyRequest<{ Params: DeleteParameters }>
reply: FastifyReply
}
export const deleteUploadedFile = async (
options: DeleteUploadedFileOptions
): Promise<string> => {
const { request, fastify, reply, folder } = options
if (request.apiKey == null) {
throw fastify.httpErrors.forbidden()
}
const { file } = request.params
const fileURL = new URL(`../../../uploads/${folder}/${file}`, import.meta.url)
const filePath = fileURLToPath(fileURL)
if (!(await isExistingFile(filePath))) {
throw fastify.httpErrors.notFound('File does not exist')
}
await fs.promises.rm(filePath, { force: true })
reply.statusCode = 200
return file
}

View File

@ -0,0 +1,10 @@
import fs from 'node:fs'
export const isExistingFile = async (path: string): Promise<boolean> => {
try {
await fs.promises.access(path, fs.constants.F_OK)
return true
} catch {
return false
}
}

View File

@ -3,7 +3,7 @@ import { URL } from 'node:url'
import { randomUUID } from 'node:crypto' import { randomUUID } from 'node:crypto'
import { FastifyInstance, FastifyRequest } from 'fastify' import { FastifyInstance, FastifyRequest } from 'fastify'
import { Multipart } from 'fastify-multipart' import { Multipart } from '@fastify/multipart'
import { API_URL, ROOT_URL } from '../configurations/index.js' import { API_URL, ROOT_URL } from '../configurations/index.js'