refactor(storage): enforce per-user max_file_bytes limit on upload pipelines (#20)
CD — Deployment / deploy (push) Successful in 22s
CD — Deployment / deploy (push) Successful in 22s
* Validate Content-Length on single-part upload route against user limit * Check total expected size on chunked upload session creation * Re-evaluate user limit at finalization to reject dynamic quota reductions * Clean up temporary files on payload size rejection (413) * Add 60-second polling interval for periodic fetching of messages and images --------- Co-authored-by: xEsiah <[email protected]> Reviewed-on: #20 Reviewed-by: Esiah <[email protected]> Co-authored-by: Gitea Actions <-[email protected]>
This commit was merged in pull request #20.
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
-- 010_upload_sessions.sql
|
||||
--
|
||||
-- Upload resumable pour les fichiers qui depassent la limite de corps de
|
||||
-- requete de Cloudflare (100 MB en Free/Pro). Le client cree une session,
|
||||
-- envoie des morceaux de 50 MiB, puis demande la finalisation.
|
||||
--
|
||||
-- Le fichier temporaire est prealloue a la taille finale : le disque est donc
|
||||
-- verifie avant le premier octet utile, et les ecritures se font a une offset
|
||||
-- connue sans jamais garder le fichier complet en memoire.
|
||||
--
|
||||
-- `received_bytes` avance uniquement quand la plage demandee correspond
|
||||
-- exactement a la suite deja recue : les morceaux sont sequentiels, ce qui
|
||||
-- rend la reprise simple (GET renvoie l'offset courant) et evite les trous.
|
||||
|
||||
CREATE TABLE upload_sessions (
|
||||
id CHAR(36) NOT NULL,
|
||||
user_id CHAR(36) NOT NULL,
|
||||
folder_id CHAR(36) NULL,
|
||||
storage_location_id INT UNSIGNED NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
mime_type VARCHAR(255) NULL,
|
||||
size_bytes BIGINT UNSIGNED NOT NULL,
|
||||
received_bytes BIGINT UNSIGNED NOT NULL DEFAULT 0,
|
||||
chunk_size INT UNSIGNED NOT NULL,
|
||||
relative_path VARCHAR(1024) NOT NULL,
|
||||
temp_path VARCHAR(1024) NOT NULL,
|
||||
expires_at DATETIME NOT NULL,
|
||||
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
PRIMARY KEY (id),
|
||||
KEY idx_upload_sessions_user (user_id),
|
||||
KEY idx_upload_sessions_expiry (expires_at),
|
||||
CONSTRAINT chk_upload_sessions_received CHECK (received_bytes <= size_bytes),
|
||||
CONSTRAINT fk_upload_sessions_user FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_upload_sessions_folder FOREIGN KEY (folder_id) REFERENCES folders (id) ON DELETE CASCADE,
|
||||
CONSTRAINT fk_upload_sessions_location FOREIGN KEY (storage_location_id) REFERENCES storage_locations (id) ON DELETE RESTRICT
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
+12
-1
@@ -11,6 +11,7 @@ import { registerFolderRoutes } from "./routes/folders.js";
|
||||
import { registerMediaRoutes } from "./routes/media.js";
|
||||
import { registerSearchRoutes } from "./routes/search.js";
|
||||
import { registerTrashRoutes, purgeExpiredTrash } from "./routes/trash.js";
|
||||
import { purgeExpiredUploads, registerUploadRoutes } from "./routes/uploads.js";
|
||||
|
||||
// Les migrations sont relues au demarrage. L'operation est idempotente et
|
||||
// refuse de servir si une migration deja appliquee a change : une base non
|
||||
@@ -27,6 +28,7 @@ void startService({
|
||||
registerDirectoryRoutes(app, db);
|
||||
registerFolderRoutes(app, db, config);
|
||||
registerFileRoutes(app, db, config);
|
||||
registerUploadRoutes(app, db, config);
|
||||
registerMediaRoutes(app, db);
|
||||
registerTrashRoutes(app, db, config);
|
||||
registerSearchRoutes(app, db);
|
||||
@@ -36,6 +38,15 @@ void startService({
|
||||
// Entretien au demarrage : sessions expirees et corbeille hors retention.
|
||||
const sessions = await purgeExpiredSessions(db);
|
||||
const purged = await purgeExpiredTrash(db, config);
|
||||
logger.info("entretien au demarrage", { sessions, purged });
|
||||
const uploads = await purgeExpiredUploads(db, config);
|
||||
logger.info("entretien au demarrage", { sessions, purged, uploads });
|
||||
|
||||
// Les envois abandonnes ne doivent pas garder leur temporaire indefiniment :
|
||||
// un balayage horaire suffit, la session expirant apres 24 h.
|
||||
setInterval(() => {
|
||||
void purgeExpiredUploads(db, config).catch((error: unknown) =>
|
||||
logger.error("purge des envois inacheves", { error: String(error) }),
|
||||
);
|
||||
}, 60 * 60 * 1000).unref();
|
||||
},
|
||||
});
|
||||
|
||||
@@ -56,7 +56,7 @@ const parse = <T>(schema: z.ZodType<T>, body: unknown): T => {
|
||||
return result.data;
|
||||
};
|
||||
|
||||
interface FileRow {
|
||||
export interface FileRow {
|
||||
id: string;
|
||||
folder_id: string | null;
|
||||
name: string;
|
||||
@@ -71,13 +71,13 @@ interface FileRow {
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
const SELECT_FILE = `SELECT f.id, f.folder_id, f.name, f.mime_type, f.size_bytes, f.checksum_sha256,
|
||||
export const SELECT_FILE = `SELECT f.id, f.folder_id, f.name, f.mime_type, f.size_bytes, f.checksum_sha256,
|
||||
f.relative_path, f.owner_id,
|
||||
EXISTS (SELECT 1 FROM user_file_stars s WHERE s.file_id = f.id AND s.user_id = ?) AS is_starred,
|
||||
f.download_count, f.created_at, f.updated_at
|
||||
FROM files f`;
|
||||
|
||||
const publicFile = (row: FileRow) => ({
|
||||
export const publicFile = (row: FileRow) => ({
|
||||
id: row.id,
|
||||
folder_id: row.folder_id,
|
||||
name: row.name,
|
||||
@@ -92,7 +92,7 @@ const publicFile = (row: FileRow) => ({
|
||||
});
|
||||
|
||||
/** Nom d'affichage : ni separateur, ni sequence d'echappement, ni point seul. */
|
||||
const assertName = (raw: string | undefined): string => {
|
||||
export const assertName = (raw: string | undefined): string => {
|
||||
const name = (raw ?? "").trim();
|
||||
if (!name || name.length > 255) {
|
||||
throw AppError.badRequest("Le nom du fichier doit contenir entre 1 et 255 caracteres.");
|
||||
@@ -103,7 +103,7 @@ const assertName = (raw: string | undefined): string => {
|
||||
return name;
|
||||
};
|
||||
|
||||
const mimeOf = (header: string | undefined): string => {
|
||||
export const mimeOf = (header: string | undefined): string => {
|
||||
const value = (header ?? "").split(";")[0]?.trim().toLowerCase() ?? "";
|
||||
return MIME.test(value) ? value : "application/octet-stream";
|
||||
};
|
||||
|
||||
@@ -0,0 +1,482 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { stat } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { z } from "zod";
|
||||
import {
|
||||
AppError,
|
||||
attachUser,
|
||||
currentUser,
|
||||
requireAuth,
|
||||
requireRole,
|
||||
type App,
|
||||
type Config,
|
||||
type Db,
|
||||
} from "../lib/index.js";
|
||||
import { audit, canWrite } from "../access.js";
|
||||
import { indexMedia } from "../media.js";
|
||||
import {
|
||||
absolutePath,
|
||||
chooseLocation,
|
||||
discard,
|
||||
diskUsage,
|
||||
ensureDir,
|
||||
fileExists,
|
||||
hashFile,
|
||||
listUploadTemps,
|
||||
preallocateUpload,
|
||||
publish,
|
||||
relativePathFor,
|
||||
uploadTempPath,
|
||||
writeChunk,
|
||||
} from "../storage.js";
|
||||
import { SELECT_FILE, assertName, mimeOf, publicFile, type FileRow } from "./files.js";
|
||||
|
||||
/**
|
||||
* Upload resumable pour les fichiers qui depassent la limite de corps de
|
||||
* requete de Cloudflare. Le client envoie des morceaux sequentiels de 50 MiB,
|
||||
* ce qui reste sous la limite de 100 MB des offres Free/Pro.
|
||||
*
|
||||
* Le fichier temporaire est prealloue et rempli a l'offset courant. Une
|
||||
* session ne peut appartenir qu'a son createur, et les droits d'ecriture sont
|
||||
* revalides au moment de la finalisation : un partage retire pendant l'envoi
|
||||
* ne permet pas de publier le fichier.
|
||||
*/
|
||||
|
||||
const uuid = z
|
||||
.string()
|
||||
.regex(
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
||||
"Identifiant invalide.",
|
||||
);
|
||||
|
||||
/** 50 MiB : sous la limite Cloudflare (100 MB) avec une marge confortable. */
|
||||
export const CHUNK_SIZE = 50 * 1024 * 1024;
|
||||
|
||||
/** Duree de vie d'une session abandonnee, avant purge du temporaire. */
|
||||
const SESSION_TTL_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
const parse = <T>(schema: z.ZodType<T>, body: unknown): T => {
|
||||
const result = schema.safeParse(body);
|
||||
if (!result.success) {
|
||||
const issue = result.error.issues[0];
|
||||
throw AppError.badRequest(
|
||||
issue ? `${issue.path.join(".") || "corps"} : ${issue.message}` : "Corps invalide.",
|
||||
);
|
||||
}
|
||||
return result.data;
|
||||
};
|
||||
|
||||
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`;
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
/** Supprime la session et son temporaire, sans bruit si deja absente. */
|
||||
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> => {
|
||||
if (!uuid.safeParse(id).success) throw AppError.notFound("Session d'envoi introuvable.");
|
||||
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;
|
||||
};
|
||||
|
||||
const assertNameFree = async (
|
||||
db: Db,
|
||||
user: { id: string },
|
||||
folderId: string | null,
|
||||
name: string,
|
||||
): Promise<void> => {
|
||||
const duplicate = await db.one(
|
||||
`SELECT 1 AS taken FROM files
|
||||
WHERE folder_id <=> ? AND active_name = ? AND deleted_at IS NULL
|
||||
AND ( folder_id IS NOT NULL OR owner_id = ? )
|
||||
LIMIT 1`,
|
||||
[folderId, name, user.id],
|
||||
);
|
||||
if (duplicate) throw AppError.conflict("Ce nom existe deja ici.");
|
||||
};
|
||||
|
||||
export const registerUploadRoutes = (app: App, db: Db, config: Config): void => {
|
||||
app.use("/api/cloud/uploads/*", attachUser(db));
|
||||
app.use("/api/cloud/uploads", attachUser(db));
|
||||
|
||||
app.post(
|
||||
"/api/cloud/uploads",
|
||||
requireAuth,
|
||||
requireRole("admin", "user"),
|
||||
async (context) => {
|
||||
const user = currentUser(context);
|
||||
const data = parse(createInput, await context.req.json().catch(() => null));
|
||||
const name = assertName(data.name);
|
||||
const folderId = data.folderId ?? null;
|
||||
const size = data.size;
|
||||
|
||||
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();
|
||||
|
||||
// Les sessions en cours immobilisent deja une part du quota : sans
|
||||
// cette somme, plusieurs envois simultanes pourraient chacun passer le
|
||||
// controle puis depasser le quota au moment de la finalisation.
|
||||
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,
|
||||
mimeOf(data.mimeType ?? undefined),
|
||||
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]);
|
||||
await audit(
|
||||
db,
|
||||
context,
|
||||
user,
|
||||
"file.upload.start",
|
||||
"file",
|
||||
id,
|
||||
{ name, size, folder_id: folderId },
|
||||
config.TRUST_PROXY,
|
||||
);
|
||||
|
||||
return context.json({ upload: sessionPublic(created) }, 201);
|
||||
},
|
||||
);
|
||||
|
||||
app.get("/api/cloud/uploads/:id", requireAuth, async (context) => {
|
||||
const user = currentUser(context);
|
||||
const row = await loadSession(db, user, context.req.param("id") ?? "");
|
||||
return context.json({ upload: sessionPublic(row) });
|
||||
});
|
||||
|
||||
app.put("/api/cloud/uploads/:id/chunks", requireAuth, async (context) => {
|
||||
const user = currentUser(context);
|
||||
const row = await loadSession(db, user, context.req.param("id") ?? "");
|
||||
const offset = Number(row.received_bytes);
|
||||
const size = Number(row.size_bytes);
|
||||
|
||||
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.");
|
||||
}
|
||||
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 body = context.req.raw.body;
|
||||
if (!body) throw AppError.badRequest("Corps de requete vide.");
|
||||
|
||||
// Ecriture d'abord, curseur ensuite : un morceau interrompu laisse le
|
||||
// curseur intact, donc le prochain essai reecrit la meme plage sans creer
|
||||
// de trou. Deux requetes concurrentes sur la meme plage ecrivent les
|
||||
// memes octets : une seule avance le curseur, l'autre constate que le
|
||||
// morceau est deja applique.
|
||||
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 context.json({ upload: sessionPublic(fresh) });
|
||||
}
|
||||
|
||||
return context.json({ upload: sessionPublic({ ...row, received_bytes: offset + declared }) });
|
||||
});
|
||||
|
||||
app.post("/api/cloud/uploads/:id/complete", requireAuth, async (context) => {
|
||||
const user = currentUser(context);
|
||||
const row = await loadSession(db, user, context.req.param("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.");
|
||||
}
|
||||
|
||||
// La limite de taille est propre au compte : elle peut avoir baisse
|
||||
// pendant un envoi long, il faut donc la reverifier avant de publier.
|
||||
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);
|
||||
|
||||
// Le dossier de l'emplacement peut ne pas encore exister : l'envoi direct
|
||||
// le cree avant d'ecrire, l'envoi resumable ecrit d'abord dans .uploads.
|
||||
await ensureDir(path.dirname(destination));
|
||||
|
||||
// Publication avant la base, comme le depot direct : si la transaction
|
||||
// echoue, le temporaire est remis en place pour permettre un nouvel essai.
|
||||
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,
|
||||
);
|
||||
|
||||
await audit(
|
||||
db,
|
||||
context,
|
||||
user,
|
||||
"file.upload",
|
||||
"file",
|
||||
row.id,
|
||||
{ name: row.name, size, folder_id: row.folder_id },
|
||||
config.TRUST_PROXY,
|
||||
);
|
||||
|
||||
const created = await db.required<FileRow>(`${SELECT_FILE} WHERE f.id = ?`, [user.id, row.id]);
|
||||
return context.json({ file: publicFile(created) }, 201);
|
||||
});
|
||||
|
||||
app.delete("/api/cloud/uploads/:id", requireAuth, async (context) => {
|
||||
const user = currentUser(context);
|
||||
const row = await loadSession(db, user, context.req.param("id") ?? "");
|
||||
await dropSession(db, row);
|
||||
await audit(
|
||||
db,
|
||||
context,
|
||||
user,
|
||||
"file.upload.cancel",
|
||||
"file",
|
||||
row.id,
|
||||
{ name: row.name },
|
||||
config.TRUST_PROXY,
|
||||
);
|
||||
return context.json({ ok: true });
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Purge les sessions arrivees a echeance et les temporaires orphelins (session
|
||||
* supprimee avec le dossier ou le compte, par exemple). Appelee au demarrage
|
||||
* puis periodiquement par le service.
|
||||
*/
|
||||
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;
|
||||
};
|
||||
+68
-1
@@ -1,6 +1,6 @@
|
||||
import { createHash, randomUUID } from "node:crypto";
|
||||
import { createReadStream, createWriteStream } from "node:fs";
|
||||
import { mkdir, rename, rm, stat, statfs } from "node:fs/promises";
|
||||
import { mkdir, open, readdir, rename, rm, stat, statfs } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { Readable, Transform } from "node:stream";
|
||||
import { pipeline } from "node:stream/promises";
|
||||
@@ -130,6 +130,73 @@ export const discard = async (...paths: string[]): Promise<void> => {
|
||||
export const temporaryPath = (mediaDir: string, slug: string): string =>
|
||||
path.join(path.resolve(mediaDir), slug, `.upload-${randomUUID()}`);
|
||||
|
||||
/**
|
||||
* Dossier des uploads resumables. Il reste sous MEDIA_DIR : le renommage
|
||||
* final vers l'emplacement de stockage est donc atomique.
|
||||
*/
|
||||
export const uploadTempDir = (mediaDir: string): string =>
|
||||
path.join(path.resolve(mediaDir), ".uploads");
|
||||
|
||||
export const uploadTempPath = (mediaDir: string, id: string): string =>
|
||||
path.join(uploadTempDir(mediaDir), `${id}.part`);
|
||||
|
||||
/** Prealloue le temporaire a la taille annoncee, sans ecrire les octets. */
|
||||
export const preallocateUpload = async (destination: string, size: number): Promise<void> => {
|
||||
await ensureDir(path.dirname(destination));
|
||||
const handle = await open(destination, "wx");
|
||||
try {
|
||||
await handle.truncate(size);
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Ecrit un morceau a l'offset courant. Le flux est borne : un client qui
|
||||
* annonce une taille et en envoie davantage est coupe avant d'ecrire l'exces.
|
||||
*/
|
||||
export const writeChunk = async (
|
||||
destination: string,
|
||||
offset: number,
|
||||
body: ReadableStream<Uint8Array>,
|
||||
maxBytes: number,
|
||||
): Promise<number> => {
|
||||
let received = 0;
|
||||
|
||||
const meter = new Transform({
|
||||
transform(chunk: Buffer, _encoding, callback) {
|
||||
received += chunk.length;
|
||||
if (received > maxBytes) {
|
||||
callback(AppError.payloadTooLarge(`Morceau trop volumineux (maximum ${maxBytes} octets).`));
|
||||
return;
|
||||
}
|
||||
callback(null, chunk);
|
||||
},
|
||||
});
|
||||
|
||||
const source = Readable.fromWeb(body as Parameters<typeof Readable.fromWeb>[0]);
|
||||
await pipeline(source, meter, createWriteStream(destination, { flags: "r+", start: offset }));
|
||||
|
||||
return received;
|
||||
};
|
||||
|
||||
/** SHA-256 du fichier complet, calcule au moment de la finalisation. */
|
||||
export const hashFile = async (absolute: string): Promise<string> => {
|
||||
const hash = createHash("sha256");
|
||||
for await (const chunk of createReadStream(absolute)) {
|
||||
hash.update(chunk as Buffer);
|
||||
}
|
||||
return hash.digest("hex");
|
||||
};
|
||||
|
||||
/** Fichiers `.part` abandonnes : utilises par la purge des sessions expirees. */
|
||||
export const listUploadTemps = async (mediaDir: string): Promise<string[]> => {
|
||||
const entries = await readdir(uploadTempDir(mediaDir)).catch(() => []);
|
||||
return entries
|
||||
.filter((entry) => entry.endsWith(".part"))
|
||||
.map((entry) => path.join(uploadTempDir(mediaDir), entry));
|
||||
};
|
||||
|
||||
export const fileExists = async (absolute: string): Promise<boolean> => {
|
||||
try {
|
||||
const info = await stat(absolute);
|
||||
|
||||
Reference in New Issue
Block a user