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

Method _writeFileFromRoot

src/fsOnedrive.ts:919–1025  ·  view source on GitHub ↗
(
    key: string,
    content: ArrayBuffer,
    mtime: number,
    ctime: number,
    origKey: string,
    emptyFile: "skip" | "error"
  )

Source from the content-addressed store, hash-verified

917 }
918
919 async _writeFileFromRoot(
920 key: string,
921 content: ArrayBuffer,
922 mtime: number,
923 ctime: number,
924 origKey: string,
925 emptyFile: "skip" | "error"
926 ): Promise<Entity> {
927 if (content.byteLength === 0) {
928 if (emptyFile === "error") {
929 throw Error(
930 `${origKey}: Empty file is not allowed in OneDrive, and please write something in it.`
931 );
932 } else {
933 return {
934 key: origKey,
935 keyRaw: origKey,
936 mtimeSvr: mtime,
937 mtimeCli: mtime,
938 ctimeCli: ctime,
939 size: 0,
940 sizeRaw: 0,
941 synthesizedFile: true,
942 // hash: ?? // TODO
943 };
944 }
945 }
946
947 const ctimeStr = new Date(ctime).toISOString();
948 const mtimeStr = new Date(mtime).toISOString();
949
950 // no need to create parent folders firstly, cool!
951
952 // hard code range size
953 const MIN_UNIT = 327680; // bytes in msft doc, about 0.32768 MB
954 const RANGE_SIZE = MIN_UNIT * 20; // about 6.5536 MB
955 const DIRECT_UPLOAD_MAX_SIZE = 1000 * 1000 * 4; // 4 Megabyte
956
957 if (content.byteLength < DIRECT_UPLOAD_MAX_SIZE) {
958 // directly using put!
959 await this._putArrayBuffer(
960 `${key}:/content?${new URLSearchParams({
961 "@microsoft.graph.conflictBehavior": "replace",
962 })}`,
963 content
964 );
965 if (mtime !== 0 && ctime !== 0) {
966 await this._patchJson(key, {
967 fileSystemInfo: {
968 lastModifiedDateTime: mtimeStr,
969 createdDateTime: ctimeStr,
970 } as FileSystemInfo,
971 });
972 }
973 } else {
974 // upload large files!
975 // ref: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_createuploadsession?view=odsp-graph-online
976

Callers 1

writeFileMethod · 0.95

Calls 5

_putArrayBufferMethod · 0.95
_patchJsonMethod · 0.95
_postJsonMethod · 0.95
_putUint8ArrayByRangeMethod · 0.95
_statFromRootMethod · 0.95

Tested by

no test coverage detected