|
| 1 | +import { applyToken } from "./security.js"; |
| 2 | + |
| 3 | +const btn = document.getElementById("go"); |
| 4 | +const log = <HTMLDivElement>document.getElementById("log"); |
| 5 | + |
| 6 | +function logger(message: string) { |
| 7 | + |
| 8 | + const p = document.createElement("p"); |
| 9 | + p.innerHTML = message; |
| 10 | + log.prepend(p); |
| 11 | +} |
| 12 | + |
| 13 | +btn.addEventListener("click", async (e) => { |
| 14 | + |
| 15 | + e.preventDefault(); |
| 16 | + |
| 17 | + const input = <HTMLInputElement>document.getElementById("the-file"); |
| 18 | + |
| 19 | + |
| 20 | + if (input.files && input.files.length > 0) { |
| 21 | + |
| 22 | + const file = input.files[0]; |
| 23 | + |
| 24 | + const getFolderInit = await applyToken({ |
| 25 | + method: "GET", |
| 26 | + }); |
| 27 | + |
| 28 | + const folderInfoResponse = await fetch("https://graph.microsoft.com/v1.0/me/drive/special/approot", getFolderInit); |
| 29 | + |
| 30 | + const folderInfo = await folderInfoResponse.json(); |
| 31 | + |
| 32 | + const { driveId } = folderInfo.parentReference; |
| 33 | + |
| 34 | + // rebase the URL to the /drives/{id} pattern |
| 35 | + const baseUrl = `https://graph.microsoft.com/v1.0/drives/${driveId}/items/${folderInfo.id}`; |
| 36 | + |
| 37 | + // 1. create upload session |
| 38 | + const createUploadSessionUrl = `${baseUrl}:/${encodeURIComponent(file.name)}:/createUploadSession`; |
| 39 | + const createUploadSessionInit = await applyToken({ |
| 40 | + method: "POST", |
| 41 | + body: JSON.stringify({ |
| 42 | + item: { |
| 43 | + "@microsoft.graph.conflictBehavior": "rename", |
| 44 | + name: file.name, |
| 45 | + }, |
| 46 | + }), |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json", |
| 49 | + } |
| 50 | + }); |
| 51 | + const uploadSessionInfoResponse = await fetch(createUploadSessionUrl, createUploadSessionInit); |
| 52 | + const uploadSessionInfo = await uploadSessionInfoResponse.json(); |
| 53 | + if (!uploadSessionInfoResponse.ok) { |
| 54 | + throw Error(JSON.stringify(uploadSessionInfo, null, 2)); |
| 55 | + } |
| 56 | + logger(`Created upload session: <pre>${JSON.stringify(uploadSessionInfo, null, 2)}</pre>`); |
| 57 | + |
| 58 | + // this is the upload url for our new file, it is opaque |
| 59 | + const { uploadUrl } = uploadSessionInfo; |
| 60 | + |
| 61 | + // 2. Get a stream representing the file |
| 62 | + const stream = file.stream(); |
| 63 | + const reader = stream.getReader(); |
| 64 | + |
| 65 | + // 3. Use the reader pattern (or any other) to upload the file |
| 66 | + let bytePointer = 0; |
| 67 | + let uploadBytesInfo; |
| 68 | + reader.read().then(async function pump({ done, value }) { |
| 69 | + |
| 70 | + if (done) { |
| 71 | + |
| 72 | + if (value) { |
| 73 | + console.error("had value at done"); |
| 74 | + } |
| 75 | + |
| 76 | + logger(`Upload complete <a href="${uploadBytesInfo.webUrl}" target="_blank">${uploadBytesInfo.name}</a>`); |
| 77 | + |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const contentLength = value.length - 1; |
| 82 | + const contentRangeStr = `bytes ${bytePointer}-${bytePointer + contentLength}/${(file.size)}`; |
| 83 | + logger(`Uploading: ${contentRangeStr}`); |
| 84 | + |
| 85 | + // 4. Execute a series of PUT commands to upload the file in chunks |
| 86 | + const uploadBytesInit = await applyToken({ |
| 87 | + method: "PUT", |
| 88 | + body: value, |
| 89 | + headers: { |
| 90 | + "Content-Length": contentLength.toString(), |
| 91 | + "Content-Range": contentRangeStr, |
| 92 | + } |
| 93 | + }); |
| 94 | + const uploadBytesInfoResponse = await fetch(uploadUrl, uploadBytesInit); |
| 95 | + uploadBytesInfo = await uploadBytesInfoResponse.json(); |
| 96 | + |
| 97 | + if (!uploadBytesInfoResponse.ok) { |
| 98 | + throw Error(JSON.stringify(uploadBytesInfo)); |
| 99 | + } |
| 100 | + |
| 101 | + bytePointer += value.length; |
| 102 | + |
| 103 | + // Read some more, and call this function again |
| 104 | + return reader.read().then(pump); |
| 105 | + }); |
| 106 | + |
| 107 | + } else { |
| 108 | + |
| 109 | + alert("Please select a file."); |
| 110 | + } |
| 111 | +}); |
0 commit comments