Skip to content

Commit

Permalink
[accel-wave] custom filename
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 17, 2024
1 parent 75ab8b7 commit 39299f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 6 additions & 1 deletion packages/accel-wave/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ export class BaseUploader {
constructor(options?: Partial<BaseUploader>) {
Object.assign(this, options);
}

get filename(): string | undefined {
return undefined;
}
}

export class FileStorage {
constructor(public uploader: BaseUploader) {}

store(file: File) {
const filename = this.uploader.filename ?? file.name;
const filePath = new URL(
`${this.uploader.root}/${this.uploader.storeDir}/${file.name}`,
`${this.uploader.root}/${this.uploader.storeDir}/${filename}`,
import.meta.url
).pathname;
actions.writeFile(filePath, file);
Expand Down
10 changes: 8 additions & 2 deletions packages/accel-wave/tests/fileStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import fs from "fs";
import path from "path";
import { BaseUploader, FileStorage } from "../src";

class MyUploader extends BaseUploader {
override get filename() {
return "myfile.txt";
}
}

test("store()", () => {
const storage = new FileStorage(new BaseUploader());
const file = buildFile();
Expand All @@ -14,12 +20,12 @@ test("store()", () => {
});

test("store() with storeDir", () => {
const storage = new FileStorage(new BaseUploader({ root: "../tmp", storeDir: "custom" }));
const storage = new FileStorage(new MyUploader({ root: "../tmp", storeDir: "custom" }));
const file = buildFile();

storage.store(file);

const filePath = path.resolve(__dirname, "../tmp/custom/example.txt");
const filePath = path.resolve(__dirname, "../tmp/custom/myfile.txt");
const content = fs.readFileSync(filePath, "utf-8");
expect(content).toBe("Hello");
});
Expand Down

0 comments on commit 39299f9

Please sign in to comment.