From c08f30fd20235219462ea2df092d9a2ba2e3e2d9 Mon Sep 17 00:00:00 2001 From: Julien Monnard Date: Sat, 21 Jan 2023 17:35:19 +0100 Subject: [PATCH] feat(remix-node): add `getFilePath`& `remove` methods to `NodeOnDiskFile` (#4408) * feat(node): add new methods to NodeOnDiskFile class * chore: add name to contributors for CLA signature * added changeset * small changeset rewording * reference the class instead of the file in changeset --- .changeset/flat-cobras-beam.md | 5 +++++ contributors.yml | 1 + .../remix-node/__tests__/fileUploadHandler-test.ts | 14 ++++++++++++++ packages/remix-node/upload/fileUploadHandler.ts | 9 ++++++++- 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/flat-cobras-beam.md diff --git a/.changeset/flat-cobras-beam.md b/.changeset/flat-cobras-beam.md new file mode 100644 index 00000000000..9f9de96e51a --- /dev/null +++ b/.changeset/flat-cobras-beam.md @@ -0,0 +1,5 @@ +--- +"@remix-run/node": minor +--- + +Add `remove` and `getFilePath` methods to `NodeOnDiskFile` diff --git a/contributors.yml b/contributors.yml index a9ae704ec43..17f858c425e 100644 --- a/contributors.yml +++ b/contributors.yml @@ -224,6 +224,7 @@ - juhanakristian - JulesBlm - juliaqiuxy +- julienmonnard - justinnoel - justinsalasdev - justinwaite diff --git a/packages/remix-node/__tests__/fileUploadHandler-test.ts b/packages/remix-node/__tests__/fileUploadHandler-test.ts index 0d52c6eae7e..02917450bd0 100644 --- a/packages/remix-node/__tests__/fileUploadHandler-test.ts +++ b/packages/remix-node/__tests__/fileUploadHandler-test.ts @@ -104,4 +104,18 @@ describe("NodeOnDiskFile", () => { expect(sliced.size).toBe(slicedRes.length); expect(await sliced.text()).toBe(slicedRes); }); + + it("returns the file path properly", async () => { + expect(file.getFilePath()).toEqual(filepath); + }); + + it("removes the file properly", async () => { + let newFilePath = `${filepath}-copy`; + fs.copyFileSync(filepath, newFilePath); + + let copiedFile = (file = new NodeOnDiskFile(newFilePath, "text/plain")); + expect(fs.existsSync(copiedFile.getFilePath())).toBe(true); + await copiedFile.remove(); + expect(fs.existsSync(copiedFile.getFilePath())).toBe(false); + }); }); diff --git a/packages/remix-node/upload/fileUploadHandler.ts b/packages/remix-node/upload/fileUploadHandler.ts index ad2836ab9c2..2f076cd1b79 100644 --- a/packages/remix-node/upload/fileUploadHandler.ts +++ b/packages/remix-node/upload/fileUploadHandler.ts @@ -1,6 +1,6 @@ import { randomBytes } from "crypto"; import { createReadStream, createWriteStream, statSync } from "fs"; -import { rm, mkdir, stat as statAsync } from "fs/promises"; +import { rm, mkdir, stat as statAsync, unlink } from "fs/promises"; import { tmpdir } from "os"; import { basename, dirname, extname, resolve as resolvePath } from "path"; import type { Readable } from "stream"; @@ -231,4 +231,11 @@ export class NodeOnDiskFile implements File { public get [Symbol.toStringTag]() { return "File"; } + + remove(): Promise { + return unlink(this.filepath); + } + getFilePath(): string { + return this.filepath; + } }