From 5211d954cabb1472d1e394f721efaf64620432aa Mon Sep 17 00:00:00 2001 From: devmhand Date: Fri, 30 Jan 2026 17:55:29 +0400 Subject: [PATCH] third part --- gemini.js | 11 +++--- package.json | 8 +++-- src/routes/upload.js | 86 ++++++++++++++++++++++++++++++++++++++++++++ src/server.js | 9 ++++- 4 files changed, 104 insertions(+), 10 deletions(-) create mode 100644 src/routes/upload.js diff --git a/gemini.js b/gemini.js index 4925a10..496b1ec 100644 --- a/gemini.js +++ b/gemini.js @@ -63,11 +63,11 @@ async function pepe(){ console.log(response.text) } -async function shneine(){ +async function shneine(fileName, mimeType){ const myfile = await ai.files.upload({ - file:'./pedik.ogg', + file:`./uploads/${fileName}`, config:{ - mimeType:'audio/ogg', + mimeType:mimeType, } }); @@ -80,7 +80,8 @@ async function shneine(){ }) console.log(response.text) } -//await shneine(); + + async function fawatafa() { @@ -103,5 +104,3 @@ async function fawatafa() { const fileName = 'outMarat5.wav'; await saveWaveFile(fileName, audioBuffer); } - -await fawatafa(); \ No newline at end of file diff --git a/package.json b/package.json index 809e279..9306b44 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,9 @@ "version": "1.0.0", "main": "index.js", "scripts": { - "dev": "nodemon src/server.js", - "start": "node src/server.js", - "test": "echo \"Error: no test specified\" && exit 1" + "dev": "nodemon src/server.js", + "start": "node src/server.js", + "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", @@ -14,9 +14,11 @@ "dependencies": { "@google/genai": "^1.38.0", "axios": "^1.13.2", + "cors": "^2.8.6", "dotenv": "^17.2.3", "express": "^5.2.1", "jsonwebtoken": "^9.0.3", + "multer": "^2.0.2", "node-telegram-bot-api": "^0.67.0", "pg": "^8.17.2", "wav": "^1.0.2" diff --git a/src/routes/upload.js b/src/routes/upload.js new file mode 100644 index 0000000..12b0ccf --- /dev/null +++ b/src/routes/upload.js @@ -0,0 +1,86 @@ +import { Router } from "express"; +import multer from "multer"; +import path from "path"; +import fs from "fs"; +import { authMiddleware } from "../middleware/auth.js"; + +import { GoogleGenAI, createUserContent, createPartFromUri } from "@google/genai"; +import dotenv from "dotenv"; +dotenv.config(); + +import wav from 'wav'; +import { text } from "stream/consumers"; + +async function shneine(fileName, mimeType){ + const myfile = await ai.files.upload({ + file:`./uploads/${fileName}`, + config:{ + mimeType:mimeType, + } + }); + + const response = await ai.models.generateContent({ + model: "gemini-3-flash-preview", + contents: createUserContent([ + createPartFromUri(myfile.uri, myfile.mimeType), + 'Нужно сделать анализ аудиофайла, краткую выжимку самого важного, вывести тезисы.' + ]) + }) + return response.text; +} + + +// The client gets the API key from the environment variable `GEMINI_API_KEY`. +const ai = new GoogleGenAI({apiKey: process.env.GEMINI_KEY}); + +const router = Router(); + +// создаём папку uploads, если её нет +const uploadDir = "uploads"; +if (!fs.existsSync(uploadDir)) { + fs.mkdirSync(uploadDir); +} + +// настройка хранилища +const storage = multer.diskStorage({ + destination: (req, file, cb) => { + cb(null, uploadDir); + }, + filename: (req, file, cb) => { + const uniqueName = + Date.now() + "-" + Math.round(Math.random() * 1e9); + + const ext = path.extname(file.originalname); + cb(null, uniqueName + ext); + }, +}); + +const upload = multer({ + storage, + limits: { + fileSize: 50 * 1024 * 1024, // 5 MB + }, +}); + + +// POST /upload +router.post( + "/", + //authMiddleware, // 🔒 если нужен Bearer token + upload.single("file"), // имя поля = file + async (req, res) => { + if (!req.file) { + return res.status(400).json({ message: "Файл не передан" }); + } + + const aiText = await shneine(req.file.filename, req.file.mimetype); + + res.json({ + message: "Файл загружен", + text: aiText, + }); + } +); + +export default router; + diff --git a/src/server.js b/src/server.js index 1b05926..09f2e2a 100644 --- a/src/server.js +++ b/src/server.js @@ -1,23 +1,30 @@ import express from "express"; import dotenv from "dotenv"; +import cors from 'cors' + import authRoutes from "./routes/auth.js"; import userRoutes from "./routes/user.js"; +import uploadRoutes from "./routes/upload.js"; dotenv.config(); const app = express(); app.use(express.json()); +app.use(cors()); app.use("/auth", authRoutes); app.use("/user", userRoutes); +app.use("/upload", uploadRoutes); + app.get("/health", (req, res) => { res.json({ status: "ok" }); }); const PORT = process.env.PORT || 3000; -app.listen(PORT, () => { +const HOST = "0.0.0.0" +app.listen(PORT,"0.0.0.0", () => { console.log(`🚀 Server started on http://localhost:${PORT}`); });