const { S3Client, PutObjectCommand, DeleteObjectCommand, ListObjectsV2Command, GetObjectCommand, HeadObjectCommand, } = require('@aws-sdk/client-s3'); const { getSignedUrl } = require('@aws-sdk/s3-request-presigner'); const BUCKET = process.env.R2_BUCKET_NAME; // "bet" const ACCOUNT_ID = process.env.R2_ACCOUNT_ID; const r2 = new S3Client({ region: 'auto', endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`, credentials: { accessKeyId: process.env.R2_ACCESS_KEY_ID, secretAccessKey: process.env.R2_SECRET_ACCESS_KEY, }, }); const USER_QUOTA_BYTES = 45 * 1024 * 1024; // 45 MB /** * Upload a file buffer to R2. * @param {string} key - e.g. "cvbora/users/{userId}/uploads/resume.pdf" * @param {Buffer} buffer * @param {string} contentType * @returns {Promise<{key, size}>} */ async function uploadFile(key, buffer, contentType) { const command = new PutObjectCommand({ Bucket: BUCKET, Key: key, Body: buffer, ContentType: contentType, ContentLength: buffer.length, }); await r2.send(command); return { key, size: buffer.length }; } /** * Upload a UTF-8 string (e.g. HTML) to R2. */ async function uploadText(key, text, contentType = 'text/html; charset=utf-8') { const buffer = Buffer.from(text, 'utf-8'); return uploadFile(key, buffer, contentType); } /** * Delete an object from R2. * @param {string} key */ async function deleteFile(key) { const command = new DeleteObjectCommand({ Bucket: BUCKET, Key: key }); await r2.send(command); } /** * List all objects under a prefix. * @param {string} prefix - e.g. "cvbora/users/{userId}/" * @returns {Promise>} */ async function listFiles(prefix) { const command = new ListObjectsV2Command({ Bucket: BUCKET, Prefix: prefix, }); const response = await r2.send(command); return (response.Contents || []).map(obj => ({ key: obj.Key, size: obj.Size, lastModified: obj.LastModified, })); } /** * Get the size of a single object. * @param {string} key * @returns {Promise} size in bytes */ async function getObjectSize(key) { try { const command = new HeadObjectCommand({ Bucket: BUCKET, Key: key }); const response = await r2.send(command); return response.ContentLength || 0; } catch { return 0; } } /** * Generate a presigned GET URL (1 hour expiry). * @param {string} key * @returns {Promise} */ async function getPresignedUrl(key, expiresInSeconds = 3600) { const command = new GetObjectCommand({ Bucket: BUCKET, Key: key }); return getSignedUrl(r2, command, { expiresIn: expiresInSeconds }); } /** * Build the R2 key prefix for a user's CVBora files. * @param {string} userId * @param {'uploads'|'generated'} folder */ function userPrefix(userId, folder) { return `cvbora/users/${userId}/${folder}/`; } /** * Download the full content of an R2 object as a Buffer. * @param {string} key * @returns {Promise} */ async function getFileContent(key) { const command = new GetObjectCommand({ Bucket: BUCKET, Key: key }); const response = await r2.send(command); // response.Body is a ReadableStream (AWS SDK v3) const chunks = []; for await (const chunk of response.Body) { chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk); } return Buffer.concat(chunks); } module.exports = { uploadFile, uploadText, deleteFile, listFiles, getObjectSize, getFileContent, getPresignedUrl, userPrefix, USER_QUOTA_BYTES, };