I'm having trouble using npm exiftool. (https://www.npmjs.com/package/exiftool) I'm trying to do some stuffs using that.
- Iterate images files through specific folder
- Get 'xpKeywords' data of each image files.
- Write file storing data.
Here's my code.
const fs = require('fs');
const exif = require('exiftool');
const folderName = 'testImages';
const inputPath = `/Users/myName/Project/project/${folderName}`;
const files = fs.readdirSync(inputPath, 'utf8');
let data = [];
(async () => {
let promises = [];
files.forEach(file => promises.push(fs.readFileSync(`${inputPath}/${file}`)));
let results = await Promise.all(promises);
for(let [index, result] of results.entries()) {
let datum = await getMetadata(result, index);
console.log("out");
data.push(datum);
}
fs.writeFileSync('outputData/metaData.json', JSON.stringify(data, null, 4), (error) => {
console.log('Error Occurred');
});
})();
async function getMetadata(result, index) {
console.log(`get metadata ${index}`);
await exif.metadata(result, (err, metadata) => {
return {
name: files[index],
hashTags: metadata.xpKeywords
};
});
}
After running that code, the file metaData.json is not what I'd expected.
[ null, null, null, null, null, null ]
I think I'm having some trouble about the usage of async function in function getMetadata.