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

@@ -1,4 +1,4 @@
import fs from "fs";
import fs from "fs-extra";
import truncate from "lodash/truncate";
import { FileOperationState, NotificationEventType } from "@shared/types";
import { bytesToHumanReadable } from "@shared/utils/files";
@@ -89,7 +89,7 @@ export default abstract class ExportTask extends BaseTask<Props> {
state: FileOperationState.Uploading,
});
const stat = await fs.promises.stat(filePath);
const stat = await fs.stat(filePath);
const url = await FileStorage.store({
body: fs.createReadStream(filePath),
contentLength: stat.size,
@@ -129,7 +129,7 @@ export default abstract class ExportTask extends BaseTask<Props> {
throw error;
} finally {
if (filePath) {
void fs.promises.unlink(filePath).catch((error) => {
void fs.unlink(filePath).catch((error) => {
Logger.error(`Failed to delete temporary file ${filePath}`, error);
});
}

View File

@@ -1,5 +1,5 @@
import path from "path";
import { rm } from "fs-extra";
import fs from "fs-extra";
import truncate from "lodash/truncate";
import tmp from "tmp";
import {
@@ -248,7 +248,7 @@ export default abstract class ImportTask extends BaseTask<Props> {
fileOperation: FileOperation
) {
try {
await rm(dirPath, { recursive: true, force: true });
await fs.rm(dirPath, { recursive: true, force: true });
} catch (error) {
Logger.error(
`ImportTask failed to cleanup extracted data for ${fileOperation.id}`,

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) {

View File

@@ -1,7 +1,7 @@
import path from "path";
import util from "util";
import AWS, { S3 } from "aws-sdk";
import { createWriteStream, remove } from "fs-extra";
import fs from "fs-extra";
import invariant from "invariant";
import compact from "lodash/compact";
import tmp from "tmp";
@@ -172,10 +172,10 @@ export default class S3Storage extends BaseStorage {
return reject(err);
}
const tmpFile = path.join(tmpDir, "tmp");
const dest = createWriteStream(tmpFile);
const dest = fs.createWriteStream(tmpFile);
dest.on("error", reject);
dest.on("finish", () =>
resolve({ path: tmpFile, cleanup: () => remove(tmpFile) })
resolve({ path: tmpFile, cleanup: () => fs.rm(tmpFile) })
);
const stream = this.getFileStream(key);

View File

@@ -1,6 +1,5 @@
import fs from "fs";
import path from "path";
import { mkdirp } from "fs-extra";
import fs from "fs-extra";
import JSZip from "jszip";
import tmp from "tmp";
import yauzl from "yauzl";
@@ -115,7 +114,7 @@ export default class ZipHelper {
Logger.debug("utils", "Extracting zip entry", entry);
if (/\/$/.test(entry.fileName)) {
// directory file names end with '/'
mkdirp(
fs.mkdirp(
path.join(outputDir, entry.fileName),
function (err: Error) {
if (err) {
@@ -131,7 +130,7 @@ export default class ZipHelper {
throw err;
}
// ensure parent directory exists
mkdirp(
fs.mkdirp(
path.join(outputDir, path.dirname(entry.fileName)),
function (err) {
if (err) {