Skip to content

Commit 61e5134

Browse files
authored
Merge pull request #302 from Carifio24/tempfile-updates
Updates to temporary file setup
2 parents 2b67c2c + 491fbfd commit 61e5134

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/app.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Express } from "express";
1+
import { Express, RequestHandler } from "express";
22
import session from "express-session";
33
import bodyParser from "body-parser";
44
import cookieParser from "cookie-parser";
@@ -16,7 +16,13 @@ import { schemas } from "./openapi/schemas";
1616
import { COSMICDS_OPENAPI_VERSION, COSMICDS_OPENAPI_APIKEY_SCHEME, COSMICDS_OPENAPI_TAGS } from "./openapi/options";
1717
import { registerSwaggerDocs } from "./openapi/utils";
1818

19-
export const uploader = multer({ storage: multer.memoryStorage() });
19+
const MAX_SIZE_MB = 5;
20+
export const uploader = multer({
21+
storage: multer.memoryStorage(),
22+
limits: {
23+
fileSize: MAX_SIZE_MB * 1024 * 1024,
24+
}
25+
});
2026

2127
export function setupApp(app: Express, db: Sequelize) {
2228

@@ -72,7 +78,22 @@ export function setupApp(app: Express, db: Sequelize) {
7278
app.use(apiKeyMiddleware);
7379

7480
// parse requests of content-type - application/json
75-
app.use(bodyParser.json());
81+
// Skip paths where we aren't going to be uploading JSON
82+
// e.g. temporary file paths
83+
const jsonParser = bodyParser.json();
84+
function skipForEndpoints(endpoints: [string, string[]][]): RequestHandler {
85+
return function (req, res, next) {
86+
for (const [path, methods] of endpoints) {
87+
if (req.path.startsWith(path) && methods.some(method => req.method.toLowerCase() == method)) {
88+
return next();
89+
}
90+
}
91+
return jsonParser(req, res, next);
92+
};
93+
}
94+
app.use(skipForEndpoints([
95+
["/temp", ["post", "patch"]],
96+
]));
7697

7798
// parse requests of content-type - application/x-www-form-urlencoded
7899
app.use(bodyParser.urlencoded({ extended: true }));

src/server.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,7 +2611,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {
26112611

26122612
/**
26132613
* @openapi
2614-
* /temp/{uuid}
2614+
* /temp/{uuid}:
26152615
* patch:
26162616
* tags:
26172617
* - temporary
@@ -2740,7 +2740,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {
27402740
* 200:
27412741
* description: The requested temporary file exists and its content has been returned. The MIME type will match the file's contents.
27422742
* content:
2743-
* * / *:
2743+
* application/octet-stream:
27442744
* schema:
27452745
* type: string
27462746
* format: binary
@@ -2757,7 +2757,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {
27572757
* schema:
27582758
* $ref: "#/components/schemas/Error"
27592759
*
2760-
*/
2760+
*/
27612761
app.get("/temp/:uuid", async (req, res) => {
27622762
const uuid = req.params.uuid;
27632763
if (!validateUUID(uuid)) {

0 commit comments

Comments
 (0)