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

View File

@@ -1,5 +1,5 @@
import path from "path"; import path from "path";
import { rm } from "fs-extra"; import fs from "fs-extra";
import truncate from "lodash/truncate"; import truncate from "lodash/truncate";
import tmp from "tmp"; import tmp from "tmp";
import { import {
@@ -248,7 +248,7 @@ export default abstract class ImportTask extends BaseTask<Props> {
fileOperation: FileOperation fileOperation: FileOperation
) { ) {
try { try {
await rm(dirPath, { recursive: true, force: true }); await fs.rm(dirPath, { recursive: true, force: true });
} catch (error) { } catch (error) {
Logger.error( Logger.error(
`ImportTask failed to cleanup extracted data for ${fileOperation.id}`, `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 { mkdir, unlink, rmdir } from "fs/promises";
import path from "path"; import path from "path";
import { Readable } from "stream"; import { Readable } from "stream";
import { import fs from "fs-extra";
ReadStream,
close,
pathExists,
createReadStream,
createWriteStream,
open,
} from "fs-extra";
import invariant from "invariant"; import invariant from "invariant";
import JWT from "jsonwebtoken"; import JWT from "jsonwebtoken";
import safeResolvePath from "resolve-path"; import safeResolvePath from "resolve-path";
@@ -48,13 +41,13 @@ export default class LocalStorage extends BaseStorage {
body, body,
key, key,
}: { }: {
body: string | ReadStream | Buffer | Uint8Array | Blob; body: string | fs.ReadStream | Buffer | Uint8Array | Blob;
contentLength?: number; contentLength?: number;
contentType?: string; contentType?: string;
key: string; key: string;
acl?: string; acl?: string;
}) => { }) => {
const exists = await pathExists(this.getFilePath(key)); const exists = await fs.pathExists(this.getFilePath(key));
if (exists) { if (exists) {
throw ValidationError(`File already exists at ${key}`); throw ValidationError(`File already exists at ${key}`);
} }
@@ -64,7 +57,7 @@ export default class LocalStorage extends BaseStorage {
}); });
let src: NodeJS.ReadableStream; let src: NodeJS.ReadableStream;
if (body instanceof ReadStream) { if (body instanceof fs.ReadStream) {
src = body; src = body;
} else if (body instanceof Blob) { } else if (body instanceof Blob) {
src = Readable.from(Buffer.from(await body.arrayBuffer())); src = Readable.from(Buffer.from(await body.arrayBuffer()));
@@ -75,10 +68,11 @@ export default class LocalStorage extends BaseStorage {
const filePath = this.getFilePath(key); const filePath = this.getFilePath(key);
// Create the file on disk first // 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) => { return new Promise<string>((resolve, reject) => {
const dest = createWriteStream(filePath) const dest = fs
.createWriteStream(filePath)
.on("error", reject) .on("error", reject)
.on("finish", () => resolve(this.getUrlForKey(key))); .on("finish", () => resolve(this.getUrlForKey(key)));
@@ -138,7 +132,7 @@ export default class LocalStorage extends BaseStorage {
} }
public getFileStream(key: string) { public getFileStream(key: string) {
return createReadStream(this.getFilePath(key)); return fs.createReadStream(this.getFilePath(key));
} }
private getFilePath(key: string) { private getFilePath(key: string) {

View File

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

View File

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