|
1 | 1 | import { |
| 2 | + BadRequestException, |
2 | 3 | Injectable, |
3 | 4 | NotFoundException, |
4 | 5 | ServiceUnavailableException, |
5 | | - BadRequestException, |
| 6 | + StreamableFile, |
6 | 7 | } from '@nestjs/common' |
7 | 8 | import { InjectConnection, InjectModel } from '@nestjs/mongoose' |
8 | 9 | import { FastifyReply, FastifyRequest } from 'fastify' |
9 | | -import { Http2ServerResponse } from 'http2' |
10 | 10 | import { GridFSBucket, ObjectId } from 'mongodb' |
11 | 11 | import { Connection, Model, mongo } from 'mongoose' |
12 | | -import { File } from './models/file.entity' |
13 | 12 | import { Stream } from 'stream' |
| 13 | +import { File } from './models/file.entity' |
14 | 14 |
|
15 | 15 | type Request = FastifyRequest |
16 | 16 | type Response = FastifyReply |
@@ -60,7 +60,11 @@ export class AppService { |
60 | 60 | }) |
61 | 61 | } |
62 | 62 |
|
63 | | - async download(id: string, request: Request, response: Response) { |
| 63 | + async download( |
| 64 | + id: string, |
| 65 | + request: Request, |
| 66 | + response: Response, |
| 67 | + ): Promise<StreamableFile> { |
64 | 68 | try { |
65 | 69 | if (!ObjectId.isValid(id)) { |
66 | 70 | throw new BadRequestException(null, 'InvalidVideoId') |
@@ -93,18 +97,10 @@ export class AppService { |
93 | 97 | 'Content-Disposition': `attachment; filename="${fileInfo.filename}"`, |
94 | 98 | }) |
95 | 99 |
|
96 | | - response.raw.on('close', () => { |
97 | | - readstream.destroy() |
98 | | - }) |
99 | | - |
100 | | - response.send(readstream) |
| 100 | + return new StreamableFile(readstream) |
101 | 101 | } else { |
102 | 102 | const readstream = this.bucket.openDownloadStream(oId) |
103 | 103 |
|
104 | | - response.raw.on('close', () => { |
105 | | - readstream.destroy() |
106 | | - }) |
107 | | - |
108 | 104 | response.status(200) |
109 | 105 | response.headers({ |
110 | 106 | 'Accept-Range': 'bytes', |
|
0 commit comments