I’m trying to test something in my NodeJS app in Postman where I want to be able to upload files. Now, the uploading itself works fine and I can just pass bunch of files in the multipart/form-data
:
async upload(req: Request, res: Response): Promise<void> {
try {
const user = await req.getUser();
const { isAdmin, orgId, type: fileType, isLogo, isIcon } = req.query;
const name = user.fullName;
const altText = req.query.altText as string;
const isAdminBool = util.toBool(isAdmin as string | undefined);
const isLogoBool = util.toBool(isLogo as string | undefined);
const isIconBool = util.toBool(isIcon as string | undefined);
// Resolve destination folder based on conditions
const destFolder = isAdminBool
? 'files/admin'
: orgId
? `files/organization/${orgId}`
: `files/user/${user.id}`;
// Get files from the request
const files = req.files as Express.Multer.File[];
// Create a list of promises to handle file uploads
const promUploads = files.map(async (file) => {
const origname = file.originalname;
let filename = origname;
// Rename filename for icons
if (fileType === 'icon') {
const ext = path.extname(origname);
if (!origname.endsWith(`.ico${ext}`)) {
filename = `${path.basename(origname, ext)}.ico${ext}`;
}
}
await this.storageService.isExists({ ...file, originalname: filename }, user.azure_id);
return this.storageService.copyAndUploadFile(file.path, {
delete: true,
renameFilepath: `${destFolder}/${filename}`,
category: fileType as string | undefined,
uploadedBy: `${user.firstname} ${user.lastname}`,
isIcon: isIconBool,
isLogo: isLogoBool,
title: name,
altText
});
});
const result = await Promise.all(promUploads);
res.jsonSuccess(result);
} catch (error) {
console.error('Error uploading images:', error);
res.jsonError({ message: 'Failed to upload images', error });
}
}
What I need to be able to do now is that I want to be able to edit the properties of each file as I upload them. So if I have 5 files being uploaded, I want to be able to edit the name
, altText
, isLogo
and isIcon
independently.
So suppose I have 5 images being uploaded at the same time. I’d like to be able to change the metadata etc of all those 5 images.
Any guidance or assistance on how I can do this?