diff options
| author | otofune <otofune@gmail.com> | 2017-11-06 15:39:16 +0900 |
|---|---|---|
| committer | otofune <otofune@gmail.com> | 2017-11-06 15:39:16 +0900 |
| commit | 28a39bccf96549a35ef77c10dce5f90f9f8cc654 (patch) | |
| tree | e07a223762f83937296522be7dc446e88584d04e /src | |
| parent | fileserver - fix dummy path (diff) | |
| download | sharkey-28a39bccf96549a35ef77c10dce5f90f9f8cc654.tar.gz sharkey-28a39bccf96549a35ef77c10dce5f90f9f8cc654.tar.bz2 sharkey-28a39bccf96549a35ef77c10dce5f90f9f8cc654.zip | |
file-server - support new DriveFile w/ GridFS on '/:id/:name'
Diffstat (limited to 'src')
| -rw-r--r-- | src/file/server.ts | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/src/file/server.ts b/src/file/server.ts index 068e88546b..f38599b89c 100644 --- a/src/file/server.ts +++ b/src/file/server.ts @@ -128,17 +128,28 @@ app.get('/:id/:name', async (req, res) => { 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) { res.status(404).sendFile(`${__dirname}/assets/dummy.png`); 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; |