Refactor API for game file download and listing

Removed user registration and user info endpoints, along with related schemas, services, and tests. Added new routes and service for listing and downloading game files. Updated README to reflect new API purpose. Refactored logger and utils for improved modularity.
This commit is contained in:
2026-01-25 21:39:44 +01:00
parent 29cf189a87
commit cf54edf146
15 changed files with 104 additions and 128 deletions

View File

@@ -0,0 +1,72 @@
const fs = require("node:fs")
const path = require("node:path")
const crypto = require("node:crypto")
const { DefaultError } = require("../errors/errors")
const { pipeline } = require("node:stream/promises")
const gameDataPath = path.join(process.cwd(), "data", "game")
function normalizePath($path) {
return $path.split(path.win32.sep).join(path.posix.sep)
}
async function getFileSha1(filePath) {
const hash = crypto.createHash("sha1")
const readStream = fs.createReadStream(filePath)
try {
await pipeline(readStream, hash)
return hash.digest("hex")
} catch (error) {
throw new Error(`Erreur lors du calcul du SHA-1 : ${error.message}`);
}
}
async function getGameFiles() {
const files = await fs.promises.readdir(gameDataPath, { recursive: true })
const gameFilesIndex = { root: [] }
for (const file of files) {
const filePath = path.join(gameDataPath, file)
const fileMetadata = await fs.promises.stat(filePath)
if (!fileMetadata.isDirectory()) {
const normalizedFilePath = normalizePath(file)
const artifact = {
path: normalizedFilePath,
sha1: await getFileSha1(filePath),
size: fileMetadata.size,
url: (process.env.BASE_POINT || "") + normalizedFilePath
}
const downloadObject = {
downloads: {
artifact,
},
name: path.parse(normalizedFilePath).base
}
gameFilesIndex.root.push(downloadObject)
}
}
return gameFilesIndex
}
async function getFile(basePath) {
try {
const fixedPath = path.join(gameDataPath, basePath)
const fileMetadata = await fs.promises.stat(fixedPath)
if (fileMetadata.isDirectory()) {
throw new DefaultError(409, "Can't download a directory", "", "NotDownloadableException")
}
return { code: 200, path: fixedPath }
} catch (error) {
if (error.code == "ENOENT") {
throw new DefaultError(404, "File not found", "", "NotFoundException")
} else {
throw error
}
}
}
module.exports = {
getFile,
getFileSha1,
getGameFiles
}

View File

@@ -1,19 +0,0 @@
const crypto = require("node:crypto")
const DefaultError = require("../errors/DefaultError")
function register({ email, username }) {
const canRegister = true
if (canRegister === true) {
return {
id: crypto.randomUUID(),
username: username,
email: email
}
} else {
throw new DefaultError(418, "I'm a teapot", "", "TeaPotExeception")
}
}
module.exports = {
register
}