chore: Normalize fs-extra usage

This commit is contained in:
Tom Moor
2023-11-15 19:43:17 -05:00
parent 726613bf1d
commit cf6a946c9c
5 changed files with 19 additions and 26 deletions

View File

@@ -2,14 +2,7 @@ import { Blob } from "buffer";
import { mkdir, unlink, rmdir } from "fs/promises";
import path from "path";
import { Readable } from "stream";
import {
ReadStream,
close,
pathExists,
createReadStream,
createWriteStream,
open,
} from "fs-extra";
import fs from "fs-extra";
import invariant from "invariant";
import JWT from "jsonwebtoken";
import safeResolvePath from "resolve-path";
@@ -48,13 +41,13 @@ export default class LocalStorage extends BaseStorage {
body,
key,
}: {
body: string | ReadStream | Buffer | Uint8Array | Blob;
body: string | fs.ReadStream | Buffer | Uint8Array | Blob;
contentLength?: number;
contentType?: string;
key: string;
acl?: string;
}) => {
const exists = await pathExists(this.getFilePath(key));
const exists = await fs.pathExists(this.getFilePath(key));
if (exists) {
throw ValidationError(`File already exists at ${key}`);
}
@@ -64,7 +57,7 @@ export default class LocalStorage extends BaseStorage {
});
let src: NodeJS.ReadableStream;
if (body instanceof ReadStream) {
if (body instanceof fs.ReadStream) {
src = body;
} else if (body instanceof Blob) {
src = Readable.from(Buffer.from(await body.arrayBuffer()));
@@ -75,10 +68,11 @@ export default class LocalStorage extends BaseStorage {
const filePath = this.getFilePath(key);
// Create the file on disk first
await open(filePath, "w").then(close);
await fs.open(filePath, "w").then(close);
return new Promise<string>((resolve, reject) => {
const dest = createWriteStream(filePath)
const dest = fs
.createWriteStream(filePath)
.on("error", reject)
.on("finish", () => resolve(this.getUrlForKey(key)));
@@ -138,7 +132,7 @@ export default class LocalStorage extends BaseStorage {
}
public getFileStream(key: string) {
return createReadStream(this.getFilePath(key));
return fs.createReadStream(this.getFilePath(key));
}
private getFilePath(key: string) {