"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.S3Service = void 0;
const fs = __importStar(require("fs"));
const fs_1 = require("fs");
const stream_1 = require("stream");
const util = __importStar(require("util"));
const pipelineAsync = util.promisify(stream_1.pipeline);
class S3Service {
    async uploadToPresignedUrl(presignedUrl, filePath) {
        try {
            const fileContent = fs.readFileSync(filePath);
            const fileStats = await fs.promises.stat(filePath);
            const response = await fetch(presignedUrl, {
                method: "PUT",
                headers: {
                    "Content-Length": String(fileStats.size),
                },
                body: fileContent,
                duplex: "half",
            });
            if (!response.ok) {
                const text = await response.text();
                throw new Error(`Upload failed with status code: ${response.status} - ${text}`);
            }
            console.log("File uploaded successfully to pre-signed URL");
        }
        catch (error) {
            console.error("Error uploading file to pre-signed URL:", error);
            throw error;
        }
    }
    async downloadFromPresignedUrl(presignedUrl, destinationPath) {
        try {
            const response = await fetch(presignedUrl);
            if (!response.ok) {
                throw new Error(`Download failed with status code: ${response.status}`);
            }
            const fileStream = (0, fs_1.createWriteStream)(destinationPath);
            await pipelineAsync(stream_1.Readable.from(response.body), fileStream);
            console.log(`File downloaded successfully from pre-signed URL to ${destinationPath}`);
        }
        catch (error) {
            console.error("Error downloading file from pre-signed URL:", error);
            throw error;
        }
    }
}
exports.S3Service = S3Service;
//# sourceMappingURL=S3.js.map