Sobesednik / node-exiftool

A Node.js interface to exiftool command-line application.
MIT License
91 stars 15 forks source link

Example request: request for example extracting from a list of filePaths #47

Closed ndtreviv closed 5 years ago

ndtreviv commented 5 years ago

For example:

Input is a list of files not necessarily in the same directory.

Output is an array of exif {...} objects.

Something like:

const filePaths = ['/dir1/photo1.jpg', '/dir2/photo1.jpg'];

ep.open()
  .then((pid) => console.log('Started exiftool process %s', pid))
  .then(() => {
// Something like this?
    return Promise.all(filePaths.map(filePath => {
      return ep.readMetadata(filePath, ['-File:all']); //TODO Somehow retrieve and flatten data
    }));
  })
  .then(console.log, console.error)
  .then(() => ep.close())
  .then(() => console.log('Closed exiftool'))
  .catch(console.error)
}
ndtreviv commented 5 years ago

In the end I did this:

return new Promise((resolve, reject) => {
    try {
    ep.open()
      .then(pid => log.info("Started exiftool process %s", pid))
      .then(() => {
        resolve(
          Promise.all(
            filePaths.map(filePath => {
              log.info("Extracting exif from image: " + filePath);
              return ep.readMetadata(filePath, OPTIONS).then(async data => {
                const hashes = await calculateHashes(filePath);
                if (data.error) { log.warn(data.error); }
                return {
                  filePath,
                  error: data.error,
                  data:
                    data.data && data.data.length
                      ? { ...data.data[0], ...hashes }
                      : hashes
                };
              });
            })
          )
        );
      })
      .then(() => ep.close())
      .then(() => log.info("Closed exiftool"))
      .catch(error => {
        log.error(error);
        log.info("Error 1", error);
        reject(error);
      });
    }catch (e) {
      log.info("Error 1", e);
    }
  });