Update MongoDB connector
This commit is contained in:
1 parent
ea8273556f
commit
dfc8928a71
6 files changed
+219
-230
No files matched your search
@@ -27,19 +27,27 @@ function listener(request, response) {
|
||||
const urlParts = url.parse(request.url, true);
|
||||
if (request.method === "GET") {
|
||||
const filename = querystring.unescape(urlParts.pathname.substring(1));
|
||||
const gs = new mongodb.GridStore(db.mongoDb, filename, "r", {});
|
||||
gs.open(err => {
|
||||
db.filesCollection.findOne({ _id: filename }, (err, file) => {
|
||||
if (err) {
|
||||
response.writeHead(500, { Connection: "close" });
|
||||
response.end(`${err.name}: ${err.message}`);
|
||||
throw err;
|
||||
}
|
||||
|
||||
if (!file) {
|
||||
response.writeHead(404);
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
const stream = gs.stream(true);
|
||||
|
||||
response.writeHead(200, {
|
||||
"Content-Type": "application/octet-stream",
|
||||
"Content-Length": gs.length
|
||||
"Content-Type": file.contentType || "application/octet-stream",
|
||||
"Content-Length": file.length
|
||||
});
|
||||
stream.pipe(response);
|
||||
|
||||
const bucket = new mongodb.GridFSBucket(db.client.db());
|
||||
const downloadStream = bucket.openDownloadStream(filename);
|
||||
downloadStream.pipe(response);
|
||||
});
|
||||
} else {
|
||||
response.writeHead(405, { Allow: "GET" });
|
||||
|
||||
Reference in new issue
Block a user