I have next code:
const buffer = Buffer.from(image, 'base64');
const { ext, mime } = await FileType.fromBuffer(buffer);
But it fails with next error:
UnhandledPromiseRejectionWarning: TypeError: Cannot destructure property 'ext' of '(intermediate value)' as it is undefined.
Image is just JPG file, which I can easy open in photo editor. But for some reason FileType can't parse it, so question is why?
You could have included that undefined
means no match, but I had to look it up here instead assuming this is indeed the package you are using.
@param buffer - A buffer representing file data. It works best if the buffer contains the entire file, it may work with a smaller portion as well.
@returns The detected file type and MIME type, or `undefined` when there is no match.
*/
function fromBuffer(buffer: Buffer | Uint8Array | ArrayBuffer): Promise<core.FileTypeResult | undefined>;
Have a look at the file that you create the buffer from with some sort of hex reader or heck, just have a look at the buffer you make in debug, does it start with the common signature for jpeg files? find them here
If you're using async-await and if the awaiting promise gets rejected then an error is thrown, are you wrapping your await call in a try-catch?
The result object is undefined and hence throws an error when accessing "ext" on it.
const buffer = Buffer.from(image, 'base64');
try{
const result = await FileType.fromBuffer(buffer);
const { ext, mime } = result;
}catch(err){
console.log('something is wrong'):
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.