Create routes/upload.js
Browse files- routes/upload.js +66 -0
routes/upload.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from "express";
|
| 2 |
+
import multer from "multer";
|
| 3 |
+
import mammoth from "mammoth";
|
| 4 |
+
import { MODEL_CAPS, MAX_FILE_BYTES } from "../lib/modelCaps.js";
|
| 5 |
+
|
| 6 |
+
const router = express.Router();
|
| 7 |
+
const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: MAX_FILE_BYTES } });
|
| 8 |
+
|
| 9 |
+
function extOf(name = "") { return (name.split(".").pop() || "").toLowerCase(); }
|
| 10 |
+
|
| 11 |
+
async function extractText(file) {
|
| 12 |
+
const ext = extOf(file.originalname);
|
| 13 |
+
if (["txt", "md", "csv", "json", "js", "ts", "py", "html", "css", "java", "c", "cpp"].includes(ext)) {
|
| 14 |
+
return file.buffer.toString("utf8");
|
| 15 |
+
}
|
| 16 |
+
if (ext === "docx") {
|
| 17 |
+
const { value } = await mammoth.extractRawText({ buffer: file.buffer });
|
| 18 |
+
return value;
|
| 19 |
+
}
|
| 20 |
+
if (ext === "pdf") {
|
| 21 |
+
// pdf-parse عبر import ديناميكي (CJS)
|
| 22 |
+
const pdfParse = (await import("pdf-parse")).default;
|
| 23 |
+
const data = await pdfParse(file.buffer);
|
| 24 |
+
return data.text;
|
| 25 |
+
}
|
| 26 |
+
return "";
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
// POST /api/upload (multipart: file, model)
|
| 30 |
+
router.post("/upload", upload.single("file"), async (req, res) => {
|
| 31 |
+
try {
|
| 32 |
+
const model = (req.body.model || "flash").toLowerCase();
|
| 33 |
+
const caps = MODEL_CAPS[model] || MODEL_CAPS.flash;
|
| 34 |
+
const file = req.file;
|
| 35 |
+
if (!file) return res.status(400).json({ error: "no_file" });
|
| 36 |
+
|
| 37 |
+
const ext = extOf(file.originalname);
|
| 38 |
+
const isImage = file.mimetype.startsWith("image/");
|
| 39 |
+
|
| 40 |
+
// تحقق من دعم النموذج لهذا النوع
|
| 41 |
+
if (isImage) {
|
| 42 |
+
if (!caps.vision || !caps.imageMimes.includes(file.mimetype)) {
|
| 43 |
+
return res.status(415).json({ error: "unsupported", message: `النموذج (${model}) لا يدعم الصور.` });
|
| 44 |
+
}
|
| 45 |
+
const b64 = file.buffer.toString("base64");
|
| 46 |
+
return res.json({
|
| 47 |
+
kind: "image",
|
| 48 |
+
name: file.originalname,
|
| 49 |
+
dataUrl: `data:${file.mimetype};base64,${b64}`,
|
| 50 |
+
});
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
if (!caps.docExts.includes(ext)) {
|
| 54 |
+
return res.status(415).json({ error: "unsupported", message: `النموذج (${model}) لا يدعم ملفات .${ext}` });
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
let text = await extractText(file);
|
| 58 |
+
if (text.length > 20000) text = text.slice(0, 20000) + "\n...[truncated]";
|
| 59 |
+
return res.json({ kind: "text", name: file.originalname, text });
|
| 60 |
+
} catch (err) {
|
| 61 |
+
console.error("upload error:", err);
|
| 62 |
+
res.status(500).json({ error: "upload_failed", message: String(err?.message || err) });
|
| 63 |
+
}
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
export default router;
|