Skip to content

Commit

Permalink
feat(file): Add pathnameToFilename utility for path handling
Browse files Browse the repository at this point in the history
Add a new utility function to extract filenames from full paths and update
file copy implementation to preserve original filenames with random suffixes.

- Add pathnameToFilename function to extract file name from path
- Add comprehensive test cases for different path scenarios
- Update copyAgentAction to maintain original file names during copy
- Add random suffix to prevent filename collisions
  • Loading branch information
toyamarinyon committed Dec 16, 2024
1 parent 886a8da commit a51dbf2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
23 changes: 22 additions & 1 deletion app/(playground)/p/[agentId]/canary/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, test } from "bun:test";
import { pathJoin } from "./utils";
import { pathJoin, pathnameToFilename } from "./utils";

describe("pathJoin", () => {
test("a, b, c", () => {
Expand All @@ -14,3 +14,24 @@ describe("pathJoin", () => {
expect(file).toBe("a/b/c.json");
});
});

describe("pathNameToFileName", () => {
test("articles/test.txt", () => {
expect(pathnameToFilename("articles/test.txt")).toBe("test.txt");
});
test("empty", () => {
expect(pathnameToFilename("")).toBe("");
});
test("just filename", () => {
expect(pathnameToFilename("test.txt")).toBe("test.txt");
});
test("nested path", () => {
expect(pathnameToFilename("path/to/nested/file.txt")).toBe("file.txt");
});
test("no extension", () => {
expect(pathnameToFilename("folder/file")).toBe("file");
});
test("with spaces", () => {
expect(pathnameToFilename("folder/my file.txt")).toBe("my file.txt");
});
});
4 changes: 4 additions & 0 deletions app/(playground)/p/[agentId]/canary/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,7 @@ export function toErrorWithMessage(maybeError: unknown): ErrorWithMessage {
return new Error(String(maybeError));
}
}

export function pathnameToFilename(pathname: string) {
return pathname.split("/").pop() ?? "";
}
7 changes: 6 additions & 1 deletion app/dev/copy-agent/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
buildFileFolderPath,
createFileId,
pathJoin,
pathnameToFilename,
} from "../../(playground)/p/[agentId]/canary/lib/utils";
import type {
AgentId,
Expand Down Expand Up @@ -66,8 +67,12 @@ export async function copyAgentAction(
blobList.blobs.map(async (blob) => {
const copyResult = await copy(
blob.url,
buildFileFolderPath(newFileId),
pathJoin(
buildFileFolderPath(newFileId),
pathnameToFilename(blob.pathname),
),
{
addRandomSuffix: true,
access: "public",
},
);
Expand Down

0 comments on commit a51dbf2

Please sign in to comment.