first commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import type { Request, Response } from "express";
|
||||
import * as oracleService from "../services/oracleStorageService";
|
||||
|
||||
const BUCKET = process.env.BUCKET_NAME!;
|
||||
if (!BUCKET) throw new Error("Faltan variables de entorno: BUCKET_NAME");
|
||||
|
||||
const streamToBuffer = async (
|
||||
stream: ReadableStream<any> | NodeJS.ReadableStream
|
||||
): Promise<Buffer> => {
|
||||
if ("getReader" in stream) {
|
||||
// Es un ReadableStream web
|
||||
const reader = stream.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
chunks.push(value);
|
||||
}
|
||||
return Buffer.concat(chunks.map((chunk) => Buffer.from(chunk)));
|
||||
} else {
|
||||
// Es un NodeJS ReadableStream
|
||||
const chunks: Buffer[] = [];
|
||||
return new Promise((resolve, reject) => {
|
||||
stream.on("data", (chunk) =>
|
||||
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk))
|
||||
);
|
||||
stream.on("end", () => resolve(Buffer.concat(chunks)));
|
||||
stream.on("error", reject);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
export const downloadFile = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const { nombre } = req.params;
|
||||
if (!nombre) return res.status(400).json({ error: "Falta parámetro nombre" });
|
||||
|
||||
const obj = await oracleService.descargarArchivo(BUCKET, nombre);
|
||||
// obj.value es un ReadableStream web estándar
|
||||
const body: Buffer = await streamToBuffer(obj.value);
|
||||
|
||||
res.setHeader("Content-Disposition", `attachment; filename="${nombre}"`);
|
||||
res.send(body);
|
||||
} catch (err) {
|
||||
console.error("Error en downloadArchivo:", err);
|
||||
res.status(500).json({ error: "Error al descargar archivo" });
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { Request, Response } from "express";
|
||||
import DataProfile from "../models/dataprofile";
|
||||
import { processFile } from "../services/processFile";
|
||||
import { validateOrganization } from "../services/validateOrganization";
|
||||
import type { FileUploadParams } from "../models/types";
|
||||
|
||||
export const uploadOrganizationFile = async (req: Request, res: Response) => {
|
||||
try {
|
||||
const data = await validateOrganization(req, res);
|
||||
const procesados: FileUploadParams[] = [];
|
||||
|
||||
for (const file of data.files) {
|
||||
const result = await processFile(file);
|
||||
const { objectName, originalName, bufferToUpload, mimeType } = result;
|
||||
|
||||
await DataProfile.create({
|
||||
objectName,
|
||||
originalName,
|
||||
size: bufferToUpload.length,
|
||||
mimeType,
|
||||
userId: data.sessionUser,
|
||||
organizationId: data.organizationId,
|
||||
});
|
||||
|
||||
procesados.push({ originalName, objectName });
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
message: `${procesados.length} archivos subidos y registrados`,
|
||||
files: procesados,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Error en uploadArchivos:", err);
|
||||
return res.status(500).json({ error: "Error al subir archivos" });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user