Files
CoHaiSe-CLOUD/backend/src/services/uploadService.ts
T
Gitea ActionsandEsiah f689ab4d57
CD — Deployment / deploy (push) Successful in 34s
refactor: restructure frontend and backend by domain services (#26)
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.

---------

Co-authored-by: xEsiah <[email protected]>
Reviewed-on: #26
Reviewed-by: Esiah <[email protected]>
Co-authored-by: Gitea Actions <-[email protected]>
2026-11-13 09:13:27 +00:00

377 lines
11 KiB
TypeScript

import { randomUUID } from "node:crypto";
import { stat } from "node:fs/promises";
import path from "node:path";
import type { Config } from "../config/appConfig.js";
import type { Db } from "../config/db.js";
import type { FileRow, SessionUser } from "../models/types.js";
import { AppError } from "../utils/errors.js";
import { canWrite } from "./auditService.js";
import { indexMedia } from "./mediaService.js";
import { assertNameFree, SELECT_FILE } from "./fileService.js";
import {
absolutePath,
chooseLocation,
discard,
diskUsage,
ensureDir,
fileExists,
hashFile,
listUploadTemps,
preallocateUpload,
publish,
relativePathFor,
uploadTempPath,
writeChunk,
} from "./storageService.js";
export const CHUNK_SIZE = 50 * 1024 * 1024;
const SESSION_TTL_MS = 24 * 60 * 60 * 1000;
interface UploadSessionRow {
id: string;
user_id: string;
folder_id: string | null;
storage_location_id: number;
name: string;
mime_type: string | null;
size_bytes: number | string;
received_bytes: number | string;
chunk_size: number;
relative_path: string;
temp_path: string;
expires_at: string | Date;
}
const SELECT_SESSION = `SELECT id, user_id, folder_id, storage_location_id, name, mime_type,
size_bytes, received_bytes, chunk_size, relative_path, temp_path, expires_at
FROM upload_sessions`;
export const sessionPublic = (row: UploadSessionRow) => ({
uploadId: row.id,
name: row.name,
folderId: row.folder_id,
size: Number(row.size_bytes),
chunkSize: row.chunk_size,
uploadedBytes: Number(row.received_bytes),
expiresAt: new Date(row.expires_at).toISOString(),
});
export type UploadSession = ReturnType<typeof sessionPublic>;
const dropSession = async (db: Db, row: UploadSessionRow): Promise<void> => {
await discard(row.temp_path);
await db.exec("DELETE FROM upload_sessions WHERE id = ?", [row.id]).catch(() => undefined);
};
const loadSession = async (
db: Db,
user: { id: string },
id: string,
): Promise<UploadSessionRow> => {
const row = await db.one<UploadSessionRow>(`${SELECT_SESSION} WHERE id = ? AND user_id = ?`, [
id,
user.id,
]);
if (!row) throw AppError.notFound("Session d'envoi introuvable.");
if (new Date(row.expires_at).getTime() <= Date.now()) {
await dropSession(db, row);
throw AppError.notFound("Session d'envoi expiree.");
}
return row;
};
export interface CreateUploadInput {
name: string;
folderId: string | null;
size: number;
mimeType: string;
}
/** Cree une session d'envoi morcele avec preallocation du fichier temporaire. */
export const createUploadSession = async (
db: Db,
config: Config,
user: SessionUser,
input: CreateUploadInput,
): Promise<UploadSession> => {
const { name, folderId, size, mimeType } = input;
if (folderId !== null && !(await canWrite(db, user, folderId))) {
throw AppError.forbidden("Vous ne pouvez pas ecrire dans ce dossier.");
}
const budget = await db.one<{
used_bytes: number | string;
quota_bytes: number | string;
max_file_bytes: number | string;
}>(
"SELECT used_bytes, quota_bytes, max_file_bytes FROM users WHERE id = ? AND deleted_at IS NULL",
[user.id],
);
if (!budget) throw AppError.unauthorized();
const pending = await db.one<{ bytes: number | string | null }>(
`SELECT COALESCE(SUM(size_bytes - received_bytes), 0) AS bytes
FROM upload_sessions
WHERE user_id = ? AND expires_at > UTC_TIMESTAMP()`,
[user.id],
);
if (BigInt(size) > BigInt(budget.max_file_bytes)) {
throw AppError.payloadTooLarge(
`Fichier trop volumineux (maximum ${budget.max_file_bytes} octets).`,
);
}
if (
BigInt(budget.used_bytes) + BigInt(pending?.bytes ?? 0) + BigInt(size) >
BigInt(budget.quota_bytes)
) {
throw AppError.payloadTooLarge("Votre quota de stockage est depasse.");
}
await assertNameFree(db, user, folderId, name);
const disk = await diskUsage(config.MEDIA_DIR);
if (disk !== null && BigInt(size) > BigInt(disk.available_bytes)) {
throw AppError.insufficientStorage(
"Espace disque insuffisant sur le serveur pour ce fichier.",
);
}
const location = await chooseLocation(db, BigInt(size));
const id = randomUUID();
const relative = relativePathFor(location.slug, id, name);
const temporary = uploadTempPath(config.MEDIA_DIR, id);
await preallocateUpload(temporary, size);
try {
await db.exec(
`INSERT INTO upload_sessions
(id, user_id, folder_id, storage_location_id, name, mime_type, size_bytes,
received_bytes, chunk_size, relative_path, temp_path, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, ?)`,
[
id,
user.id,
folderId,
location.id,
name,
mimeType,
size,
CHUNK_SIZE,
relative,
temporary,
new Date(Date.now() + SESSION_TTL_MS),
],
);
} catch (error) {
await discard(temporary);
throw error;
}
const created = await db.required<UploadSessionRow>(`${SELECT_SESSION} WHERE id = ?`, [id]);
return sessionPublic(created);
};
export const getUploadSession = async (
db: Db,
user: SessionUser,
id: string,
): Promise<UploadSession> => {
const row = await loadSession(db, user, id);
return sessionPublic(row);
};
/** Ecrit un morceau a l'offset attendu et met a jour la progression. */
export const receiveChunk = async (
db: Db,
user: SessionUser,
id: string,
body: ReadableStream<Uint8Array>,
declared: number,
): Promise<UploadSession> => {
const row = await loadSession(db, user, id);
const offset = Number(row.received_bytes);
const size = Number(row.size_bytes);
if (declared > row.chunk_size) {
throw AppError.payloadTooLarge(`Morceau trop volumineux (maximum ${row.chunk_size} octets).`);
}
if (offset + declared > size) {
throw AppError.badRequest("Le morceau depasse la taille annoncee du fichier.");
}
const written = await writeChunk(row.temp_path, offset, body, declared);
if (written !== declared) {
throw AppError.badRequest(
`Longueur incoherente : ${declared} octets annonces, ${written} recus.`,
);
}
const reserved = await db.exec(
`UPDATE upload_sessions
SET received_bytes = received_bytes + ?
WHERE id = ? AND user_id = ? AND received_bytes = ?
AND received_bytes + ? <= size_bytes AND expires_at > UTC_TIMESTAMP()`,
[declared, row.id, user.id, offset, declared],
);
if (reserved.affectedRows === 0) {
const fresh = await db.one<UploadSessionRow>(
`${SELECT_SESSION} WHERE id = ? AND user_id = ?`,
[row.id, user.id],
);
if (!fresh) throw AppError.notFound("Session d'envoi introuvable.");
if (Number(fresh.received_bytes) < offset + declared) {
throw AppError.conflict("Morceau deja recu ou session modifiee. Reprenez depuis le statut.");
}
return sessionPublic(fresh);
}
return sessionPublic({ ...row, received_bytes: offset + declared });
};
/** Finalise l'envoi : controle, publication et enregistrement du fichier. */
export const completeUpload = async (
db: Db,
config: Config,
user: SessionUser,
id: string,
): Promise<FileRow> => {
const row = await loadSession(db, user, id);
const size = Number(row.size_bytes);
const received = Number(row.received_bytes);
if (received !== size) {
throw AppError.badRequest("Envoi incomplet : des morceaux sont encore manquants.");
}
if (!(await fileExists(row.temp_path))) {
await dropSession(db, row);
throw AppError.notFound("Fichier temporaire introuvable. Relancez l'envoi.");
}
const budget = await db.one<{ max_file_bytes: number | string }>(
"SELECT max_file_bytes FROM users WHERE id = ? AND deleted_at IS NULL",
[user.id],
);
if (!budget) {
await dropSession(db, row);
throw AppError.unauthorized();
}
if (BigInt(size) > BigInt(budget.max_file_bytes)) {
await dropSession(db, row);
throw AppError.payloadTooLarge(
`Fichier trop volumineux (maximum ${budget.max_file_bytes} octets).`,
);
}
if (row.folder_id !== null && !(await canWrite(db, user, row.folder_id))) {
await dropSession(db, row);
throw AppError.forbidden("Vous ne pouvez plus ecrire dans ce dossier.");
}
try {
await assertNameFree(db, user, row.folder_id, row.name);
} catch (error) {
await dropSession(db, row);
throw error;
}
const checksum = await hashFile(row.temp_path);
const destination = absolutePath(config.MEDIA_DIR, row.relative_path);
await ensureDir(path.dirname(destination));
await publish(row.temp_path, destination);
try {
await db.transaction(async (connection) => {
const [quota] = await connection.execute(
`UPDATE users SET used_bytes = used_bytes + ?
WHERE id = ? AND used_bytes + ? <= quota_bytes`,
[size, user.id, size],
);
if ((quota as { affectedRows: number }).affectedRows === 0) {
throw AppError.payloadTooLarge("Votre quota de stockage est depasse.");
}
const [support] = await connection.execute(
`UPDATE storage_locations
SET used_bytes = used_bytes + ?
WHERE id = ? AND (quota_bytes IS NULL OR used_bytes + ? <= quota_bytes)`,
[size, row.storage_location_id, size],
);
if ((support as { affectedRows: number }).affectedRows === 0) {
throw AppError.payloadTooLarge("L'espace disponible sur le support est insuffisant.");
}
await connection.execute(
`INSERT INTO files
(id, storage_location_id, folder_id, name, mime_type, size_bytes,
checksum_sha256, relative_path, owner_id, created_by)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
row.id,
row.storage_location_id,
row.folder_id,
row.name,
row.mime_type ?? "application/octet-stream",
size,
checksum,
row.relative_path,
user.id,
user.id,
],
);
await connection.execute("DELETE FROM upload_sessions WHERE id = ?", [row.id]);
});
} catch (error) {
await publish(destination, row.temp_path).catch(async () => {
await discard(destination);
await db.exec("DELETE FROM upload_sessions WHERE id = ?", [row.id]).catch(() => undefined);
});
if ((error as { errno?: number }).errno === 1062) {
throw AppError.conflict("Ce nom existe deja ici.");
}
throw error;
}
await indexMedia(db, config, row.id, row.mime_type ?? "application/octet-stream", destination).catch(
() => undefined,
);
return db.required<FileRow>(`${SELECT_FILE} WHERE f.id = ?`, [user.id, row.id]);
};
export const cancelUpload = async (
db: Db,
user: SessionUser,
id: string,
): Promise<UploadSessionRow> => {
const row = await loadSession(db, user, id);
await dropSession(db, row);
return row;
};
/** Purge les sessions d'envoi expirees et leurs fichiers temporaires. */
export const purgeExpiredUploads = async (db: Db, config: Config): Promise<number> => {
const expired = await db.query<UploadSessionRow>(
`${SELECT_SESSION} WHERE expires_at <= UTC_TIMESTAMP()`,
);
for (const row of expired) await discard(row.temp_path);
const result = await db.exec("DELETE FROM upload_sessions WHERE expires_at <= UTC_TIMESTAMP()");
const cutoff = Date.now() - SESSION_TTL_MS;
for (const temporary of await listUploadTemps(config.MEDIA_DIR)) {
const info = await stat(temporary).catch(() => null);
if (info !== null && info.mtimeMs < cutoff) await discard(temporary);
}
return result.affectedRows;
};