Skip to content

🐸 Rybbit - open-source and privacy-friendly alternative to Google Analytics that is 10x more intuitive.

Fix Umami endpoint

Edward Cui4281ae0dd492
diff --git a/server/src/api/import/umami.ts b/server/src/api/import/umami.ts
index 15ee2f0f8..de504baf3 100644
--- a/server/src/api/import/umami.ts
+++ b/server/src/api/import/umami.ts
@@ -1,48 +1,68 @@
import { FastifyRequest, FastifyReply } from "fastify";
-import boss from "../../lib/boss.js";
+import { pipeline } from "stream/promises";
+import { createWriteStream } from "fs";
import { getUserHasAccessToSite } from "../../lib/auth-utils.js";
+import fs from "fs/promises";
+import path from "path";
+import boss from "../../lib/boss.js";
+
+type Platform = "umami";
interface ImportUmamiRequest {
Params: {
site: string;
};
+ Body: {
+ platform: Platform;
+ }
}
-const MAX_FILE_SIZE = 100 * 1024 * 1024; // 100MB
+const IMPORT_DIR = "/tmp/imports";
export async function importUmami(request: FastifyRequest<ImportUmamiRequest>, reply: FastifyReply) {
const { site } = request.params;
+ const { platform } = request.body;
const userHasAccess = await getUserHasAccessToSite(request, site);
if (!userHasAccess) {
return reply.status(403).send({ error: "Forbidden" });
}
- const part = await request.file();
-
- if (!part) {
+ const data = await request.file();
+ if (!data) {
return reply.status(400).send({ error: "No file uploaded." });
}
- const chunks = [];
- let totalSize = 0;
- for await (const chunk of part.file) {
- totalSize += chunk.length;
- if (totalSize > MAX_FILE_SIZE) {
- return reply.status(400).send({ error: `File ${part.filename} is larger than the 100MB limit.` });
- }
- chunks.push(chunk);
+ if (!data.filename.endsWith(".csv")) {
+ return reply.status(400).send({ error: "Invalid file type. Only .csv files are accepted." });
}
- const fileContent = Buffer.concat(chunks).toString("utf-8");
- const file = { fileName: part.filename, fileContent };
- if (!file.fileName.endsWith(".csv")) {
- return reply.status(400).send({ error: `Invalid file name format for ${file.fileName}.` });
- }
+ const batchId = crypto.randomUUID();
+ const tempFilePath = path.join(IMPORT_DIR, `${batchId}-${data.filename}`);
- const importId = crypto.randomUUID();
+ try {
+ // Ensure the import directory exists
+ await fs.mkdir(IMPORT_DIR, { recursive: true });
+
+ // Stream the file directly to the disk. This is memory efficient.
+ await pipeline(data.file, createWriteStream(tempFilePath));
+ } catch (error) {
+ console.error("Error saving uploaded file:", error);
+ return reply.status(500).send({ error: "Failed to process uploaded file." });
+ }
- await boss.send("umami-import", { file, site_id: Number(site), importId });
+ // 4. Queue the Initiation Job
+ // We only send the file's location, not its content.
+ await boss.send("import-initiation", {
+ filePath: tempFilePath,
+ platform: platform,
+ siteId: site,
+ batchId,
+ });
- reply.send({ success: true, importId });
+ reply.status(202).send({
+ success: true,
+ message: "Import accepted and is now being processed.",
+ batchId,
+ });
}