microsoft-graph 3.9.3 → 3.9.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CODEOWNERS +1 -0
- package/dist/cjs/operations/driveItem/createDriveItemContent.d.ts +3 -3
- package/dist/cjs/operations/driveItem/createDriveItemContent.d.ts.map +1 -1
- package/dist/cjs/operations/driveItem/createDriveItemContent.js +38 -30
- package/dist/cjs/operations/driveItem/getDriveItemByPath.d.ts +2 -2
- package/dist/cjs/operations/driveItem/getDriveItemByPath.d.ts.map +1 -1
- package/dist/cjs/operations/driveItem/getDriveItemByPath.js +14 -6
- package/dist/esm/operations/driveItem/createDriveItemContent.d.ts +3 -3
- package/dist/esm/operations/driveItem/createDriveItemContent.d.ts.map +1 -1
- package/dist/esm/operations/driveItem/createDriveItemContent.js +38 -30
- package/dist/esm/operations/driveItem/getDriveItemByPath.d.ts +2 -2
- package/dist/esm/operations/driveItem/getDriveItemByPath.d.ts.map +1 -1
- package/dist/esm/operations/driveItem/getDriveItemByPath.js +14 -6
- package/docs/api/createDriveItemContent.md +10 -12
- package/docs/api/getDriveItemByPath.md +2 -2
- package/package.json +1 -1
package/CODEOWNERS
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @benfsai
|
|
@@ -20,7 +20,7 @@ export declare const chunkSizeMultiple: number;
|
|
|
20
20
|
*/
|
|
21
21
|
export interface CreateDriveItemContentOptions {
|
|
22
22
|
conflictBehavior?: "fail" | "replace" | "rename";
|
|
23
|
-
|
|
23
|
+
maxChunkSize?: number;
|
|
24
24
|
progress?: (bytes: number) => void;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -31,8 +31,8 @@ export interface CreateDriveItemContentOptions {
|
|
|
31
31
|
* @param contentLength The total size in bytes of the content to be uploaded.
|
|
32
32
|
* @param options Optional. Additional options for the upload operation.
|
|
33
33
|
* @param options.conflictBehavior Optional. Specifies how to handle conflicts if the file already exists. Default is 'fail'.
|
|
34
|
-
* @param options.
|
|
35
|
-
* @param options.progress Optional. A callback function that is called
|
|
34
|
+
* @param options.maxChunkSize Optional. The size of each chunk to be uploaded in bytes. Default is 10MB.
|
|
35
|
+
* @param options.progress Optional. A callback function that is called with the number of bytes uploaded after each chunk.
|
|
36
36
|
* @returns The newly created drive item.
|
|
37
37
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
38
38
|
* @see https://learn.microsoft.com/en-us/graph/api/resources/uploadsession
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDriveItemContent.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/createDriveItemContent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"createDriveItemContent.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/createDriveItemContent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAIlE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAM1F;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,QAAa,CAAC;AAc5C;;;;;GAKG;AACH,MAAM,WAAW,6BAA6B;IAC7C,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAA8B,sBAAsB,CAAC,SAAS,EAAE,QAAQ,GAAG,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,CAAC,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,GAAE,6BAAkC,GAAG,OAAO,CAAC,SAAS,GAAG,YAAY,CAAC,CAmG7P"}
|
|
@@ -10,6 +10,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
10
10
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
11
11
|
exports.chunkSizeMultiple = void 0;
|
|
12
12
|
exports.default = createDriveItemContent;
|
|
13
|
+
const node_path_1 = require("node:path");
|
|
13
14
|
const InvalidArgumentError_ts_1 = __importDefault(require("../../errors/InvalidArgumentError.js"));
|
|
14
15
|
const ProtocolError_ts_1 = __importDefault(require("../../errors/ProtocolError.js"));
|
|
15
16
|
const driveItem_ts_1 = require("../../services/driveItem.js");
|
|
@@ -22,7 +23,7 @@ const templatedPaths_ts_1 = require("../../services/templatedPaths.js");
|
|
|
22
23
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
23
24
|
*/
|
|
24
25
|
exports.chunkSizeMultiple = 320 * 1024;
|
|
25
|
-
const
|
|
26
|
+
const defaultMaxChunkSize = exports.chunkSizeMultiple * 32;
|
|
26
27
|
/**
|
|
27
28
|
* Creates a new drive item in the specified parent drive or folder using a stream as content.
|
|
28
29
|
* @param parentRef Reference to the parent drive or folder where the drive item will be created.
|
|
@@ -31,21 +32,21 @@ const defaultChunkSize = exports.chunkSizeMultiple * 32;
|
|
|
31
32
|
* @param contentLength The total size in bytes of the content to be uploaded.
|
|
32
33
|
* @param options Optional. Additional options for the upload operation.
|
|
33
34
|
* @param options.conflictBehavior Optional. Specifies how to handle conflicts if the file already exists. Default is 'fail'.
|
|
34
|
-
* @param options.
|
|
35
|
-
* @param options.progress Optional. A callback function that is called
|
|
35
|
+
* @param options.maxChunkSize Optional. The size of each chunk to be uploaded in bytes. Default is 10MB.
|
|
36
|
+
* @param options.progress Optional. A callback function that is called with the number of bytes uploaded after each chunk.
|
|
36
37
|
* @returns The newly created drive item.
|
|
37
38
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
38
39
|
* @see https://learn.microsoft.com/en-us/graph/api/resources/uploadsession
|
|
39
40
|
*/
|
|
40
41
|
async function createDriveItemContent(parentRef, itemPath, contentStream, contentLength, options = {}) {
|
|
41
|
-
const { conflictBehavior = "fail",
|
|
42
|
-
if (
|
|
43
|
-
throw new InvalidArgumentError_ts_1.default(`Chunk size (${
|
|
42
|
+
const { conflictBehavior = "fail", maxChunkSize = defaultMaxChunkSize, progress = () => { } } = options;
|
|
43
|
+
if (maxChunkSize % exports.chunkSizeMultiple !== 0) {
|
|
44
|
+
throw new InvalidArgumentError_ts_1.default(`Chunk size (${maxChunkSize.toLocaleString()}) must be a multiple of ${(exports.chunkSizeMultiple / 1024).toLocaleString()} KiB *${exports.chunkSizeMultiple.toLocaleString()} bytes).`);
|
|
44
45
|
}
|
|
45
46
|
const pathSegment = parentRef.itemId ? "items/{item-id}" : "root";
|
|
46
47
|
const uploadSessionUrl = `${operationInvoker_ts_1.endpoint}${(0, templatedPaths_ts_1.generatePath)(`/sites/{site-id}/drives/{drive-id}/${pathSegment}:/${itemPath}:/createUploadSession`, parentRef)}`;
|
|
47
48
|
const accessToken = await parentRef.context.generateAccessToken();
|
|
48
|
-
const fileName =
|
|
49
|
+
const fileName = (0, node_path_1.basename)(itemPath);
|
|
49
50
|
const { uploadUrl } = await (0, http_ts_1.execute)({
|
|
50
51
|
url: uploadSessionUrl,
|
|
51
52
|
method: "POST",
|
|
@@ -65,26 +66,46 @@ async function createDriveItemContent(parentRef, itemPath, contentStream, conten
|
|
|
65
66
|
if (!reader) {
|
|
66
67
|
throw new InvalidArgumentError_ts_1.default("contentStream is not an async iterable");
|
|
67
68
|
}
|
|
68
|
-
const buffer = Buffer.alloc(chunkSize);
|
|
69
69
|
let contentPosition = 0;
|
|
70
70
|
let item = null;
|
|
71
|
+
let leftover = null;
|
|
71
72
|
while (contentPosition < contentLength) {
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
73
|
+
const chunkSize = Math.min(maxChunkSize, contentLength - contentPosition);
|
|
74
|
+
const buffer = Buffer.alloc(chunkSize);
|
|
75
|
+
let length = 0;
|
|
76
|
+
while (length < chunkSize) {
|
|
77
|
+
let c = null;
|
|
78
|
+
if (leftover) {
|
|
79
|
+
c = leftover;
|
|
80
|
+
leftover = null;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const { value, done } = await reader.next();
|
|
84
|
+
if (done)
|
|
85
|
+
break;
|
|
86
|
+
c = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
87
|
+
}
|
|
88
|
+
const toCopy = Math.min(c.length, chunkSize - length);
|
|
89
|
+
c.copy(buffer, length, 0, toCopy);
|
|
90
|
+
length += toCopy;
|
|
91
|
+
if (toCopy < c.length) {
|
|
92
|
+
leftover = c.subarray(toCopy);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
const chunkStart = contentPosition;
|
|
96
|
+
const chunkEnd = contentPosition + length - 1;
|
|
97
|
+
const chunk = buffer.subarray(0, length);
|
|
77
98
|
const response = await (0, http_ts_1.execute)({
|
|
78
99
|
url: uploadUrl,
|
|
79
100
|
method: "PUT",
|
|
80
101
|
headers: {
|
|
81
|
-
"Content-Length":
|
|
82
|
-
"Content-Range":
|
|
102
|
+
"Content-Length": `${length}`,
|
|
103
|
+
"Content-Range": `bytes ${chunkStart}-${chunkEnd}/${contentLength}`,
|
|
83
104
|
},
|
|
84
|
-
data:
|
|
105
|
+
data: chunk,
|
|
85
106
|
responseType: "json",
|
|
86
107
|
});
|
|
87
|
-
contentPosition +=
|
|
108
|
+
contentPosition += length;
|
|
88
109
|
progress(contentPosition);
|
|
89
110
|
if (isDriveItem(response)) {
|
|
90
111
|
item = response;
|
|
@@ -103,16 +124,3 @@ async function createDriveItemContent(parentRef, itemPath, contentStream, conten
|
|
|
103
124
|
return typeof obj === "object" && obj !== null && "id" in obj;
|
|
104
125
|
}
|
|
105
126
|
}
|
|
106
|
-
async function read(chunkSize, contentPosition, contentLength, reader, buffer) {
|
|
107
|
-
let length = 0;
|
|
108
|
-
while (length < chunkSize && contentPosition + length < contentLength) {
|
|
109
|
-
const { value, done } = await reader.next();
|
|
110
|
-
if (done)
|
|
111
|
-
break;
|
|
112
|
-
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
113
|
-
const toCopy = Math.min(chunk.length, chunkSize - length, contentLength - contentPosition - length);
|
|
114
|
-
chunk.copy(buffer, length, 0, toCopy);
|
|
115
|
-
length += toCopy;
|
|
116
|
-
}
|
|
117
|
-
return length;
|
|
118
|
-
}
|
|
@@ -9,10 +9,10 @@ import type { DriveItemPath, DriveItemRef } from "../../models/DriveItem.ts";
|
|
|
9
9
|
import type { GraphOperation } from "../../models/GraphOperation.ts";
|
|
10
10
|
/**
|
|
11
11
|
* Retrieve the metadata for an item in a drive by file path.
|
|
12
|
-
* @param
|
|
12
|
+
* @param parentRef Reference to the drive or drive item containing the item.
|
|
13
13
|
* @param itemPath Path of the item within the drive.
|
|
14
14
|
* @returns The metadata of the specified drive item, including its reference information.
|
|
15
15
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-get
|
|
16
16
|
*/
|
|
17
|
-
export default function getDriveItemByPath(
|
|
17
|
+
export default function getDriveItemByPath(parentRef: DriveRef | DriveItemRef, itemPath: DriveItemPath): GraphOperation<DriveItem & DriveItemRef>;
|
|
18
18
|
//# sourceMappingURL=getDriveItemByPath.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDriveItemByPath.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/getDriveItemByPath.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAKrE;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"getDriveItemByPath.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/getDriveItemByPath.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAKrE;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,SAAS,EAAE,QAAQ,GAAG,YAAY,EAAE,QAAQ,EAAE,aAAa,GAAG,cAAc,CAAC,SAAS,GAAG,YAAY,CAAC,CA8BhJ"}
|
|
@@ -15,25 +15,33 @@ const operationInvoker_ts_1 = require("../../services/operationInvoker.js");
|
|
|
15
15
|
const templatedPaths_ts_1 = require("../../services/templatedPaths.js");
|
|
16
16
|
/**
|
|
17
17
|
* Retrieve the metadata for an item in a drive by file path.
|
|
18
|
-
* @param
|
|
18
|
+
* @param parentRef Reference to the drive or drive item containing the item.
|
|
19
19
|
* @param itemPath Path of the item within the drive.
|
|
20
20
|
* @returns The metadata of the specified drive item, including its reference information.
|
|
21
21
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-get
|
|
22
22
|
*/
|
|
23
|
-
function getDriveItemByPath(
|
|
23
|
+
function getDriveItemByPath(parentRef, itemPath) {
|
|
24
24
|
if (!itemPath.startsWith("/")) {
|
|
25
25
|
throw new InvalidArgumentError_ts_1.default("itemPath must start with a forward slash (/)");
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
let normalizedPath;
|
|
28
|
+
if ("id" in parentRef) {
|
|
29
|
+
// parentRef is a DriveItemRef
|
|
30
|
+
normalizedPath = itemPath === "/" ? "/sites/{site-id}/drives/{drive-id}/items/{item-id}" : `/sites/{site-id}/drives/{drive-id}/items/{item-id}:${itemPath}`;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// parentRef is a DriveRef
|
|
34
|
+
normalizedPath = itemPath === "/" ? "/sites/{site-id}/drives/{drive-id}/root" : `/sites/{site-id}/drives/{drive-id}/root:${itemPath}`;
|
|
35
|
+
}
|
|
28
36
|
return (0, operationInvoker_ts_1.operation)({
|
|
29
|
-
context:
|
|
37
|
+
context: parentRef.context,
|
|
30
38
|
method: "GET",
|
|
31
|
-
path: (0, templatedPaths_ts_1.generatePath)(normalizedPath,
|
|
39
|
+
path: (0, templatedPaths_ts_1.generatePath)(normalizedPath, parentRef),
|
|
32
40
|
headers: {},
|
|
33
41
|
body: null,
|
|
34
42
|
responseTransform: (response) => {
|
|
35
43
|
const item = response;
|
|
36
|
-
const itemRef = (0, driveItem_ts_1.createDriveItemRef)(
|
|
44
|
+
const itemRef = (0, driveItem_ts_1.createDriveItemRef)(parentRef, item.id);
|
|
37
45
|
return {
|
|
38
46
|
...item,
|
|
39
47
|
...itemRef,
|
|
@@ -20,7 +20,7 @@ export declare const chunkSizeMultiple: number;
|
|
|
20
20
|
*/
|
|
21
21
|
export interface CreateDriveItemContentOptions {
|
|
22
22
|
conflictBehavior?: "fail" | "replace" | "rename";
|
|
23
|
-
|
|
23
|
+
maxChunkSize?: number;
|
|
24
24
|
progress?: (bytes: number) => void;
|
|
25
25
|
}
|
|
26
26
|
/**
|
|
@@ -31,8 +31,8 @@ export interface CreateDriveItemContentOptions {
|
|
|
31
31
|
* @param contentLength The total size in bytes of the content to be uploaded.
|
|
32
32
|
* @param options Optional. Additional options for the upload operation.
|
|
33
33
|
* @param options.conflictBehavior Optional. Specifies how to handle conflicts if the file already exists. Default is 'fail'.
|
|
34
|
-
* @param options.
|
|
35
|
-
* @param options.progress Optional. A callback function that is called
|
|
34
|
+
* @param options.maxChunkSize Optional. The size of each chunk to be uploaded in bytes. Default is 10MB.
|
|
35
|
+
* @param options.progress Optional. A callback function that is called with the number of bytes uploaded after each chunk.
|
|
36
36
|
* @returns The newly created drive item.
|
|
37
37
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
38
38
|
* @see https://learn.microsoft.com/en-us/graph/api/resources/uploadsession
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createDriveItemContent.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/createDriveItemContent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"createDriveItemContent.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/createDriveItemContent.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAIlE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAM1F;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,QAAa,CAAC;AAc5C;;;;;GAKG;AACH,MAAM,WAAW,6BAA6B;IAC7C,gBAAgB,CAAC,EAAE,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACnC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAA8B,sBAAsB,CAAC,SAAS,EAAE,QAAQ,GAAG,YAAY,EAAE,QAAQ,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,CAAC,cAAc,EAAE,aAAa,EAAE,MAAM,EAAE,OAAO,GAAE,6BAAkC,GAAG,OAAO,CAAC,SAAS,GAAG,YAAY,CAAC,CAmG7P"}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* @module createDriveItemContent
|
|
4
4
|
* @category Operations
|
|
5
5
|
*/
|
|
6
|
+
import { basename } from "node:path";
|
|
6
7
|
import InvalidArgumentError from "../../errors/InvalidArgumentError.js";
|
|
7
8
|
import ProtocolError from "../../errors/ProtocolError.js";
|
|
8
9
|
import { createDriveItemRef } from "../../services/driveItem.js";
|
|
@@ -15,7 +16,7 @@ import { generatePath } from "../../services/templatedPaths.js";
|
|
|
15
16
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
16
17
|
*/
|
|
17
18
|
export const chunkSizeMultiple = 320 * 1024;
|
|
18
|
-
const
|
|
19
|
+
const defaultMaxChunkSize = chunkSizeMultiple * 32;
|
|
19
20
|
/**
|
|
20
21
|
* Creates a new drive item in the specified parent drive or folder using a stream as content.
|
|
21
22
|
* @param parentRef Reference to the parent drive or folder where the drive item will be created.
|
|
@@ -24,21 +25,21 @@ const defaultChunkSize = chunkSizeMultiple * 32;
|
|
|
24
25
|
* @param contentLength The total size in bytes of the content to be uploaded.
|
|
25
26
|
* @param options Optional. Additional options for the upload operation.
|
|
26
27
|
* @param options.conflictBehavior Optional. Specifies how to handle conflicts if the file already exists. Default is 'fail'.
|
|
27
|
-
* @param options.
|
|
28
|
-
* @param options.progress Optional. A callback function that is called
|
|
28
|
+
* @param options.maxChunkSize Optional. The size of each chunk to be uploaded in bytes. Default is 10MB.
|
|
29
|
+
* @param options.progress Optional. A callback function that is called with the number of bytes uploaded after each chunk.
|
|
29
30
|
* @returns The newly created drive item.
|
|
30
31
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
31
32
|
* @see https://learn.microsoft.com/en-us/graph/api/resources/uploadsession
|
|
32
33
|
*/
|
|
33
34
|
export default async function createDriveItemContent(parentRef, itemPath, contentStream, contentLength, options = {}) {
|
|
34
|
-
const { conflictBehavior = "fail",
|
|
35
|
-
if (
|
|
36
|
-
throw new InvalidArgumentError(`Chunk size (${
|
|
35
|
+
const { conflictBehavior = "fail", maxChunkSize = defaultMaxChunkSize, progress = () => { } } = options;
|
|
36
|
+
if (maxChunkSize % chunkSizeMultiple !== 0) {
|
|
37
|
+
throw new InvalidArgumentError(`Chunk size (${maxChunkSize.toLocaleString()}) must be a multiple of ${(chunkSizeMultiple / 1024).toLocaleString()} KiB *${chunkSizeMultiple.toLocaleString()} bytes).`);
|
|
37
38
|
}
|
|
38
39
|
const pathSegment = parentRef.itemId ? "items/{item-id}" : "root";
|
|
39
40
|
const uploadSessionUrl = `${endpoint}${generatePath(`/sites/{site-id}/drives/{drive-id}/${pathSegment}:/${itemPath}:/createUploadSession`, parentRef)}`;
|
|
40
41
|
const accessToken = await parentRef.context.generateAccessToken();
|
|
41
|
-
const fileName = itemPath
|
|
42
|
+
const fileName = basename(itemPath);
|
|
42
43
|
const { uploadUrl } = await execute({
|
|
43
44
|
url: uploadSessionUrl,
|
|
44
45
|
method: "POST",
|
|
@@ -58,26 +59,46 @@ export default async function createDriveItemContent(parentRef, itemPath, conten
|
|
|
58
59
|
if (!reader) {
|
|
59
60
|
throw new InvalidArgumentError("contentStream is not an async iterable");
|
|
60
61
|
}
|
|
61
|
-
const buffer = Buffer.alloc(chunkSize);
|
|
62
62
|
let contentPosition = 0;
|
|
63
63
|
let item = null;
|
|
64
|
+
let leftover = null;
|
|
64
65
|
while (contentPosition < contentLength) {
|
|
65
|
-
const
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
const chunkSize = Math.min(maxChunkSize, contentLength - contentPosition);
|
|
67
|
+
const buffer = Buffer.alloc(chunkSize);
|
|
68
|
+
let length = 0;
|
|
69
|
+
while (length < chunkSize) {
|
|
70
|
+
let c = null;
|
|
71
|
+
if (leftover) {
|
|
72
|
+
c = leftover;
|
|
73
|
+
leftover = null;
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
const { value, done } = await reader.next();
|
|
77
|
+
if (done)
|
|
78
|
+
break;
|
|
79
|
+
c = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
80
|
+
}
|
|
81
|
+
const toCopy = Math.min(c.length, chunkSize - length);
|
|
82
|
+
c.copy(buffer, length, 0, toCopy);
|
|
83
|
+
length += toCopy;
|
|
84
|
+
if (toCopy < c.length) {
|
|
85
|
+
leftover = c.subarray(toCopy);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
const chunkStart = contentPosition;
|
|
89
|
+
const chunkEnd = contentPosition + length - 1;
|
|
90
|
+
const chunk = buffer.subarray(0, length);
|
|
70
91
|
const response = await execute({
|
|
71
92
|
url: uploadUrl,
|
|
72
93
|
method: "PUT",
|
|
73
94
|
headers: {
|
|
74
|
-
"Content-Length":
|
|
75
|
-
"Content-Range":
|
|
95
|
+
"Content-Length": `${length}`,
|
|
96
|
+
"Content-Range": `bytes ${chunkStart}-${chunkEnd}/${contentLength}`,
|
|
76
97
|
},
|
|
77
|
-
data:
|
|
98
|
+
data: chunk,
|
|
78
99
|
responseType: "json",
|
|
79
100
|
});
|
|
80
|
-
contentPosition +=
|
|
101
|
+
contentPosition += length;
|
|
81
102
|
progress(contentPosition);
|
|
82
103
|
if (isDriveItem(response)) {
|
|
83
104
|
item = response;
|
|
@@ -96,16 +117,3 @@ export default async function createDriveItemContent(parentRef, itemPath, conten
|
|
|
96
117
|
return typeof obj === "object" && obj !== null && "id" in obj;
|
|
97
118
|
}
|
|
98
119
|
}
|
|
99
|
-
async function read(chunkSize, contentPosition, contentLength, reader, buffer) {
|
|
100
|
-
let length = 0;
|
|
101
|
-
while (length < chunkSize && contentPosition + length < contentLength) {
|
|
102
|
-
const { value, done } = await reader.next();
|
|
103
|
-
if (done)
|
|
104
|
-
break;
|
|
105
|
-
const chunk = Buffer.isBuffer(value) ? value : Buffer.from(value);
|
|
106
|
-
const toCopy = Math.min(chunk.length, chunkSize - length, contentLength - contentPosition - length);
|
|
107
|
-
chunk.copy(buffer, length, 0, toCopy);
|
|
108
|
-
length += toCopy;
|
|
109
|
-
}
|
|
110
|
-
return length;
|
|
111
|
-
}
|
|
@@ -9,10 +9,10 @@ import type { DriveItemPath, DriveItemRef } from "../../models/DriveItem.ts";
|
|
|
9
9
|
import type { GraphOperation } from "../../models/GraphOperation.ts";
|
|
10
10
|
/**
|
|
11
11
|
* Retrieve the metadata for an item in a drive by file path.
|
|
12
|
-
* @param
|
|
12
|
+
* @param parentRef Reference to the drive or drive item containing the item.
|
|
13
13
|
* @param itemPath Path of the item within the drive.
|
|
14
14
|
* @returns The metadata of the specified drive item, including its reference information.
|
|
15
15
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-get
|
|
16
16
|
*/
|
|
17
|
-
export default function getDriveItemByPath(
|
|
17
|
+
export default function getDriveItemByPath(parentRef: DriveRef | DriveItemRef, itemPath: DriveItemPath): GraphOperation<DriveItem & DriveItemRef>;
|
|
18
18
|
//# sourceMappingURL=getDriveItemByPath.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDriveItemByPath.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/getDriveItemByPath.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAKrE;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,
|
|
1
|
+
{"version":3,"file":"getDriveItemByPath.d.ts","sourceRoot":"","sources":["../../../../src/operations/driveItem/getDriveItemByPath.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,kCAAkC,CAAC;AAElE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACtD,OAAO,KAAK,EAAe,aAAa,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAC1F,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gCAAgC,CAAC;AAKrE;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,kBAAkB,CAAC,SAAS,EAAE,QAAQ,GAAG,YAAY,EAAE,QAAQ,EAAE,aAAa,GAAG,cAAc,CAAC,SAAS,GAAG,YAAY,CAAC,CA8BhJ"}
|
|
@@ -9,25 +9,33 @@ import { operation } from "../../services/operationInvoker.js";
|
|
|
9
9
|
import { generatePath } from "../../services/templatedPaths.js";
|
|
10
10
|
/**
|
|
11
11
|
* Retrieve the metadata for an item in a drive by file path.
|
|
12
|
-
* @param
|
|
12
|
+
* @param parentRef Reference to the drive or drive item containing the item.
|
|
13
13
|
* @param itemPath Path of the item within the drive.
|
|
14
14
|
* @returns The metadata of the specified drive item, including its reference information.
|
|
15
15
|
* @see https://learn.microsoft.com/en-us/graph/api/driveitem-get
|
|
16
16
|
*/
|
|
17
|
-
export default function getDriveItemByPath(
|
|
17
|
+
export default function getDriveItemByPath(parentRef, itemPath) {
|
|
18
18
|
if (!itemPath.startsWith("/")) {
|
|
19
19
|
throw new InvalidArgumentError("itemPath must start with a forward slash (/)");
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
let normalizedPath;
|
|
22
|
+
if ("id" in parentRef) {
|
|
23
|
+
// parentRef is a DriveItemRef
|
|
24
|
+
normalizedPath = itemPath === "/" ? "/sites/{site-id}/drives/{drive-id}/items/{item-id}" : `/sites/{site-id}/drives/{drive-id}/items/{item-id}:${itemPath}`;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
// parentRef is a DriveRef
|
|
28
|
+
normalizedPath = itemPath === "/" ? "/sites/{site-id}/drives/{drive-id}/root" : `/sites/{site-id}/drives/{drive-id}/root:${itemPath}`;
|
|
29
|
+
}
|
|
22
30
|
return operation({
|
|
23
|
-
context:
|
|
31
|
+
context: parentRef.context,
|
|
24
32
|
method: "GET",
|
|
25
|
-
path: generatePath(normalizedPath,
|
|
33
|
+
path: generatePath(normalizedPath, parentRef),
|
|
26
34
|
headers: {},
|
|
27
35
|
body: null,
|
|
28
36
|
responseTransform: (response) => {
|
|
29
37
|
const item = response;
|
|
30
|
-
const itemRef = createDriveItemRef(
|
|
38
|
+
const itemRef = createDriveItemRef(parentRef, item.id);
|
|
31
39
|
return {
|
|
32
40
|
...item,
|
|
33
41
|
...itemRef,
|
|
@@ -8,33 +8,31 @@ Creates a new drive item in the specified parent drive or folder using a stream
|
|
|
8
8
|
|
|
9
9
|
### CreateDriveItemContentOptions
|
|
10
10
|
|
|
11
|
-
Defined in: [src/operations/driveItem/createDriveItemContent.ts:
|
|
11
|
+
Defined in: [src/operations/driveItem/createDriveItemContent.ts:43](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L43)
|
|
12
12
|
|
|
13
13
|
Options for creating a drive item with content upload.
|
|
14
14
|
|
|
15
15
|
#### Properties
|
|
16
16
|
|
|
17
|
-
#####
|
|
17
|
+
##### conflictBehavior?
|
|
18
18
|
|
|
19
|
-
> `optional` **
|
|
19
|
+
> `optional` **conflictBehavior**: `"replace"` \| `"fail"` \| `"rename"`
|
|
20
20
|
|
|
21
21
|
Defined in: [src/operations/driveItem/createDriveItemContent.ts:44](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L44)
|
|
22
22
|
|
|
23
|
-
Optional.
|
|
23
|
+
Optional. Specifies how to handle conflicts if the file already exists. Can be 'fail', 'replace', or 'rename'.
|
|
24
24
|
|
|
25
|
-
#####
|
|
25
|
+
##### maxChunkSize?
|
|
26
26
|
|
|
27
|
-
> `optional` **
|
|
27
|
+
> `optional` **maxChunkSize**: `number`
|
|
28
28
|
|
|
29
|
-
Defined in: [src/operations/driveItem/createDriveItemContent.ts:
|
|
30
|
-
|
|
31
|
-
Optional. Specifies how to handle conflicts if the file already exists. Can be 'fail', 'replace', or 'rename'.
|
|
29
|
+
Defined in: [src/operations/driveItem/createDriveItemContent.ts:45](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L45)
|
|
32
30
|
|
|
33
31
|
##### progress()?
|
|
34
32
|
|
|
35
33
|
> `optional` **progress**: (`bytes`) => `void`
|
|
36
34
|
|
|
37
|
-
Defined in: [src/operations/driveItem/createDriveItemContent.ts:
|
|
35
|
+
Defined in: [src/operations/driveItem/createDriveItemContent.ts:46](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L46)
|
|
38
36
|
|
|
39
37
|
Optional. Callback function called with the number of bytes uploaded after each chunk.
|
|
40
38
|
|
|
@@ -54,7 +52,7 @@ Optional. Callback function called with the number of bytes uploaded after each
|
|
|
54
52
|
|
|
55
53
|
> `const` **chunkSizeMultiple**: `number`
|
|
56
54
|
|
|
57
|
-
Defined in: [src/operations/driveItem/createDriveItemContent.ts:
|
|
55
|
+
Defined in: [src/operations/driveItem/createDriveItemContent.ts:23](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L23)
|
|
58
56
|
|
|
59
57
|
The required chunk size multiple for upload sessions.
|
|
60
58
|
|
|
@@ -72,7 +70,7 @@ https://learn.microsoft.com/en-us/graph/api/driveitem-createuploadsession
|
|
|
72
70
|
|
|
73
71
|
> **createDriveItemContent**(`parentRef`, `itemPath`, `contentStream`, `contentLength`, `options`): [`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)\<`DriveItem` & [`SiteRef`](Site-1.md#siteref) & `object` & `object`\>
|
|
74
72
|
|
|
75
|
-
Defined in: [src/operations/driveItem/createDriveItemContent.ts:
|
|
73
|
+
Defined in: [src/operations/driveItem/createDriveItemContent.ts:63](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/createDriveItemContent.ts#L63)
|
|
76
74
|
|
|
77
75
|
Creates a new drive item in the specified parent drive or folder using a stream as content.
|
|
78
76
|
|
|
@@ -8,7 +8,7 @@ Retrieve the metadata for an item in a drive by file path.
|
|
|
8
8
|
|
|
9
9
|
### getDriveItemByPath()
|
|
10
10
|
|
|
11
|
-
> **getDriveItemByPath**(`
|
|
11
|
+
> **getDriveItemByPath**(`parentRef`, `itemPath`): [`GraphOperation`](GraphOperation.md#graphoperation)\<`DriveItem` & [`SiteRef`](Site-1.md#siteref) & `object` & `object`\>
|
|
12
12
|
|
|
13
13
|
Defined in: [src/operations/driveItem/getDriveItemByPath.ts:23](https://github.com/Future-Secure-AI/microsoft-graph/blob/main/src/operations/driveItem/getDriveItemByPath.ts#L23)
|
|
14
14
|
|
|
@@ -18,7 +18,7 @@ Retrieve the metadata for an item in a drive by file path.
|
|
|
18
18
|
|
|
19
19
|
| Parameter | Type | Description |
|
|
20
20
|
| ------ | ------ | ------ |
|
|
21
|
-
| `
|
|
21
|
+
| `parentRef` | [`DriveRef`](Drive-1.md#driveref) \| [`DriveItemRef`](DriveItem-1.md#driveitemref) | Reference to the drive or drive item containing the item. |
|
|
22
22
|
| `itemPath` | [`DriveItemPath`](DriveItem-1.md#driveitempath) | Path of the item within the drive. |
|
|
23
23
|
|
|
24
24
|
#### Returns
|