file-server - support new DriveFile w/ GridFS on '/:id/:name'
This commit is contained in:
parent
2ce3179d50
commit
28a39bccf9
1 changed files with 16 additions and 5 deletions
|
@ -128,17 +128,28 @@ app.get('/:id/:name', async (req, res) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const file = await File.findOne({ _id: new mongodb.ObjectID(req.params.id) });
|
const fileId = new mongodb.ObjectID(req.params.id)
|
||||||
|
const file = await DriveFile.findOne({ _id: fileId });
|
||||||
|
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
res.status(404).sendFile(`${__dirname}/assets/dummy.png`);
|
res.status(404).sendFile(`${__dirname}/assets/dummy.png`);
|
||||||
return;
|
return;
|
||||||
} else if (file.data == null) {
|
|
||||||
res.sendStatus(400);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send(file.data.buffer, file.type, req, res);
|
const bucket = await getGridFSBucket()
|
||||||
|
|
||||||
|
const buffer = await ((id): Promise<Buffer> => new Promise((resolve, reject) => {
|
||||||
|
const chunks = []
|
||||||
|
const readableStream = bucket.openDownloadStream(id)
|
||||||
|
readableStream.on('data', chunk => {
|
||||||
|
chunks.push(chunk);
|
||||||
|
})
|
||||||
|
readableStream.on('end', () => {
|
||||||
|
resolve(Buffer.concat(chunks))
|
||||||
|
})
|
||||||
|
}))(fileId)
|
||||||
|
|
||||||
|
send(buffer, file.metadata.type, req, res);
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = app;
|
module.exports = app;
|
||||||
|
|
Loading…
Reference in a new issue