diff --git a/packages/backend/src/misc/download-text-file.ts b/packages/backend/src/misc/download-text-file.ts index 9d3821b20f..a1e615263f 100644 --- a/packages/backend/src/misc/download-text-file.ts +++ b/packages/backend/src/misc/download-text-file.ts @@ -1,5 +1,4 @@ -import * as fs from "node:fs"; -import * as util from "node:util"; +import * as fs from "node:fs/promises"; import Logger from "@/services/logger.js"; import { createTemp } from "./create-temp.js"; import { downloadUrl } from "./download-url.js"; @@ -16,7 +15,7 @@ export async function downloadTextFile(url: string): Promise { // write content at URL to temp file await downloadUrl(url, path); - const text = await util.promisify(fs.readFile)(path, "utf8"); + const text = await fs.readFile(path, "utf-8"); return text; } finally { diff --git a/packages/backend/src/misc/download-url.ts b/packages/backend/src/misc/download-url.ts index f516265106..e3066ff333 100644 --- a/packages/backend/src/misc/download-url.ts +++ b/packages/backend/src/misc/download-url.ts @@ -1,6 +1,5 @@ import * as fs from "node:fs"; -import * as stream from "node:stream"; -import * as util from "node:util"; +import * as stream from "node:stream/promises"; import got, * as Got from "got"; import { config } from "@/config.js"; import { getAgentByHostname, StatusError } from "./fetch.js"; @@ -10,8 +9,6 @@ import IPCIDR from "ip-cidr"; import PrivateIp from "private-ip"; import { isValidUrl } from "./is-valid-url.js"; -const pipeline = util.promisify(stream.pipeline); - export async function downloadUrl(url: string, path: string): Promise { if (!isValidUrl(url)) { throw new StatusError("Invalid URL", 400); @@ -84,7 +81,7 @@ export async function downloadUrl(url: string, path: string): Promise { }); try { - await pipeline(req, fs.createWriteStream(path)); + await stream.pipeline(req, fs.createWriteStream(path)); } catch (e) { if (e instanceof Got.HTTPError) { throw new StatusError( diff --git a/packages/backend/src/misc/get-file-info.ts b/packages/backend/src/misc/get-file-info.ts index dadbda9e9e..532bbeec56 100644 --- a/packages/backend/src/misc/get-file-info.ts +++ b/packages/backend/src/misc/get-file-info.ts @@ -1,7 +1,7 @@ -import * as fs from "node:fs"; +import * as fs from "node:fs/promises"; +import { createReadStream } from "node:fs"; import * as crypto from "node:crypto"; -import * as stream from "node:stream"; -import * as util from "node:util"; +import * as stream from "node:stream/promises"; import { fileTypeFromFile } from "file-type"; import probeImageSize from "probe-image-size"; import isSvg from "is-svg"; @@ -9,8 +9,6 @@ import sharp from "sharp"; import { encode } from "blurhash"; import { inspect } from "node:util"; -const pipeline = util.promisify(stream.pipeline); - export type FileInfo = { size: number; md5: string; @@ -163,7 +161,7 @@ export async function checkSvg(path: string) { try { const size = await getFileSize(path); if (size > 1 * 1024 * 1024) return false; - return isSvg(fs.readFileSync(path)); + return isSvg(await fs.readFile(path, "utf-8")); } catch { return false; } @@ -173,8 +171,7 @@ export async function checkSvg(path: string) { * Get file size */ export async function getFileSize(path: string): Promise { - const getStat = util.promisify(fs.stat); - return (await getStat(path)).size; + return (await fs.stat(path)).size; } /** @@ -182,7 +179,7 @@ export async function getFileSize(path: string): Promise { */ async function calcHash(path: string): Promise { const hash = crypto.createHash("md5").setEncoding("hex"); - await pipeline(fs.createReadStream(path), hash); + await stream.pipeline(createReadStream(path), hash); return hash.read(); } @@ -196,7 +193,7 @@ async function detectImageSize(path: string): Promise<{ hUnits: string; orientation?: number; }> { - const readable = fs.createReadStream(path); + const readable = createReadStream(path); const imageSize = await probeImageSize(readable); readable.destroy(); return imageSize; @@ -214,7 +211,7 @@ function getBlurhash(path: string): Promise { .toBuffer((err, buffer, { width, height }) => { if (err) return reject(err); - let hash; + let hash: string; try { hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7);