forked from nestjs/nest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.service.ts
42 lines (36 loc) 路 1.11 KB
/
app.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { Injectable, StreamableFile } from '@nestjs/common';
import { createReadStream, readFileSync } from 'fs';
import { join } from 'path';
import { Observable, of } from 'rxjs';
import { NonFile } from './non-file';
@Injectable()
export class AppService {
getReadStream(): StreamableFile {
return new StreamableFile(
createReadStream(join(process.cwd(), 'Readme.md')),
);
}
getBuffer(): StreamableFile {
return new StreamableFile(readFileSync(join(process.cwd(), 'Readme.md')));
}
getNonFile(): NonFile {
return new NonFile('Hello world');
}
getRxJSFile(): Observable<StreamableFile> {
return of(this.getReadStream());
}
getFileWithHeaders(): StreamableFile {
const file = readFileSync(join(process.cwd(), 'Readme.md'));
return new StreamableFile(
createReadStream(join(process.cwd(), 'Readme.md')),
{
type: 'text/markdown',
disposition: 'attachment; filename="Readme.md"',
length: file.byteLength,
},
);
}
getFileThatDoesNotExist(): StreamableFile {
return new StreamableFile(createReadStream('does-not-exist.txt'));
}
}