Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Express } from "express";
import { Express, RequestHandler } from "express";
import session from "express-session";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
Expand All @@ -16,7 +16,13 @@ import { schemas } from "./openapi/schemas";
import { COSMICDS_OPENAPI_VERSION, COSMICDS_OPENAPI_APIKEY_SCHEME, COSMICDS_OPENAPI_TAGS } from "./openapi/options";
import { registerSwaggerDocs } from "./openapi/utils";

export const uploader = multer({ storage: multer.memoryStorage() });
const MAX_SIZE_MB = 5;
export const uploader = multer({
storage: multer.memoryStorage(),
limits: {
fileSize: MAX_SIZE_MB * 1024 * 1024,
}
});

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

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

// parse requests of content-type - application/json
app.use(bodyParser.json());
// Skip paths where we aren't going to be uploading JSON
// e.g. temporary file paths
const jsonParser = bodyParser.json();
function skipForEndpoints(endpoints: [string, string[]][]): RequestHandler {
return function (req, res, next) {
for (const [path, methods] of endpoints) {
if (req.path.startsWith(path) && methods.some(method => req.method.toLowerCase() == method)) {
return next();
}
}
return jsonParser(req, res, next);
};
}
app.use(skipForEndpoints([
["/temp", ["post", "patch"]],
]));

// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
Expand Down
6 changes: 3 additions & 3 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2611,7 +2611,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {

/**
* @openapi
* /temp/{uuid}
* /temp/{uuid}:
* patch:
* tags:
* - temporary
Expand Down Expand Up @@ -2740,7 +2740,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {
* 200:
* description: The requested temporary file exists and its content has been returned. The MIME type will match the file's contents.
* content:
* * / *:
* application/octet-stream:
* schema:
* type: string
* format: binary
Expand All @@ -2757,7 +2757,7 @@ export function createApp(db: Sequelize, options?: AppOptions): Express {
* schema:
* $ref: "#/components/schemas/Error"
*
*/
*/
app.get("/temp/:uuid", async (req, res) => {
const uuid = req.params.uuid;
if (!validateUUID(uuid)) {
Expand Down