MCPcopy
hub / github.com/remotely-save/remotely-save / mkdir

Method mkdir

pro/src/fsGoogleDrive.ts:423–483  ·  view source on GitHub ↗

* https://developers.google.com/drive/api/guides/folder

(
    key: string,
    mtime: number | undefined,
    ctime: number | undefined
  )

Source from the content-addressed store, hash-verified

421 * https://developers.google.com/drive/api/guides/folder
422 */
423 async mkdir(
424 key: string,
425 mtime: number | undefined,
426 ctime: number | undefined
427 ): Promise<Entity> {
428 if (!key.endsWith("/")) {
429 throw Error(`you should not mkdir on key=${key}`);
430 }
431
432 await this._init();
433
434 // xxx/ => ["xxx"]
435 // xxx/yyy/zzz/ => ["xxx", "xxx/yyy", "xxx/yyy/zzz"]
436 const folderLevels = getFolderLevels(key);
437 let parentFolderPath: string | undefined = undefined;
438 let parentID: string | undefined = undefined;
439 if (folderLevels.length === 0) {
440 throw Error(`cannot getFolderLevels of ${key}`);
441 } else if (folderLevels.length === 1) {
442 parentID = this.baseDirID;
443 parentFolderPath = ""; // ignore base dir
444 } else {
445 // length > 1
446 parentFolderPath = `${folderLevels[folderLevels.length - 2]}/`;
447 if (!(parentFolderPath in this.keyToGDEntity)) {
448 throw Error(
449 `parent of ${key}: ${parentFolderPath} is not created before??`
450 );
451 }
452 parentID = this.keyToGDEntity[parentFolderPath].id;
453 }
454
455 // xxx/yyy/zzz/ => ["xxx", "xxx/yyy", "xxx/yyy/zzz"] => "xxx/yyy/zzz" => "zzz"
456 let folderItselfWithoutSlash = folderLevels[folderLevels.length - 1];
457 folderItselfWithoutSlash = folderItselfWithoutSlash.split("/").pop()!;
458
459 const meta: any = {
460 mimeType: FOLDER_MIME_TYPE,
461 modifiedTime: unixTimeToStr(mtime, true),
462 createdTime: unixTimeToStr(ctime, true),
463 name: folderItselfWithoutSlash,
464 parents: [parentID],
465 };
466 const res = await fetch("https://www.googleapis.com/drive/v3/files", {
467 method: "POST",
468 headers: {
469 Authorization: `Bearer ${await this._getAccessToken()}`,
470 "Content-Type": "application/json",
471 },
472 body: JSON.stringify(meta),
473 });
474 if (res.status !== 200 && res.status !== 201) {
475 throw Error(`create folder ${key} failed! meta=${JSON.stringify(meta)}`);
476 }
477 const res2: File = await res.json();
478 // console.debug(res2);
479 const entity = fromFileToGDEntity(res2, parentID, parentFolderPath);
480 // insert into cache

Callers

nothing calls this directly

Calls 5

_initMethod · 0.95
_getAccessTokenMethod · 0.95
getFolderLevelsFunction · 0.90
unixTimeToStrFunction · 0.90
fromFileToGDEntityFunction · 0.85

Tested by

no test coverage detected