sharp icon indicating copy to clipboard operation
sharp copied to clipboard

rewrite existing file with buffer and fs responds with errno: -4094, code: 'UNKNOWN', syscall: 'open',

Open aabaza97 opened this issue 1 year ago • 1 comments

I'm using this function to check if an uploaded image matches a specified size and, if not, resize it to the desired dimensions before proceeding with the next middleware or route handler

const resizeUpload = (size) => async (req, res, next) => {
	try {
		if (!req.uploadInfo) throw errors.UnprocessableEntity('msg_no_uploads_to_process');
		const { uploadPath, filename } = req.uploadInfo;

		const filePath = path.join(uploadPath, filename);
		const image = sharp(filePath);
		const metadata = await image.metadata();

		const validWidth = metadata.width === size.width;
		const validHeight = metadata.height === size.height;

		if (!validWidth || !validHeight) {
			// resize image
			const fileBuffer = await image
				.resize({ width: 768, height: 1024 })
				.withMetadata({ density: 72 })
				.toFormat('jpg')
				.toBuffer();
			fs.writeFileSync(filePath, fileBuffer);
		}

		return next();
	} catch (error) {
		return next(error);
	}
};

It works just fine on MacOS, yet on Windows machine I get this error:

Error: UNKNOWN: unknown error, open 'C:\Users\User\Desktop\backend\core-backend\public\storage\user_model\1\vton\test\image\39294_00.jpg'
    at Object.openSync (node:fs:573:18)
    at Object.writeFileSync (node:fs:2361:35)
    at C:\Users\User\Desktop\backend\core-backend\src\middleware\uploader\resizeUpload.js:25:7 {
  errno: -4094,
  code: 'UNKNOWN',
  syscall: 'open',
  path: 'C:\\Users\\User\\Desktop\\backend\\core-backend\\public\\storage\\user_model\\1\\vton\\test\\image\\39294_00.jpg'
}

Note: The image to modify already exists and saved to disk.

aabaza97 avatar Jun 28 '24 17:06 aabaza97

sharp.cache(false);

https://sharp.pixelplumbing.com/api-utility#cache

lovell avatar Jun 29 '24 08:06 lovell

I hope this information helped. Please feel free to re-open with more details if further assistance is required.

lovell avatar Jul 16 '24 07:07 lovell