diff options
| author | otofune <otofune@gmail.com> | 2017-11-07 09:14:39 +0900 |
|---|---|---|
| committer | otofune <otofune@gmail.com> | 2017-11-07 09:14:39 +0900 |
| commit | 8e62cc1efd6dc1710e7faa7d3ad2086425573cf5 (patch) | |
| tree | 2bab2ccd12edff0a4f4b63a233633bd28d8dc8b3 /src | |
| parent | add 'format' script to use autofix w/ tslint (diff) | |
| download | misskey-8e62cc1efd6dc1710e7faa7d3ad2086425573cf5.tar.gz misskey-8e62cc1efd6dc1710e7faa7d3ad2086425573cf5.tar.bz2 misskey-8e62cc1efd6dc1710e7faa7d3ad2086425573cf5.zip | |
file - unify '/:id' & '/:id/:name'
Diffstat (limited to 'src')
| -rw-r--r-- | src/file/server.ts | 42 |
1 files changed, 7 insertions, 35 deletions
diff --git a/src/file/server.ts b/src/file/server.ts index 39e21d10ec..e83acd4f24 100644 --- a/src/file/server.ts +++ b/src/file/server.ts @@ -86,11 +86,7 @@ function send(data: Buffer, type: string, req: express.Request, res: express.Res } } -/** - * Routing - */ - -app.get('/:id', async (req, res) => { +async function sendFileById (req: express.Request, res: express.Response): Promise<void> { // Validate id if (!mongodb.ObjectID.isValid(req.params.id)) { res.status(400).send('incorrect id'); @@ -119,37 +115,13 @@ app.get('/:id', async (req, res) => { }))(fileId); send(buffer, file.metadata.type, req, res); -}); - -app.get('/:id/:name', async (req, res) => { - // Validate id - if (!mongodb.ObjectID.isValid(req.params.id)) { - res.status(400).send('incorrect id'); - return; - } - - 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; - } - - 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); +/** + * Routing + */ - send(buffer, file.metadata.type, req, res); -}); +app.get('/:id', sendFileById); +app.get('/:id/:name', sendFileById); module.exports = app; |