Unable to use "resume-parser" to parsing a simple pdf in Firebase using Node js

Viewed 409

I am trying to make a api for parsing a simple pdf in firebase. I am able to upload a resume using my api under any given user (using Bearer token), but after uploading a pdf, I made a pdf url. Then using that pdf url, I was trying to using "resume-parser" library for parsing the pdf, but it seems like not responding when I check in postman.

Here is the code for making api:

    const firebase = require("firebase");
    firebase.initializeApp(config);

    const dataBase = require("../models");
    const axios = require("axios");
    const moment = require("moment");
    const User = dataBase.User;
    
    //.... I have login and authentication code here and which are working fine.
    // Then I want to use this pdf parsing code 

    exports.pdfparse2 = (req, res) => {
    const BusBoy = require("busboy");
    const path = require("path");
    const os = require("os");
    const fs = require("fs");

    var busboy = new BusBoy({ headers: req.headers });
    const bucket = admin.storage().bucket("mybucketname.appspot.com");
    let mimtype;
    var saveTo;
    let pdfFileName;

    busboy.on("file", function(name, file, filename, encoding, mimetype) {
     console.log(
      "File [" +
        name +
        "]: filename: " +
        filename +
        ", encoding: " +
        encoding +
        ", mimetype: " +
        mimetype
    );
    const imageExtension = filename.split(".")[filename.split(".").length - 1];
    var fname = filename + "." + imageExtension;
    pdfFileName = filename;
    saveTo = path.join(os.tmpdir(), filename);
    file.pipe(fs.createWriteStream(saveTo));
    mimtype = mimetype;
  });

   busboy.on("finish", async function() {
     await bucket
       .upload(saveTo, {
         resumable: false,
         gzip: true,
         metadata: {
           metadata: {
             contentType: mimtype
           }
         }
       })
       .then(() => {
         const pdfUrl = `https://storage.googleapis.com/mybucketname.appspot.com/${pdfFileName}`;
         return db.doc(`/users/${req.user.userId}`).update({ pdfUrl });
         ResumeParser.parseResumeUrl(pdfUrl) // url
         .then(data => {
           resumeData = {
           link: pdfUrl
          };
          db.doc(`/users/${req.user.userId}`).set(
             {
              resumeList: admin.firestore.FieldValue.arrayUnion(resumeData)
            },
            { merge: true }
           );

        //console.log('Yay! ', data);
          return res.status(200).json({
            resume_data: data,
            resume_link: pdfUrl
          });
        })
        .catch(error => {
          console.error(error);
        }); 
      })
      .then(() => {
        return res.json({ message: "Image Uploaded Successfully" });
      })
      .catch(err => {
        console.error(err);
        return res
          .status(400)
          .send(JSON.stringify(err, ["message", "arguments", "type", "name"]));
      });

    res.end();
  });
  req.pipe(busboy);
};

Then I checked in postman, it is giving me only json output {"message": "Image Uploaded Successfully"}, but pdf parsing is now working. postman.png

Could anybody help me with that?

0 Answers
Related