| 6 | import { OneDriveFileReader, OneDriveFileWriter } from "./rw"; |
| 7 | |
| 8 | export default class OneDriveFileSystem implements FileSystem { |
| 9 | accessToken?: string; |
| 10 | |
| 11 | path: string; |
| 12 | |
| 13 | constructor(path?: string, accessToken?: string) { |
| 14 | this.path = path || "/"; |
| 15 | this.accessToken = accessToken; |
| 16 | } |
| 17 | |
| 18 | async verify(): Promise<void> { |
| 19 | const token = await AuthVerify("onedrive"); |
| 20 | this.accessToken = token; |
| 21 | return this.list().then(); |
| 22 | } |
| 23 | |
| 24 | async open(file: FileInfo): Promise<FileReader> { |
| 25 | return new OneDriveFileReader(this, file); |
| 26 | } |
| 27 | |
| 28 | async openDir(path: string): Promise<FileSystem> { |
| 29 | if (path.startsWith("ScriptCat")) { |
| 30 | path = path.substring(9); |
| 31 | } |
| 32 | return new OneDriveFileSystem(joinPath(this.path, path), this.accessToken); |
| 33 | } |
| 34 | |
| 35 | async create(path: string, _opts?: FileCreateOptions): Promise<FileWriter> { |
| 36 | return new OneDriveFileWriter(this, joinPath(this.path, path)); |
| 37 | } |
| 38 | |
| 39 | async createDir(dir: string, _opts?: FileCreateOptions): Promise<void> { |
| 40 | if (dir && dir.startsWith("ScriptCat")) { |
| 41 | dir = dir.substring(9); |
| 42 | if (dir.startsWith("/")) { |
| 43 | dir = dir.substring(1); |
| 44 | } |
| 45 | } |
| 46 | if (!dir) { |
| 47 | return; |
| 48 | } |
| 49 | const dirs = joinPath(this.path, dir).split("/").filter(Boolean); |
| 50 | const myHeaders = new Headers(); |
| 51 | myHeaders.append("Content-Type", "application/json"); |
| 52 | |
| 53 | for (let i = 0; i < dirs.length; i++) { |
| 54 | const parentPath = dirs.slice(0, i).join("/"); |
| 55 | const parent = parentPath ? `:/${parentPath}:` : ""; |
| 56 | try { |
| 57 | await this.request(`https://graph.microsoft.com/v1.0/me/drive/special/approot${parent}/children`, { |
| 58 | method: "POST", |
| 59 | headers: myHeaders, |
| 60 | body: JSON.stringify({ |
| 61 | name: dirs[i], |
| 62 | folder: {}, |
| 63 | "@microsoft.graph.conflictBehavior": "fail", |
| 64 | }), |
| 65 | }); |
nothing calls this directly
no outgoing calls
no test coverage detected