PR — Ouverture automatique / create-pr (push) Successful in 3s
Split the monolithic backend (lib/, routes/, access.ts, storage.ts, media.ts) into a layered structure inspired by JeanXXIII-CRM: - config/: appConfig, db - middleware/: auth (session, roles, cookies) - models/: shared types - routes/: *Routes.ts (thin HTTP handlers) - services/: *Service.ts (business logic, one per domain) - utils/: errors, http, migrate, ratelimit, validators, service bootstrap Frontend likewise: services/*Service.ts + apiClient, contexts/, types/, utils/format. Route files now carry the "Routes" suffix; service files are named authService.ts, ratelimit.ts, etc. and live in their own files, frontend and backend separated. Updated docs and READMEs to reflect the new layout. Typecheck and builds pass.
123 lines
3.7 KiB
TypeScript
123 lines
3.7 KiB
TypeScript
import { z } from "zod";
|
|
import type { Config } from "../config/appConfig.js";
|
|
import type { Db } from "../config/db.js";
|
|
import { attachUser, currentUser, requireAuth, requireRole } from "../middleware/auth.js";
|
|
import { audit } from "../services/auditService.js";
|
|
import { publicFile } from "../services/fileService.js";
|
|
import {
|
|
cancelUpload,
|
|
completeUpload,
|
|
createUploadSession,
|
|
getUploadSession,
|
|
receiveChunk,
|
|
} from "../services/uploadService.js";
|
|
import { AppError } from "../utils/errors.js";
|
|
import { readJson, type App } from "../utils/http.js";
|
|
import { assertName, mimeOf, parse, uuid } from "../utils/validators.js";
|
|
|
|
const createInput = z.object({
|
|
name: z.string().trim().min(1).max(255),
|
|
folderId: uuid.nullish(),
|
|
size: z.number().int().positive().max(Number.MAX_SAFE_INTEGER),
|
|
mimeType: z.string().max(255).nullish(),
|
|
});
|
|
|
|
export const registerUploadRoutes = (app: App, db: Db, config: Config): void => {
|
|
app.use("/api/cloud/uploads/*", attachUser(db, config.SESSION_SECRET));
|
|
app.use("/api/cloud/uploads", attachUser(db, config.SESSION_SECRET));
|
|
|
|
app.post(
|
|
"/api/cloud/uploads",
|
|
requireAuth,
|
|
requireRole("admin", "user"),
|
|
async (context) => {
|
|
const user = currentUser(context);
|
|
const data = parse(createInput, await readJson(context));
|
|
const name = assertName(data.name, "fichier");
|
|
const folderId = data.folderId ?? null;
|
|
|
|
const upload = await createUploadSession(db, config, user, {
|
|
name,
|
|
folderId,
|
|
size: data.size,
|
|
mimeType: mimeOf(data.mimeType ?? undefined),
|
|
});
|
|
|
|
await audit(
|
|
db,
|
|
context,
|
|
user,
|
|
"file.upload.start",
|
|
"file",
|
|
upload.uploadId,
|
|
{ name, size: data.size, folder_id: folderId },
|
|
config.TRUST_PROXY,
|
|
);
|
|
|
|
return context.json({ upload }, 201);
|
|
},
|
|
);
|
|
|
|
app.get("/api/cloud/uploads/:id", requireAuth, async (context) => {
|
|
const user = currentUser(context);
|
|
const upload = await getUploadSession(db, user, context.req.param("id") ?? "");
|
|
return context.json({ upload });
|
|
});
|
|
|
|
app.put("/api/cloud/uploads/:id/chunks", requireAuth, async (context) => {
|
|
const user = currentUser(context);
|
|
const id = context.req.param("id") ?? "";
|
|
|
|
const declaredHeader = context.req.header("content-length");
|
|
if (declaredHeader === undefined) {
|
|
throw new AppError(411, "LENGTH_REQUIRED", "En-tête Content-Length manquant.");
|
|
}
|
|
const declared = Number(declaredHeader);
|
|
if (!Number.isSafeInteger(declared) || declared <= 0) {
|
|
throw AppError.badRequest("Longueur de morceau invalide.");
|
|
}
|
|
|
|
const body = context.req.raw.body;
|
|
if (!body) throw AppError.badRequest("Corps de requete vide.");
|
|
|
|
const upload = await receiveChunk(db, user, id, body, declared);
|
|
return context.json({ upload });
|
|
});
|
|
|
|
app.post("/api/cloud/uploads/:id/complete", requireAuth, async (context) => {
|
|
const user = currentUser(context);
|
|
const id = context.req.param("id") ?? "";
|
|
|
|
const created = await completeUpload(db, config, user, id);
|
|
|
|
await audit(
|
|
db,
|
|
context,
|
|
user,
|
|
"file.upload",
|
|
"file",
|
|
created.id,
|
|
{ name: created.name, size: created.size_bytes, folder_id: created.folder_id },
|
|
config.TRUST_PROXY,
|
|
);
|
|
|
|
return context.json({ file: publicFile(created) }, 201);
|
|
});
|
|
|
|
app.delete("/api/cloud/uploads/:id", requireAuth, async (context) => {
|
|
const user = currentUser(context);
|
|
const row = await cancelUpload(db, user, context.req.param("id") ?? "");
|
|
await audit(
|
|
db,
|
|
context,
|
|
user,
|
|
"file.upload.cancel",
|
|
"file",
|
|
row.id,
|
|
{ name: row.name },
|
|
config.TRUST_PROXY,
|
|
);
|
|
return context.json({ ok: true });
|
|
});
|
|
};
|