"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.upload = upload; const client_s3_1 = require("@aws-sdk/client-s3"); const s3_request_presigner_1 = require("@aws-sdk/s3-request-presigner"); const fs_1 = require("fs"); const path_1 = require("path"); /** * Uploads a file to S3 and returns a private, time-limited URL to access it * @param filePath - Absolute or relative path to the file to upload * @param config - S3 configuration parameters * @returns Promise containing the result with URL if successful */ async function upload(id, filePath, config) { const fullPath = (0, path_1.resolve)(filePath); const fileName = (0, path_1.basename)(filePath); const extName = (0, path_1.extname)(filePath); const key = `${id}${extName}`; if (!key) { return { success: false, error: 'Could not create key' }; } let fileStream; try { fileStream = (0, fs_1.createReadStream)(fullPath); } catch (err) { const error = err; return { success: false, error: `Error reading file: ${error.message}` }; } const s3Client = new client_s3_1.S3Client({ region: config.region }); try { const uploadCommand = new client_s3_1.PutObjectCommand({ Bucket: config.bucketName, Key: key, Body: fileStream }); await s3Client.send(uploadCommand); const getCommand = new client_s3_1.GetObjectCommand({ Bucket: config.bucketName, Key: key }); const url = await (0, s3_request_presigner_1.getSignedUrl)(s3Client, getCommand, { expiresIn: config.expirationSeconds }); return { success: true, url, key }; } catch (err) { const error = err; return { success: false, error: `Error uploading to S3: ${error.message}` }; } } module.exports = { upload }; //# sourceMappingURL=index.js.map