Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/filesystem/onedrive/onedrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,38 @@ describe("OneDriveFileSystem", () => {

await expect(fs.delete("missing.txt")).resolves.toBeUndefined();
});

it("createDir should create nested directories from root", async () => {
const fs = new OneDriveFileSystem("/", "token");
const requestSpy = vi.spyOn(fs, "request").mockResolvedValue({});

await expect(fs.createDir("A/B/C")).resolves.toBeUndefined();

expect(requestSpy).toHaveBeenCalledTimes(3);
expect(requestSpy.mock.calls[0][0]).toBe("https://graph.microsoft.com/v1.0/me/drive/special/approot/children");
expect(requestSpy.mock.calls[1][0]).toBe("https://graph.microsoft.com/v1.0/me/drive/special/approot:/A:/children");
expect(requestSpy.mock.calls[2][0]).toBe(
"https://graph.microsoft.com/v1.0/me/drive/special/approot:/A/B:/children"
);
expect(JSON.parse((requestSpy.mock.calls[2][1] as RequestInit).body as string)).toMatchObject({
name: "C",
folder: {},
"@microsoft.graph.conflictBehavior": "fail",
});
});

it("createDir should continue when an intermediate directory already exists", async () => {
const fs = new OneDriveFileSystem("/", "token");
const requestSpy = vi
.spyOn(fs, "request")
.mockRejectedValueOnce(new Error('{"error":{"code":"nameAlreadyExists"}}'))
.mockResolvedValueOnce({});

await expect(fs.createDir("A/B")).resolves.toBeUndefined();

expect(requestSpy).toHaveBeenCalledTimes(2);
expect(JSON.parse((requestSpy.mock.calls[1][1] as RequestInit).body as string)).toMatchObject({
name: "B",
});
});
});
46 changes: 26 additions & 20 deletions packages/filesystem/onedrive/onedrive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,37 @@ export default class OneDriveFileSystem implements FileSystem {
if (!dir) {
return;
}
dir = joinPath(this.path, dir);
const dirs = dir.split("/");
let parent = "";
if (dirs.length > 2) {
parent = dirs.slice(0, dirs.length - 1).join("/");
}
const dirs = joinPath(this.path, dir).split("/").filter(Boolean);
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
if (parent !== "") {
parent = `:${parent}:`;
}
const data = await this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, {
method: "POST",
headers: myHeaders,
body: JSON.stringify({
name: dirs[dirs.length - 1],
folder: {},
"@microsoft.graph.conflictBehavior": "replace",
}),
});
if (data.errno) {
throw new Error(JSON.stringify(data));

for (let i = 0; i < dirs.length; i++) {
const parentPath = dirs.slice(0, i).join("/");
const parent = parentPath ? `:/${parentPath}:` : "";
try {
await this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, {
method: "POST",
headers: myHeaders,
body: JSON.stringify({
name: dirs[i],
folder: {},
"@microsoft.graph.conflictBehavior": "fail",
}),
});
} catch (error) {
if (this.isDirectoryAlreadyExistsError(error)) {
continue;
}
throw error;
}
}
}

private isDirectoryAlreadyExistsError(error: unknown): boolean {
const msg = String(error);
return msg.includes("nameAlreadyExists") || msg.includes("itemAlreadyExists");
}

request(url: string, config?: RequestInit, nothen?: boolean): Promise<Response | any> {
config = config || {};
const headers = <Headers>config.headers || new Headers();
Expand Down
Loading