-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftp.js
34 lines (30 loc) · 917 Bytes
/
ftp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const ftp = require("ftp");
const { headers } = require("./get");
const logger = require("./logger");
const ftpGet = (loc, fileName, response) => {
const ftpClient = new ftp();
ftpClient.on("ready", () => {
ftpClient.get(fileName, (error, stream) => {
if (error) {
logger.error(error);
// never do this - there's no reason
response.writeHead(500, headers);
response.end(`internal server error: ${error}`);
} else {
logger.debug("closing stream");
stream.once("close", () => {
ftpClient.end();
});
stream.pipe((output) => {
logger.debug("got file!");
response.writeHead(200, headers);
response.end(output);
});
}
});
});
logger.debug(`connecting to ${loc} to retrieve ${fileName}`);
// connect as anonymous user
ftpClient.connect(loc);
};
module.exports = ftpGet;