Home > other >  Get all images exif and pass to route as object
Get all images exif and pass to route as object

Time:02-27

how to get all Images exif and pass it to view in Expressjs

basically it has to wait for 2 promisses

get filnames with readdir and get exifs and return as array to view

here what I have but lost.

const express = require('express');
const exifr = require('exifr');
const fs = require('fs');
const fsPromises = fs.promises;

const app = express();
 
app.set('view engine', 'ejs')

let defaultOptions = {
// Segments (JPEG APP Segment, PNG Chunks, HEIC Boxes, etc...)
tiff: false,
xmp: false,
icc: false,
iptc: true,
jfif: false, // (jpeg only)
ihdr: false, // (png only)
// Sub-blocks inside TIFF segment
ifd0: false, // aka image
ifd1: false, // aka thumbnail
exif: false,
gps: true, 
}
 

const photosDir = './photos/'
 
 
app.use('/photos', express.static('photos'))
async function listDir() {
  try {
    return fsPromises.readdir(photosDir) .then(filenames => {
      for (let filename of filenames) {
          //console.log(filename)
            exifr.parse(photosDir filename, defaultOptions).then(function(data){
              console.log(data)
              return data
            })
      }
  });
  } catch (err) {
    console.error('Error occured while reading directory!', err);
  }
}
listDir()
 
app.get('/', async (_,res) => {

 let data =  await listDir() 
console.log(data)
  res.render('index',{"files":  data })
});

app.listen(3000, () => console.log('Example app is listening on port 3000.'));

CodePudding user response:

Use Promise.all, this will resolve after all exifr.parse promises have resolved.

return fsPromises.readdir(photosDir)
.then(filenames => Promise.all(filenames.map(filename =>
  exifr.parse(photosDir filename, defaultOptions)
  .then(function(data) {
    console.log(data)
    return data
  })
)));
  • Related