mirror of
https://git.joinfirefish.org/firefish/firefish.git
synced 2024-05-18 20:11:10 +02:00
chore (backend): remove promisify
This commit is contained in:
parent
e8b39be387
commit
9ef5350a00
|
@ -1,5 +1,4 @@
|
||||||
import * as fs from "node:fs";
|
import * as fs from "node:fs/promises";
|
||||||
import * as util from "node:util";
|
|
||||||
import Logger from "@/services/logger.js";
|
import Logger from "@/services/logger.js";
|
||||||
import { createTemp } from "./create-temp.js";
|
import { createTemp } from "./create-temp.js";
|
||||||
import { downloadUrl } from "./download-url.js";
|
import { downloadUrl } from "./download-url.js";
|
||||||
|
@ -16,7 +15,7 @@ export async function downloadTextFile(url: string): Promise<string> {
|
||||||
// write content at URL to temp file
|
// write content at URL to temp file
|
||||||
await downloadUrl(url, path);
|
await downloadUrl(url, path);
|
||||||
|
|
||||||
const text = await util.promisify(fs.readFile)(path, "utf8");
|
const text = await fs.readFile(path, "utf-8");
|
||||||
|
|
||||||
return text;
|
return text;
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import * as fs from "node:fs";
|
import * as fs from "node:fs";
|
||||||
import * as stream from "node:stream";
|
import * as stream from "node:stream/promises";
|
||||||
import * as util from "node:util";
|
|
||||||
import got, * as Got from "got";
|
import got, * as Got from "got";
|
||||||
import { config } from "@/config.js";
|
import { config } from "@/config.js";
|
||||||
import { getAgentByHostname, StatusError } from "./fetch.js";
|
import { getAgentByHostname, StatusError } from "./fetch.js";
|
||||||
|
@ -10,8 +9,6 @@ import IPCIDR from "ip-cidr";
|
||||||
import PrivateIp from "private-ip";
|
import PrivateIp from "private-ip";
|
||||||
import { isValidUrl } from "./is-valid-url.js";
|
import { isValidUrl } from "./is-valid-url.js";
|
||||||
|
|
||||||
const pipeline = util.promisify(stream.pipeline);
|
|
||||||
|
|
||||||
export async function downloadUrl(url: string, path: string): Promise<void> {
|
export async function downloadUrl(url: string, path: string): Promise<void> {
|
||||||
if (!isValidUrl(url)) {
|
if (!isValidUrl(url)) {
|
||||||
throw new StatusError("Invalid URL", 400);
|
throw new StatusError("Invalid URL", 400);
|
||||||
|
@ -84,7 +81,7 @@ export async function downloadUrl(url: string, path: string): Promise<void> {
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await pipeline(req, fs.createWriteStream(path));
|
await stream.pipeline(req, fs.createWriteStream(path));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Got.HTTPError) {
|
if (e instanceof Got.HTTPError) {
|
||||||
throw new StatusError(
|
throw new StatusError(
|
||||||
|
|
|
@ -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 crypto from "node:crypto";
|
||||||
import * as stream from "node:stream";
|
import * as stream from "node:stream/promises";
|
||||||
import * as util from "node:util";
|
|
||||||
import { fileTypeFromFile } from "file-type";
|
import { fileTypeFromFile } from "file-type";
|
||||||
import probeImageSize from "probe-image-size";
|
import probeImageSize from "probe-image-size";
|
||||||
import isSvg from "is-svg";
|
import isSvg from "is-svg";
|
||||||
|
@ -9,8 +9,6 @@ import sharp from "sharp";
|
||||||
import { encode } from "blurhash";
|
import { encode } from "blurhash";
|
||||||
import { inspect } from "node:util";
|
import { inspect } from "node:util";
|
||||||
|
|
||||||
const pipeline = util.promisify(stream.pipeline);
|
|
||||||
|
|
||||||
export type FileInfo = {
|
export type FileInfo = {
|
||||||
size: number;
|
size: number;
|
||||||
md5: string;
|
md5: string;
|
||||||
|
@ -163,7 +161,7 @@ export async function checkSvg(path: string) {
|
||||||
try {
|
try {
|
||||||
const size = await getFileSize(path);
|
const size = await getFileSize(path);
|
||||||
if (size > 1 * 1024 * 1024) return false;
|
if (size > 1 * 1024 * 1024) return false;
|
||||||
return isSvg(fs.readFileSync(path));
|
return isSvg(await fs.readFile(path, "utf-8"));
|
||||||
} catch {
|
} catch {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -173,8 +171,7 @@ export async function checkSvg(path: string) {
|
||||||
* Get file size
|
* Get file size
|
||||||
*/
|
*/
|
||||||
export async function getFileSize(path: string): Promise<number> {
|
export async function getFileSize(path: string): Promise<number> {
|
||||||
const getStat = util.promisify(fs.stat);
|
return (await fs.stat(path)).size;
|
||||||
return (await getStat(path)).size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -182,7 +179,7 @@ export async function getFileSize(path: string): Promise<number> {
|
||||||
*/
|
*/
|
||||||
async function calcHash(path: string): Promise<string> {
|
async function calcHash(path: string): Promise<string> {
|
||||||
const hash = crypto.createHash("md5").setEncoding("hex");
|
const hash = crypto.createHash("md5").setEncoding("hex");
|
||||||
await pipeline(fs.createReadStream(path), hash);
|
await stream.pipeline(createReadStream(path), hash);
|
||||||
return hash.read();
|
return hash.read();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +193,7 @@ async function detectImageSize(path: string): Promise<{
|
||||||
hUnits: string;
|
hUnits: string;
|
||||||
orientation?: number;
|
orientation?: number;
|
||||||
}> {
|
}> {
|
||||||
const readable = fs.createReadStream(path);
|
const readable = createReadStream(path);
|
||||||
const imageSize = await probeImageSize(readable);
|
const imageSize = await probeImageSize(readable);
|
||||||
readable.destroy();
|
readable.destroy();
|
||||||
return imageSize;
|
return imageSize;
|
||||||
|
@ -214,7 +211,7 @@ function getBlurhash(path: string): Promise<string> {
|
||||||
.toBuffer((err, buffer, { width, height }) => {
|
.toBuffer((err, buffer, { width, height }) => {
|
||||||
if (err) return reject(err);
|
if (err) return reject(err);
|
||||||
|
|
||||||
let hash;
|
let hash: string;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7);
|
hash = encode(new Uint8ClampedArray(buffer), width, height, 7, 7);
|
||||||
|
|
Loading…
Reference in a new issue