perf: use read stream to transfer data
Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user