@secret-momo/notion 1.0.5 → 1.1.1
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/lib/upload.d.ts.map +1 -1
- package/lib/upload.js +33 -12
- package/package.json +1 -1
package/lib/upload.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/upload.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"upload.d.ts","sourceRoot":"","sources":["../src/upload.ts"],"names":[],"mappings":"AA2KA;;;;;GAKG;AACH,wBAAsB,yBAAyB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,iBAuBnF;AAED;;;;GAIG;AACH,wBAAsB,wBAAwB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAYpF;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,iBAYpF;AAED;;;GAGG;AACH,wBAAsB,0BAA0B,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,iBAqBpF"}
|
package/lib/upload.js
CHANGED
|
@@ -5,7 +5,8 @@ import '@notionhq/client';
|
|
|
5
5
|
import { newNotionClient } from './client.js';
|
|
6
6
|
|
|
7
7
|
const SINGLE_PART_MAX_BYTES = 20 * 1024 * 1024;
|
|
8
|
-
const MULTIPART_CHUNK_BYTES =
|
|
8
|
+
const MULTIPART_CHUNK_BYTES = 18 * 1024 * 1024;
|
|
9
|
+
const MULTIPART_UPLOAD_CONCURRENCY = 3;
|
|
9
10
|
const MIME_BY_EXT = {
|
|
10
11
|
".avif": "image/avif",
|
|
11
12
|
".bmp": "image/bmp",
|
|
@@ -55,6 +56,22 @@ async function ensureLocalFileReadable(absolutePath) {
|
|
|
55
56
|
throw new Error(`\u627E\u4E0D\u5230\u8981\u4E0A\u4F20\u7684\u6587\u4EF6: ${absolutePath}`);
|
|
56
57
|
}
|
|
57
58
|
}
|
|
59
|
+
async function runWithConcurrency(items, concurrency, worker) {
|
|
60
|
+
let nextIndex = 0;
|
|
61
|
+
const workerCount = Math.min(concurrency, items.length);
|
|
62
|
+
await Promise.all(
|
|
63
|
+
Array.from({ length: workerCount }, async () => {
|
|
64
|
+
while (nextIndex < items.length) {
|
|
65
|
+
const currentIndex = nextIndex++;
|
|
66
|
+
const item = items[currentIndex];
|
|
67
|
+
if (item === void 0) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
await worker(item);
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
}
|
|
58
75
|
async function completeFileUpload(displayName, contentType, fileBuffer) {
|
|
59
76
|
const notion = newNotionClient();
|
|
60
77
|
if (fileBuffer.length <= SINGLE_PART_MAX_BYTES) {
|
|
@@ -80,17 +97,21 @@ async function completeFileUpload(displayName, contentType, fileBuffer) {
|
|
|
80
97
|
content_type: contentType,
|
|
81
98
|
number_of_parts
|
|
82
99
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
await runWithConcurrency(
|
|
101
|
+
Array.from({ length: number_of_parts }, (_, index) => index + 1),
|
|
102
|
+
MULTIPART_UPLOAD_CONCURRENCY,
|
|
103
|
+
async (part) => {
|
|
104
|
+
const start = (part - 1) * MULTIPART_CHUNK_BYTES;
|
|
105
|
+
const end = Math.min(start + MULTIPART_CHUNK_BYTES, fileBuffer.length);
|
|
106
|
+
const slice = fileBuffer.subarray(start, end);
|
|
107
|
+
const data = new Blob([slice], { type: contentType });
|
|
108
|
+
await notion.fileUploads.send({
|
|
109
|
+
file_upload_id: created.id,
|
|
110
|
+
part_number: String(part),
|
|
111
|
+
file: { filename: displayName, data }
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
);
|
|
94
115
|
const afterComplete = await notion.fileUploads.complete({
|
|
95
116
|
file_upload_id: created.id
|
|
96
117
|
});
|