mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Add Unsupported Media Type Exception (#11096)
* add unsuported media type exception * throw error when not multipart/form-data * use unsupportedMediaType when checking import file * add `UNSUPPORTED_MEDIA_TYPE` to Error Codes docs
This commit is contained in:
@@ -4,7 +4,7 @@ import express from 'express';
|
||||
import Joi from 'joi';
|
||||
import path from 'path';
|
||||
import env from '../env';
|
||||
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
|
||||
import { ForbiddenException, InvalidPayloadException, UnsupportedMediaTypeException } from '../exceptions';
|
||||
import { respond } from '../middleware/respond';
|
||||
import useCollection from '../middleware/use-collection';
|
||||
import { validateBatch } from '../middleware/validate-batch';
|
||||
@@ -18,7 +18,8 @@ const router = express.Router();
|
||||
router.use(useCollection('directus_files'));
|
||||
|
||||
const multipartHandler = asyncHandler(async (req, res, next) => {
|
||||
if (req.is('multipart/form-data') === false) return next();
|
||||
if (req.is('multipart/form-data') === false)
|
||||
throw new UnsupportedMediaTypeException(`Unsupported Content-Type header`);
|
||||
|
||||
let headers: BusboyHeaders;
|
||||
|
||||
|
||||
@@ -2,7 +2,12 @@ import argon2 from 'argon2';
|
||||
import { Router } from 'express';
|
||||
import Joi from 'joi';
|
||||
import { nanoid } from 'nanoid';
|
||||
import { ForbiddenException, InvalidPayloadException, InvalidQueryException } from '../exceptions';
|
||||
import {
|
||||
ForbiddenException,
|
||||
InvalidPayloadException,
|
||||
InvalidQueryException,
|
||||
UnsupportedMediaTypeException,
|
||||
} from '../exceptions';
|
||||
import collectionExists from '../middleware/collection-exists';
|
||||
import { respond } from '../middleware/respond';
|
||||
import { RevisionsService, UtilsService, ImportService } from '../services';
|
||||
@@ -94,6 +99,9 @@ router.post(
|
||||
'/import/:collection',
|
||||
collectionExists,
|
||||
asyncHandler(async (req, res, next) => {
|
||||
if (req.is('multipart/form-data') === false)
|
||||
throw new UnsupportedMediaTypeException(`Unsupported Content-Type header`);
|
||||
|
||||
const service = new ImportService({
|
||||
accountability: req.accountability,
|
||||
schema: req.schema,
|
||||
|
||||
@@ -14,5 +14,6 @@ export * from './range-not-satisfiable';
|
||||
export * from './route-not-found';
|
||||
export * from './service-unavailable';
|
||||
export * from './unprocessable-entity';
|
||||
export * from './unsupported-media-type';
|
||||
export * from './user-suspended';
|
||||
export * from './unexpected-response';
|
||||
|
||||
7
api/src/exceptions/unsupported-media-type.ts
Normal file
7
api/src/exceptions/unsupported-media-type.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { BaseException } from '@directus/shared/exceptions';
|
||||
|
||||
export class UnsupportedMediaTypeException extends BaseException {
|
||||
constructor(message: string, extensions?: Record<string, unknown>) {
|
||||
super(message, 415, 'UNSUPPORTED_MEDIA_TYPE', extensions);
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ import { Knex } from 'knex';
|
||||
import getDatabase from '../database';
|
||||
import { AbstractServiceOptions, SchemaOverview } from '../types';
|
||||
import { Accountability } from '@directus/shared/types';
|
||||
import { ForbiddenException, InvalidPayloadException } from '../exceptions';
|
||||
import { ForbiddenException, InvalidPayloadException, UnsupportedMediaTypeException } from '../exceptions';
|
||||
import StreamArray from 'stream-json/streamers/StreamArray';
|
||||
import { ItemsService } from './items';
|
||||
import { queue } from 'async';
|
||||
@@ -42,7 +42,7 @@ export class ImportService {
|
||||
case 'text/csv':
|
||||
return await this.importCSV(collection, stream);
|
||||
default:
|
||||
throw new InvalidPayloadException(`Can't import files of type "${mimetype}"`);
|
||||
throw new UnsupportedMediaTypeException(`Can't import files of type "${mimetype}"`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user