S3-Import mit bekannter Dateigröße hochladen
All checks were successful
Build and Push Docker Images / build-frontend (push) Successful in 22s
Build and Push Docker Images / build-central-services-api (push) Successful in 21s
Build and Push Docker Images / build-backend (push) Successful in 43s
Build and Push Docker Images / build-website (push) Successful in 22s
Build and Push Docker Images / build-central-services-admin (push) Successful in 22s
Build and Push Docker Images / build-docs (push) Successful in 21s

This commit is contained in:
root
2026-08-31 20:14:18 +00:00
parent 405cc95b41
commit b1785a35a9

View File

@@ -3,6 +3,9 @@ import { and, eq, inArray, isNull, sql } from "drizzle-orm";
import multipart from "@fastify/multipart"; import multipart from "@fastify/multipart";
import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3"; import { DeleteObjectCommand, GetObjectCommand, PutObjectCommand } from "@aws-sdk/client-s3";
import { randomUUID } from "node:crypto"; import { randomUUID } from "node:crypto";
import { createReadStream, createWriteStream } from "node:fs";
import { stat, unlink } from "node:fs/promises";
import { pipeline } from "node:stream/promises";
import { import {
authTenantUsers, authTenantUsers,
@@ -1446,6 +1449,7 @@ export default async function adminRoutes(server: FastifyInstance) {
// ------------------------------------------------------------- // -------------------------------------------------------------
server.post("/admin/tenant-imports", { bodyLimit: 1024 * 1024 * 1024 }, async (req, reply) => { server.post("/admin/tenant-imports", { bodyLimit: 1024 * 1024 * 1024 }, async (req, reply) => {
let archiveStoragePath: string | null = null; let archiveStoragePath: string | null = null;
let temporaryArchivePath: string | null = null;
try { try {
const currentUser = await requireAdmin(req, reply); const currentUser = await requireAdmin(req, reply);
if (!currentUser) return; if (!currentUser) return;
@@ -1469,13 +1473,19 @@ export default async function adminRoutes(server: FastifyInstance) {
} }
archiveStoragePath = `tenant-imports/${randomUUID()}-${filename}`; archiveStoragePath = `tenant-imports/${randomUUID()}-${filename}`;
temporaryArchivePath = `${process.env.TMPDIR || "/tmp"}/fedeo-tenant-import-${randomUUID()}.zip`;
await pipeline(data.file, createWriteStream(temporaryArchivePath, { flags: "wx" }));
const temporaryArchive = await stat(temporaryArchivePath);
await s3.send(new PutObjectCommand({ await s3.send(new PutObjectCommand({
Bucket: secrets.S3_BUCKET, Bucket: secrets.S3_BUCKET,
Key: archiveStoragePath, Key: archiveStoragePath,
Body: data.file, Body: createReadStream(temporaryArchivePath),
ContentLength: temporaryArchive.size,
ContentType: data.mimetype || "application/zip", ContentType: data.mimetype || "application/zip",
})); }));
fileSize = data.file.bytesRead || null; fileSize = temporaryArchive.size;
await unlink(temporaryArchivePath);
temporaryArchivePath = null;
source = { archiveStoragePath }; source = { archiveStoragePath };
} else { } else {
const body = req.body as TenantFullExport | { exportData?: TenantFullExport; targetTenantId?: number }; const body = req.body as TenantFullExport | { exportData?: TenantFullExport; targetTenantId?: number };
@@ -1520,6 +1530,11 @@ export default async function adminRoutes(server: FastifyInstance) {
}); });
} catch (err: any) { } catch (err: any) {
console.error("ERROR /admin/tenant-imports:", err); console.error("ERROR /admin/tenant-imports:", err);
if (temporaryArchivePath) {
await unlink(temporaryArchivePath).catch((cleanupError) => {
console.error("ERROR cleanup temporary tenant import archive:", cleanupError);
});
}
if (archiveStoragePath) { if (archiveStoragePath) {
await s3.send(new DeleteObjectCommand({ await s3.send(new DeleteObjectCommand({
Bucket: secrets.S3_BUCKET, Bucket: secrets.S3_BUCKET,