Skip to content

Commit

Permalink
feat(remix-node): add getFilePath& remove methods to `NodeOnDiskF…
Browse files Browse the repository at this point in the history
…ile` (remix-run#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
  • Loading branch information
julienmonnard authored and andrelandgraf committed Jan 22, 2023
1 parent a263c33 commit ceceb26
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/flat-cobras-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/node": minor
---

Add `remove` and `getFilePath` methods to `NodeOnDiskFile`
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@
- juhanakristian
- JulesBlm
- juliaqiuxy
- julienmonnard
- justinnoel
- justinsalasdev
- justinwaite
Expand Down
14 changes: 14 additions & 0 deletions packages/remix-node/__tests__/fileUploadHandler-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
9 changes: 8 additions & 1 deletion packages/remix-node/upload/fileUploadHandler.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -231,4 +231,11 @@ export class NodeOnDiskFile implements File {
public get [Symbol.toStringTag]() {
return "File";
}

remove(): Promise<void> {
return unlink(this.filepath);
}
getFilePath(): string {
return this.filepath;
}
}

0 comments on commit ceceb26

Please sign in to comment.