Skip to content

Commit

Permalink
allow multer config to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi committed Jan 10, 2025
1 parent e81dddf commit 066b493
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 23 deletions.
49 changes: 27 additions & 22 deletions src/api/generated/api/resources/imdb/service/ImdbService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import express from "express";
import multer from "multer";
import path from "path";
import * as errors from "../../../../errors/index";
import * as serializers from "../../../../serialization/index";
import * as FernApi from "../../../index";
Expand Down Expand Up @@ -58,27 +57,9 @@ export interface ImdbServiceMethods {

export class ImdbService {
private router;
private upload;
// private upload;

constructor(private readonly methods: ImdbServiceMethods, middleware: express.RequestHandler[] = []) {
// Configure multer storage
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`);
}
});

// Initialize multer with storage config
this.upload = multer({
storage: storage,
limits: {
fileSize: 1024 * 1024 * 100 // 100MB file size limit for videos
}
});

this.router = express.Router({ mergeParams: true }).use(
express.json({
strict: false,
Expand All @@ -92,7 +73,12 @@ export class ImdbService {
return this;
}

public toRouter(): express.Router {
public toRouter(multerConfig?: {
storage?: multer.StorageEngine;
limits?: {
fileSize?: number;
};
}): express.Router {
this.router.post("/create-movie", async (req, res, next) => {
const request = serializers.CreateMovieRequest.parse(req.body);
if (request.ok) {
Expand Down Expand Up @@ -167,27 +153,42 @@ export class ImdbService {
next(error);
}
});
this.router.post("/:movieId/upload", this.upload.single('video') as any, async (req, res, next) => {
this.router.post("/:movieId/upload", (multer(multerConfig).single('video') as unknown as express.RequestHandler), async (req, res, next) => {
console.log('Starting upload movie request...');
try {
console.log('Request headers:', req.headers);
console.log('Request params:', req.params);
console.log('Request body:', req.body);

if (!req.file) {
console.log('Error: Missing video file');
res.status(400).json({
error: "Missing required video file"
});
return;
}
console.log('Uploaded file details:', {
filename: req.file.filename,
size: req.file.size,
mimetype: req.file.mimetype
});

const length = parseFloat(req.body.length);
if (!length) {
console.log('Error: Missing or invalid length parameter');
res.status(400).json({
error: "Missing required length parameter"
});
return;
}
console.log('Movie length:', length);

console.log('Calling uploadMovie method...');
await this.methods.uploadMovie(
req as any,
{
send: async (responseBody) => {
console.log('Upload successful, sending response');
res.json(responseBody);
},
cookie: res.cookie.bind(res),
Expand All @@ -197,12 +198,15 @@ export class ImdbService {
);
next();
} catch (error) {
console.log('Error occurred during upload:', error);
if (error instanceof multer.MulterError) {
if (error.code === 'LIMIT_FILE_SIZE') {
console.log('File size limit exceeded');
res.status(400).json({
error: 'File size is too large. Max size is 100MB'
});
} else {
console.log('Multer error:', error.code);
res.status(400).json({
error: error.message
});
Expand All @@ -217,6 +221,7 @@ export class ImdbService {
);
await error.send(res);
} else {
console.error('Internal server error:', error);
res.status(500).json("Internal Server Error");
}
next(error);
Expand Down
9 changes: 8 additions & 1 deletion src/api/generated/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
*/

import express from "express";
import multer from "multer";
import { ImdbService } from "./api/resources/imdb/service/ImdbService";

export function register(
expressApp: express.Express | express.Router,
services: {
imdb: ImdbService;
},
multerConfig: {
storage?: multer.StorageEngine;
limits?: {
fileSize?: number;
};
}
): void {
(expressApp as any).use("/movies", services.imdb.toRouter());
(expressApp as any).use("/movies", services.imdb.toRouter(multerConfig));
}
14 changes: 14 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import cors from "cors";
import express from "express";
import multer from "multer";
import path from "path";
import { register } from "./api/generated";
import imdb from "./services/imdb";

Expand All @@ -12,6 +14,18 @@ app.use(cors());

register(app, {
imdb,
}, {
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, '../');
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`);
}
}),
limits: {
fileSize: 1024 * 1024 * 100 // 100MB file size limit for videos
}
});

app.listen(PORT);
Expand Down

0 comments on commit 066b493

Please sign in to comment.