Skip to content

Commit

Permalink
Resumes (#12)
Browse files Browse the repository at this point in the history
* google download

* Shit works b

* deprecate var

* added stuff
  • Loading branch information
David Castaneda authored Jan 30, 2019
1 parent 1981f43 commit 1036acd
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"hydrate": "node ./src/utils/hydrate.js"
},
"dependencies": {
"axios": "^0.18.0",
"backpack-core": "^0.8.1",
"body-parser": "^1.18.3",
"cors": "^2.8.4",
Expand All @@ -29,6 +30,7 @@
"nodemailer": "^4.7.0",
"nodemailer-express-handlebars": "^3.0.0",
"nodemailer-mailgun-transport": "^1.4.0",
"url-parse": "^1.4.4",
"winston": "^3.1.0",
"winston-daily-rotate-file": "^3.5.1"
}
Expand Down
16 changes: 15 additions & 1 deletion src/controllers/cabinet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Applicant from "../models/applicant";

import httpResponse from "../utils/httpResponses";

import googleService from "../services/google/drive";

const confirmed = async (req, res) => {
try {
const confirmed = await Applicant.find({ confirmation: true }).exec();
Expand Down Expand Up @@ -42,4 +44,16 @@ const males = async (req, res) => {
}
};

export default { confirmed, unconfirmed, females, males };
const download = async (req, res) => {
try {
const { id } = req.query;

const file_path = await googleService.download(id);

res.download(file_path);
} catch (e) {
httpResponse.failureResponse(res, e);
}
};

export default { confirmed, unconfirmed, females, males, download };
1 change: 1 addition & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ apiRouter.get(
tokenAuthMiddleware.validateToken,
cabinet.unconfirmed
);
apiRouter.get("/cabinet/download", cabinet.download);

/* ------ Prereg signup Route ------ */
// Deprecating this route, this alert is no longer needed
Expand Down
46 changes: 45 additions & 1 deletion src/services/google/drive.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { google } from "googleapis";
import { Duplex } from "stream";
import path from "path";
import fs from "fs";
import { WriteStream } from "tty";
import axios from "axios";

const {
GOOGLE_CLIENT_EMAIL,
Expand Down Expand Up @@ -27,6 +31,33 @@ const asyncUpload = (drive, payload) =>
});
});

const asyncDownload = fileId =>
new Promise((resolve, reject) => {
const file_path = path.resolve(`tmp/${fileId}.pdf`);
const dest = fs.createWriteStream(file_path);

const drive = google.drive("v3");

drive.files.get(
{ auth, fileId: fileId, alt: "media", mimeType: "application/pdf" },
{
responseType: "stream"
},
(err, response) => {
if (err) throw err;

response.data
.on("error", err => {
throw err;
})
.on("end", () => {
resolve(file_path);
})
.pipe(dest);
}
);
});

const bufferToStream = buffer => {
let stream = new Duplex();
stream.push(buffer);
Expand Down Expand Up @@ -54,4 +85,17 @@ const upload = async (file, filename, folder) => {
}
};

export default { upload };
const download = async fileId => {
try {
await auth.authorize();

const dest = await asyncDownload(fileId);

return dest;
} catch (e) {
console.log(e);
throw e;
}
};

export default { upload, download };

0 comments on commit 1036acd

Please sign in to comment.