Skip to content
Open
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
51 changes: 51 additions & 0 deletions packages/filesystem/onedrive/onedrive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,55 @@ describe("OneDriveFileSystem", () => {
name: "B",
});
});

it("writer should upload empty string with simple PUT", async () => {
const fs = new OneDriveFileSystem("/", "token");
const requestSpy = vi.spyOn(fs, "request").mockResolvedValue({});

const writer = await fs.create("empty.txt");
await writer.write("");

expect(requestSpy).toHaveBeenCalledTimes(1);
expect(requestSpy.mock.calls[0][0]).toBe(
"https://graph.microsoft.com/v1.0/me/drive/special/approot:/empty.txt:/content"
);
expect(requestSpy.mock.calls[0][1]).toMatchObject({
method: "PUT",
body: "",
});
});

it("writer should upload empty Blob with simple PUT", async () => {
const fs = new OneDriveFileSystem("/", "token");
const requestSpy = vi.spyOn(fs, "request").mockResolvedValue({});
const emptyBlob = new Blob([]);

const writer = await fs.create("empty.bin");
await writer.write(emptyBlob);

expect(requestSpy).toHaveBeenCalledTimes(1);
expect(requestSpy.mock.calls[0][0]).toBe(
"https://graph.microsoft.com/v1.0/me/drive/special/approot:/empty.bin:/content"
);
expect((requestSpy.mock.calls[0][1] as RequestInit).body).toBe(emptyBlob);
});

it("writer should keep upload session for non-empty content", async () => {
const fs = new OneDriveFileSystem("/", "token");
const requestSpy = vi
.spyOn(fs, "request")
.mockResolvedValueOnce({ uploadUrl: "https://upload.example/session" })
.mockResolvedValueOnce({});

const writer = await fs.create("not-empty.txt");
await writer.write("abc");

expect(requestSpy).toHaveBeenCalledTimes(2);
expect(requestSpy.mock.calls[0][0]).toBe(
"https://graph.microsoft.com/v1.0/me/drive/special/approot:/not-empty.txt:/createUploadSession"
);
expect(requestSpy.mock.calls[1][0]).toBe("https://upload.example/session");
const headers = (requestSpy.mock.calls[1][1] as RequestInit).headers as Headers;
expect(headers.get("Content-Range")).toBe("bytes 0-2/3");
});
});
11 changes: 9 additions & 2 deletions packages/filesystem/onedrive/rw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,14 @@ export class OneDriveFileWriter implements FileWriter {

async write(content: string | Blob): Promise<void> {
// 预上传获取id
const size = this.size(content).toString();
const size = this.size(content);
if (size === 0) {
return this.fs.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot:${this.path}:/content`, {
method: "PUT",
body: content,
});
}

let myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
const uploadUrl = await this.fs
Expand All @@ -83,7 +90,7 @@ export class OneDriveFileWriter implements FileWriter {
return data.uploadUrl;
});
myHeaders = new Headers();
myHeaders.append("Content-Range", `bytes 0-${parseInt(size, 10) - 1}/${size}`);
myHeaders.append("Content-Range", `bytes 0-${size - 1}/${size}`);
return this.fs.request(uploadUrl, {
method: "PUT",
body: content,
Expand Down
Loading