outreach/lib/attachmentHandler.js
2025-08-15 01:03:38 -08:00

73 lines
1.8 KiB
JavaScript

const fs = require("fs").promises;
const path = require("path");
const config = require("../config");
class AttachmentHandler {
constructor() {
this.attachmentsDir = path.join(__dirname, "..", "attachments");
}
async getAttachments() {
if (!config.attachments.enabled || config.attachments.files.length === 0) {
return [];
}
const attachments = [];
for (const filename of config.attachments.files) {
try {
const filePath = path.join(this.attachmentsDir, filename);
// Check if file exists
await fs.access(filePath);
// Get file stats for size validation
const stats = await fs.stat(filePath);
// Skip files larger than 10MB to avoid email size issues
if (stats.size > 10 * 1024 * 1024) {
console.warn(`Skipping ${filename}: File too large (>10MB)`);
continue;
}
attachments.push({
filename: filename,
path: filePath,
});
} catch (error) {
console.warn(`Unable to attach ${filename}: ${error.message}`);
}
}
return attachments;
}
// Helper to validate attachment types
isValidFileType(filename) {
const validExtensions = [
".pdf",
".doc",
".docx",
".txt",
".jpg",
".jpeg",
".png",
".gif",
];
const ext = path.extname(filename).toLowerCase();
return validExtensions.includes(ext);
}
// Get list of available attachments for logging
async listAvailableFiles() {
try {
const files = await fs.readdir(this.attachmentsDir);
return files.filter((file) => this.isValidFileType(file));
} catch (error) {
return [];
}
}
}
module.exports = new AttachmentHandler();