"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uploadFile = uploadFile;
exports.deleteFile = deleteFile;
const uuid_1 = require("uuid");
const consts_1 = require("../../utils/consts");
const minio_1 = require("../../utils/minio");
function uploadFile(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        const buffer = yield data.document.toBuffer();
        const ext = data.document.filename.split(".").pop();
        const fileName = "folder/" + (0, uuid_1.v4)() + "." + ext;
        yield minio_1.minioClient.putObject(consts_1.bucketName, fileName, buffer);
        return reply.code(200).send({ document: fileName });
    });
}
function deleteFile(req, reply) {
    return __awaiter(this, void 0, void 0, function* () {
        const data = req.body;
        yield minio_1.minioClient.removeObject(consts_1.bucketName, data.path);
        return reply.code(200).send({});
    });
}
// export async function minio(
//   req: FastifyRequest<{
//     Body: MinioSchema;
//   }>,
//   reply: FastifyReply
// ) {
//   const data = req.body;
//   const fileName = "folder/" + data.file.filename;
//   const buffer = await data.file.toBuffer();
//   const res = await minioClient.putObject(bucketName, fileName, buffer);
//   // await minioClient.removeObject(bucketName, fileName);
//   console.log(res);
//   return reply.code(200).send({});
// }
// export async function minioGet(req: FastifyRequest, reply: FastifyReply) {
//   let size = 0;
// console.log(size);
// const dataStream = await minioClient.getObject(
//   bucketName,
//   "folder/IMG-20240508-WA0011.jpg"
// );
// dataStream.on("data", function (chunk) {
//   size += chunk.length;
// });
// dataStream.on("end", function () {
//   console.log("End. Total size = " + size);
// });
// dataStream.on("error", function (err) {
//   console.log(err);
// });
// const url = await getPresignedUrl("image.jpg");
// return reply.code(200).send(url);
// }
