Choose files type/extension (mimetype) #11733
Replies: 6 comments 9 replies
-
In the meantime (I think this is already somewhere here), you can listen to action |
Beta Was this translation helpful? Give feedback.
-
I also needed a similar restriction on filetype and looked at using the It's a pity there isn't a files.upload filter where an error could be thrown before the upload takes place. I have a poc of this, but the issue is you then need to pass in and deal with a stream. |
Beta Was this translation helpful? Give feedback.
-
Is there any news on that topic? The only way I see now is to do all of this with a seperate custom endpoint and upon successful validation (including busboy) passing it on to the directus files. edit: I want to use a captcha for file upload including form fields. |
Beta Was this translation helpful? Give feedback.
-
Also, the mine type validation should be done on front and backend. |
Beta Was this translation helpful? Give feedback.
-
Here is my workaround using Custom API Hooks, given that I'd like to allow only import { defineHook } from '@directus/extensions-sdk';
import type { FailedValidationException as TFailedValidationException } from '@directus/shared/exceptions';
import type { FilterHandler } from '@directus/shared/types';
import type { ItemsService } from 'directus/dist/services';
import type { File } from 'directus/dist/types';
// import { validatePayload } from '@directus/shared/utils';
export default defineHook(async ({ filter }, { exceptions, services, getSchema }) => {
const { FailedValidationException }: { FailedValidationException: typeof TFailedValidationException } = exceptions;
const { FilesService } = services;
const filesService: ItemsService<File> = new FilesService({
schema: await getSchema(),
});
const filterHandler: FilterHandler<{ image?: string | null }> = async (payload) => {
if (payload.image) {
const file = await filesService.readOne(payload.image);
if (file?.type !== 'image/jpeg') {
throw new FailedValidationException({
message: '"image.type" must be [image/jpeg]',
path: ['image', 'type'],
type: 'any.only',
context: {
valids: ['image/jpeg'],
label: 'image.type',
value: file.type,
key: 'type',
},
});
}
/*
// Using directus validatePayload
const validationErrors = validatePayload(
{ "_and": [{"image": {"type": {"_eq": "image/jpeg"}}}] },
{ image: file }
);
if (validationErrors[0] && validationErrors[0].details[0]) {
throw new FailedValidationException(validationErrors[0].details[0]);
}
// */
}
return payload;
};
filter('news.items.create', filterHandler as FilterHandler);
filter('news.items.update', filterHandler as FilterHandler);
}); |
Beta Was this translation helpful? Give feedback.
-
Heya! Thanks for opening this feature request! This feature request has received over 15 votes from the community. This means we'll move this feature request to the Under Review state! The Core team will schedule a meeting to review this request as soon as possible. The discussion will then be approved or denied. You may or may not be invited to join this meeting with the core team. For more information, see our Feature Request Process. |
Beta Was this translation helpful? Give feedback.
-
Currently Directus allows to upload any file type when selecting field type as file or files while creating. Only Images are specifically supported. I had multiple usecases where we needed to upload specific files such as pdf, csv or mp4 (videos).
Feature Request - Option to select what mimetypes a given file type field would allow. Either a multi select dropdown to allow for images, pdfs, videos or a string to pass option like
image/jpeg, application/pdf
etcBeta Was this translation helpful? Give feedback.
All reactions