From fa2df2a3cdc6c8cfa20966f33119a257fd914da9 Mon Sep 17 00:00:00 2001 From: idranme Date: Sun, 4 Aug 2024 23:11:59 +0800 Subject: [PATCH] opt --- src/common/utils/file.ts | 22 ++++++++-------------- src/ntqqapi/hook.ts | 2 +- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/common/utils/file.ts b/src/common/utils/file.ts index c400e80..2b2aafa 100644 --- a/src/common/utils/file.ts +++ b/src/common/utils/file.ts @@ -1,11 +1,9 @@ -import fs from 'fs' -import fsPromise from 'fs/promises' -import util from 'util' +import fs from 'node:fs' +import fsPromise from 'node:fs/promises' import path from 'node:path' import { log, TEMP_DIR } from './index' import { dbUtil } from '../db' import * as fileType from 'file-type' -import { net } from 'electron' import { randomUUID, createHash } from 'node:crypto' export function isGIF(path: string) { @@ -36,7 +34,6 @@ export function checkFileReceived(path: string, timeout: number = 3000): Promise } export async function file2base64(path: string) { - const readFile = util.promisify(fs.readFile) let result = { err: '', data: '', @@ -52,7 +49,7 @@ export async function file2base64(path: string) { result.err = e.toString() return result } - const data = await readFile(path) + const data = await fsPromise.readFile(path) // 转换为Base64编码 result.data = data.toString('base64') } catch (err) { @@ -90,7 +87,6 @@ export interface HttpDownloadOptions { headers?: Record | string } export async function httpDownload(options: string | HttpDownloadOptions): Promise { - let chunks: Buffer[] = [] let url: string let headers: Record = { 'User-Agent': @@ -108,12 +104,10 @@ export async function httpDownload(options: string | HttpDownloadOptions): Promi } } } - const fetchRes = await net.fetch(url, { headers }) + const fetchRes = await fetch(url, { headers }) if (!fetchRes.ok) throw new Error(`下载文件失败: ${fetchRes.statusText}`) - const blob = await fetchRes.blob() - let buffer = await blob.arrayBuffer() - return Buffer.from(buffer) + return Buffer.from(await fetchRes.arrayBuffer()) } type Uri2LocalRes = { @@ -152,7 +146,7 @@ export async function uri2local(uri: string, fileName: string = null): Promise|]/g, '_') res.fileName = fileName filePath = path.join(TEMP_DIR, randomUUID() + fileName) - fs.writeFileSync(filePath, buffer) + await fsPromise.writeFile(filePath, buffer) } catch (e: any) { res.errMsg = `${url}下载失败,` + e.toString() return res @@ -217,7 +211,7 @@ export async function uri2local(uri: string, fileName: string = null): Promise