added server side validation for both image upload forms

This commit is contained in:
Joonas 2023-01-14 16:36:39 +02:00
parent b73338a0f3
commit c592853852
2 changed files with 15 additions and 1 deletions

View File

@ -16,6 +16,13 @@ export async function action({ request }) {
directory: './public/uploads/',
maxPartSize: 500000,
file: ({ filename }) => filename,
filter: data => {
const fileTypes = ['jpeg', 'jpg', 'png', 'gif']
// if sent file is not an image, don't handle it
if (!fileTypes.includes(data.contentType.split('/')[1])) return false;
return true;
},
});
const errors = {};
@ -25,7 +32,7 @@ export async function action({ request }) {
multiPartformdata = await unstable_parseMultipartFormData(request, fileUploadHandler);
imageName = multiPartformdata.get("image").name;
} catch (err) {
errors.image = "Image size too big";
errors.image = "Image size too big or file sent is not an image";
}
if (typeof title !== "string" || title.length > 50 || title.length < 3) {

View File

@ -17,6 +17,13 @@ export async function action({ request, params }) {
directory: './public/uploads/',
maxPartSize: 500000,
file: ({ filename }) => filename,
filter: data => {
const fileTypes = ['jpeg', 'jpg', 'png', 'gif']
// if sent file is not an image, don't handle it
if (!fileTypes.includes(data.contentType.split('/')[1])) return false;
return true;
},
});
const errors = {};