Skip to content

Commit

Permalink
fix: enable use of dest option as async function
Browse files Browse the repository at this point in the history
  • Loading branch information
sor4chi committed May 18, 2024
1 parent 68624aa commit cc61e15
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
17 changes: 7 additions & 10 deletions packages/node-disk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import { File } from "@web-std/file";

import type { Context } from "hono";

type HDSCustomFunction = (c: Context, file: HonoStorageFile) => string;
type HDSCustomFunction = (c: Context, file: HonoStorageFile) => Promise<string> | string;

interface HonoDiskStorageOption {
dest?: string | HDSCustomFunction;
filename?: HDSCustomFunction;
}

export class HonoDiskStorage extends HonoStorage {
private dest: string | ((c: Context, file: HonoStorageFile) => string);

constructor(option: HonoDiskStorageOption = {}) {
const { dest = "/tmp" } = option;
Expand All @@ -24,23 +23,21 @@ export class HonoDiskStorage extends HonoStorage {
storage: async (c, files) => {
await Promise.all(
files.map(async (file) => {
const dest =
typeof this.dest === "function" ? this.dest(c, file) : this.dest;
await mkdir(dest, { recursive: true });
const finalDest =
typeof dest === "function" ? await dest(c, file) : dest;
await mkdir(finalDest, { recursive: true });
if (option.filename) {
await this.handleDestStorage(
dest,
new File([file], option.filename(c, file)),
finalDest,
new File([file], await option.filename(c, file)),
);
} else {
await this.handleDestStorage(dest, new File([file], file.name));
await this.handleDestStorage(finalDest, new File([file], file.name));
}
}),
);
},
});

this.dest = dest;
}

handleDestStorage = async (dest: string, file: File) => {
Expand Down
31 changes: 31 additions & 0 deletions packages/node-disk/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { rm } from "fs";
import { join } from "path";

import { createAdaptorServer } from "@hono/node-server";
Expand All @@ -15,6 +16,10 @@ describe("HonoDiskStorage", () => {
});

describe("dest option", () => {
beforeEach(() => {
rm(join(__dirname, "tmp"), { recursive: true }, () => {});
});

it("should work with string dest", async () => {
const storage = new HonoDiskStorage({
dest: join(__dirname, "tmp"),
Expand Down Expand Up @@ -61,6 +66,32 @@ describe("HonoDiskStorage", () => {
}),
);
});

it("should work with async custom function dest", async () => {
const storage = new HonoDiskStorage({
dest: async (c) => {
if (c.req.query("store")) {
return join(__dirname, `tmp/store${c.req.query("store")}`);
}
return join(__dirname, "tmp");
},
});
const spyHandleDestStorage = vi.spyOn(storage, "handleDestStorage");
const app = new Hono();
app.post("/upload", storage.single("file"), (c) => c.text("Hello World"));
const server = createAdaptorServer(app);
const res = await request(server)
.post("/upload?store=1")
.attach("file", join(__dirname, "fixture/sample1.txt"));
expect(res.status).toBe(200);
expect(res.text).toBe("Hello World");
expect(spyHandleDestStorage).toBeCalledWith(
join(__dirname, "tmp/store1"),
expect.objectContaining({
name: "sample1.txt",
}),
);
});
});

describe("filename option", () => {
Expand Down

0 comments on commit cc61e15

Please sign in to comment.