@andrewlwn77/s3-upload-mcp-server
Version:
Pure Node.js MCP server for uploading images to AWS S3 with high-performance validation using Sharp and file-type
138 lines • 5.4 kB
JavaScript
;
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.UniqueNameGenerator = void 0;
const uuid_1 = require("uuid");
const path = __importStar(require("path"));
const logger_1 = require("./logger");
class UniqueNameGenerator {
constructor() {
this.logger = logger_1.Logger.getInstance();
}
generateUniqueFilename(originalName, prefix, includeTimestamp = true) {
try {
if (!originalName || originalName.trim().length === 0) {
return {
success: false,
error: {
code: 'VALIDATION_ERROR',
message: 'Original filename cannot be empty'
}
};
}
const ext = path.extname(originalName);
const baseName = path.basename(originalName, ext);
if (!ext) {
return {
success: false,
error: {
code: 'VALIDATION_ERROR',
message: 'Filename must have an extension'
}
};
}
const parts = [];
// Add prefix if provided
if (prefix && prefix.trim().length > 0) {
parts.push(prefix.trim());
}
// Add base name (sanitized)
const sanitizedBaseName = baseName.replace(/[^a-zA-Z0-9._-]/g, '_');
parts.push(sanitizedBaseName);
// Add timestamp if requested
let timestamp = '';
if (includeTimestamp) {
timestamp = new Date().toISOString().replace(/[:.]/g, '-');
parts.push(timestamp);
}
// Add UUID for uniqueness
const uuid = (0, uuid_1.v4)().substring(0, 8);
parts.push(uuid);
const uniqueFilename = parts.join('_') + ext;
this.logger.debug('Generated unique filename', {
original: originalName,
unique: uniqueFilename,
timestamp,
uuid
});
return {
success: true,
unique_filename: uniqueFilename,
original_extension: ext,
timestamp
};
}
catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown generation error';
this.logger.error('Unique filename generation failed', { error: errorMessage, originalName });
return {
success: false,
error: {
code: 'GENERATION_ERROR',
message: errorMessage
}
};
}
}
generateS3Key(filename, prefix, includeDate = true) {
const parts = [];
// Add date-based prefix if requested
if (includeDate) {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
parts.push(year.toString(), month, day);
}
// Add custom prefix if provided
if (prefix && prefix.trim().length > 0) {
parts.push(prefix.trim());
}
// Add filename
parts.push(filename);
return parts.join('/');
}
sanitizeFilename(filename) {
// Remove or replace invalid characters for S3
return filename
.replace(/[<>:"|?*\x00-\x1f]/g, '_') // Replace invalid chars with underscore
.replace(/\s+/g, '_') // Replace spaces with underscore
.replace(/_{2,}/g, '_') // Replace multiple underscores with single
.replace(/^_|_$/g, '') // Remove leading/trailing underscores
.substring(0, 1024); // Limit length to 1024 chars
}
}
exports.UniqueNameGenerator = UniqueNameGenerator;
//# sourceMappingURL=unique-name-generator.js.map