media-exporter-processor
Version:
Media processing API with thumbnail generation and cloud storage
168 lines • 6.52 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handler = void 0;
const hono_1 = require("hono");
const aws_lambda_1 = require("hono/aws-lambda");
const bearer_auth_1 = require("hono/bearer-auth");
const valibot_validator_1 = require("@hono/valibot-validator");
const VideoSchemas_1 = require("../schemas/VideoSchemas");
const VideoProcessingService_1 = require("../services/VideoProcessingService");
const ImageProcessingService_1 = require("../services/ImageProcessingService");
const ThumbnailService_1 = require("../services/ThumbnailService");
const UploadService_1 = require("../services/UploadService");
const Config_1 = require("../utils/Config");
const app = new hono_1.Hono();
// Initialize services (cached across invocations)
let videoProcessingService;
let imageProcessingService;
let initialized = false;
function initializeServices() {
if (!initialized) {
console.log("Initializing services...");
try {
const config = Config_1.Config.getInstance();
console.log("Config loaded successfully");
const uploadService = new UploadService_1.UploadService(config.r2Config);
const thumbnailService = new ThumbnailService_1.ThumbnailService();
videoProcessingService = new VideoProcessingService_1.VideoProcessingService(thumbnailService, uploadService);
imageProcessingService = new ImageProcessingService_1.ImageProcessingService(thumbnailService, uploadService);
initialized = true;
console.log("Services initialized successfully");
}
catch (error) {
console.error("Failed to initialize services:", error);
console.error("Error details:", error);
throw error;
}
}
}
// Bearer token authentication middleware
const authToken = process.env.AUTH_TOKEN;
if (!authToken) {
console.error("AUTH_TOKEN environment variable not set");
throw new Error("AUTH_TOKEN environment variable not set");
}
app.use("*", (0, bearer_auth_1.bearerAuth)({ token: authToken }));
// Health check endpoint
app.get("/", (c) => {
console.log("Health check endpoint hit");
return c.json({
success: true,
message: "Media Exporter Processor API",
version: "1.0.2",
timestamp: new Date().toISOString(),
});
});
// Chain route definitions to create routes variable
const routes = app
.post("/video", (0, valibot_validator_1.vValidator)("query", VideoSchemas_1.VideoQueryWithFileKeySchema), async (c) => {
try {
initializeServices();
// Get validated query parameters from middleware
const queryParams = c.req.valid("query");
const { fileKey, ...videoProcessingParams } = queryParams;
let videoBuffer;
if (fileKey) {
// Download video from S3 using fileKey
try {
videoBuffer =
await videoProcessingService.uploadService.downloadFile(fileKey);
}
catch (err) {
return c.json({
success: false,
error: "S3_DOWNLOAD_ERROR",
message: err instanceof Error
? err.message
: "Failed to download file from S3",
}, 400);
}
}
else {
// Get request body (old behavior)
const body = await c.req.arrayBuffer();
if (!body || body.byteLength === 0) {
return c.json({
success: false,
error: "NO_FILE",
message: "No video file provided in request body",
}, 400);
}
videoBuffer = Buffer.from(body);
}
// Process video directly in this Lambda
const result = await videoProcessingService.processVideo(videoBuffer, videoProcessingParams);
return c.json({
success: true,
message: "Video processed successfully",
data: result,
});
}
catch (error) {
console.error("Error processing video:", error);
return c.json({
success: false,
error: "PROCESSING_ERROR",
message: error instanceof Error
? error.message
: "Video processing failed",
}, 500);
}
})
.post("/image", (0, valibot_validator_1.vValidator)("query", VideoSchemas_1.ImageQueryWithFileKeySchema), async (c) => {
try {
initializeServices();
// Get validated query parameters from middleware
const queryParams = c.req.valid("query");
const { fileKey, ...imageProcessingParams } = queryParams;
let imageBuffer;
if (fileKey) {
// Download image from S3 using fileKey
try {
imageBuffer =
await imageProcessingService.uploadService.downloadFile(fileKey);
}
catch (err) {
return c.json({
success: false,
error: "S3_DOWNLOAD_ERROR",
message: err instanceof Error
? err.message
: "Failed to download file from S3",
}, 400);
}
}
else {
// Get request body (old behavior)
const body = await c.req.arrayBuffer();
if (!body || body.byteLength === 0) {
return c.json({
success: false,
error: "NO_FILE",
message: "No image file provided in request body",
}, 400);
}
imageBuffer = Buffer.from(body);
}
// Process image directly in this Lambda
const result = await imageProcessingService.processImage(imageBuffer, imageProcessingParams);
return c.json({
success: true,
message: "Image processed successfully",
data: result,
});
}
catch (error) {
console.error("Error processing image:", error);
return c.json({
success: false,
error: "PROCESSING_ERROR",
message: error instanceof Error
? error.message
: "Image processing failed",
}, 500);
}
});
// Export the handler directly for AWS Lambda
exports.handler = (0, aws_lambda_1.handle)(app);
//# sourceMappingURL=all-in-one.js.map