perf: use read stream to transfer data

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2022-07-08 13:25:20 +08:00
parent 0864e414f4
commit 37e42972af
3 changed files with 5 additions and 9 deletions

View File

@@ -2,7 +2,6 @@ import { existsSync, statSync } from 'fs'
import { readFile, readdir, rm, writeFile } from 'fs/promises'
import mkdirp from 'mkdirp'
import { join, resolve } from 'path'
import { Readable } from 'stream'
import { quiet } from 'zx-cjs'
import {
@@ -116,10 +115,7 @@ export class BackupService {
async getFileStream(dirname: string) {
const path = this.checkBackupExist(dirname)
const stream = new Readable()
stream.push(await readFile(path))
stream.push(null)
const stream = fs.createReadStream(path)
return stream
}

View File

@@ -47,7 +47,7 @@ export class FileController {
const mimetype = lookup(ext)
try {
const buffer = await this.service.getFile(type, name)
const stream = this.service.getFileStream(type, name)
if (mimetype) {
reply.type(mimetype)
reply.header('cache-control', 'public, max-age=31536000')
@@ -57,7 +57,7 @@ export class FileController {
)
}
reply.send(buffer)
reply.send(stream)
} catch {
throw new CannotFindException()
}

View File

@@ -24,8 +24,8 @@ export class FileService {
.catch(() => false)
}
async getFile(type: FileType, name: string) {
return await fs.readFile(this.resolveFilePath(type, name))
getFileStream(type: FileType, name: string) {
return fs.createReadStream(this.resolveFilePath(type, name))
}
writeFile(