@secret-momo/notion 1.0.4 → 1.1.0
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/index.d.ts +1 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -1
- package/lib/upload.d.ts +6 -6
- package/lib/upload.d.ts.map +1 -1
- package/lib/upload.js +39 -15
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type NumberPropertyItemObjectResponse, type PageObjectResponse, type RichTextItemResponse } from '@notionhq/client';
|
|
2
2
|
import { newNotionClient } from './client';
|
|
3
|
-
export { uploadLocalFileToPageBody, uploadLocalImageToPageBody, uploadLocalVideoToPageBody, } from './upload';
|
|
3
|
+
export { uploadLocalFileToPageBody, uploadLocalImageToPageBody, uploadLocalVideoToPageBody, uploadLocalImageToNotion, } from './upload';
|
|
4
4
|
export { newNotionClient };
|
|
5
5
|
export type { NumberPropertyItemObjectResponse, PageObjectResponse, RichTextItemResponse };
|
|
6
6
|
export declare function n(num?: number): number | null;
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,gCAAgC,EACrC,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EAC1B,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EACL,yBAAyB,EACzB,0BAA0B,EAC1B,0BAA0B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,gCAAgC,EACrC,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,EAC1B,MAAM,kBAAkB,CAAC;AAE1B,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EACL,yBAAyB,EACzB,0BAA0B,EAC1B,0BAA0B,EAC1B,wBAAwB,GACzB,MAAM,UAAU,CAAC;AAElB,OAAO,EAAE,eAAe,EAAE,CAAC;AAC3B,YAAY,EAAE,gCAAgC,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,CAAC;AAE3F,wBAAgB,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,iBAE7B;AAED,wBAAsB,kBAAkB,CACtC,EAAE,EAAE,CAAC,IAAI,EAAE,kBAAkB,KAAK,IAAI,EACtC,YAAY,CAAC,EAAE,MAAM,iBAMtB;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CActD"}
|
package/lib/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '@notionhq/client';
|
|
2
2
|
import { newNotionClient } from './client.js';
|
|
3
|
-
export { uploadLocalFileToPageBody, uploadLocalImageToPageBody, uploadLocalVideoToPageBody } from './upload.js';
|
|
3
|
+
export { uploadLocalFileToPageBody, uploadLocalImageToNotion, uploadLocalImageToPageBody, uploadLocalVideoToPageBody } from './upload.js';
|
|
4
4
|
|
|
5
5
|
function n(num) {
|
|
6
6
|
return num ?? null;
|
package/lib/upload.d.ts
CHANGED
|
@@ -1,9 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 创建并完成 Notion File Upload,返回 `file_upload_id`。
|
|
3
|
-
* ≤20 MiB 用单段;更大文件用官方多段流程(每段约 10 MiB,最后一段可更小)。
|
|
4
|
-
* Blob 的 MIME 须与 create 时一致。
|
|
5
|
-
*/
|
|
6
|
-
export declare function completeFileUpload(displayName: string, contentType: string, fileBuffer: Buffer): Promise<string>;
|
|
7
1
|
/**
|
|
8
2
|
* 将本地文件经 Notion File Upload API 上传后,作为「文件」块挂到页面正文中。
|
|
9
3
|
* @param pageId - 页面 ID
|
|
@@ -11,6 +5,12 @@ export declare function completeFileUpload(displayName: string, contentType: str
|
|
|
11
5
|
* @param contentType - 可选;默认按扩展名推断,未知则为 application/octet-stream
|
|
12
6
|
*/
|
|
13
7
|
export declare function uploadLocalFileToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* 上传本地图片到 Notion(数据库中附件中需要使用到这个函数)
|
|
10
|
+
* @param absolutePath - 本地图片路径
|
|
11
|
+
* @returns - 文件上传 ID
|
|
12
|
+
*/
|
|
13
|
+
export declare function uploadLocalImageToNotion(absolutePath: string): Promise<string>;
|
|
14
14
|
/**
|
|
15
15
|
* 上传本地图片并在页面正文插入「图片」块(`image`)。
|
|
16
16
|
* @param contentType - 可选;默认按扩展名推断,须为 image/*
|
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 = 16 * 1024 * 1024;
|
|
9
|
+
const MULTIPART_UPLOAD_CONCURRENCY = 2;
|
|
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
|
});
|
|
@@ -124,7 +145,7 @@ async function uploadLocalFileToPageBody(pageId, absolutePath) {
|
|
|
124
145
|
}
|
|
125
146
|
]);
|
|
126
147
|
}
|
|
127
|
-
async function
|
|
148
|
+
async function uploadLocalImageToNotion(absolutePath) {
|
|
128
149
|
const displayName = basename(absolutePath);
|
|
129
150
|
await ensureLocalFileReadable(absolutePath);
|
|
130
151
|
const mime = contentTypeFromPath(absolutePath);
|
|
@@ -132,7 +153,10 @@ async function uploadLocalImageToPageBody(pageId, absolutePath) {
|
|
|
132
153
|
throw new Error(`\u4E0D\u662F\u56FE\u7247 MIME\uFF08${mime}\uFF09\uFF0C\u8BF7\u4F20\u56FE\u7247\u8DEF\u5F84\u6216\u663E\u5F0F\u6307\u5B9A contentType`);
|
|
133
154
|
}
|
|
134
155
|
const fileBuffer = await readFile(absolutePath);
|
|
135
|
-
|
|
156
|
+
return completeFileUpload(displayName, mime, fileBuffer);
|
|
157
|
+
}
|
|
158
|
+
async function uploadLocalImageToPageBody(pageId, absolutePath) {
|
|
159
|
+
const fileUploadId = await uploadLocalImageToNotion(absolutePath);
|
|
136
160
|
await appendBlocksToPage(pageId, [
|
|
137
161
|
{
|
|
138
162
|
type: "image",
|
|
@@ -163,4 +187,4 @@ async function uploadLocalVideoToPageBody(pageId, absolutePath) {
|
|
|
163
187
|
]);
|
|
164
188
|
}
|
|
165
189
|
|
|
166
|
-
export {
|
|
190
|
+
export { uploadLocalFileToPageBody, uploadLocalImageToNotion, uploadLocalImageToPageBody, uploadLocalVideoToPageBody };
|