@secret-momo/notion 1.1.0 → 2.0.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/helpers/custom-fetch.d.ts +2 -0
- package/lib/helpers/custom-fetch.d.ts.map +1 -0
- package/lib/{client.js → helpers/custom-fetch.js} +1 -11
- package/lib/helpers/page.d.ts +12 -0
- package/lib/helpers/page.d.ts.map +1 -0
- package/lib/helpers/page.js +16 -0
- package/lib/helpers/upload/config.d.ts +8 -0
- package/lib/helpers/upload/config.d.ts.map +1 -0
- package/lib/helpers/upload/config.js +32 -0
- package/lib/helpers/upload/helper.d.ts +13 -0
- package/lib/helpers/upload/helper.d.ts.map +1 -0
- package/lib/helpers/upload/helper.js +45 -0
- package/lib/helpers/upload/index.d.ts +5 -0
- package/lib/helpers/upload/index.d.ts.map +1 -0
- package/lib/helpers/upload/index.js +96 -0
- package/lib/index.d.ts +3 -15
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +3 -27
- package/lib/notion-client.d.ts +29 -0
- package/lib/notion-client.d.ts.map +1 -0
- package/lib/notion-client.js +93 -0
- package/lib/utils.d.ts +2 -0
- package/lib/utils.d.ts.map +1 -0
- package/lib/utils.js +5 -0
- package/package.json +9 -8
- package/lib/client.d.ts +0 -3
- package/lib/client.d.ts.map +0 -1
- package/lib/upload.d.ts +0 -24
- package/lib/upload.d.ts.map +0 -1
- package/lib/upload.js +0 -190
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom-fetch.d.ts","sourceRoot":"","sources":["../../src/helpers/custom-fetch.ts"],"names":[],"mappings":"AAiBA,wBAAsB,WAAW,CAC/B,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,OAAO,EAC7B,IAAI,CAAC,EAAE,WAAW,GACjB,OAAO,CAAC,QAAQ,CAAC,CAgCnB"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { Client } from '@notionhq/client';
|
|
2
1
|
import { logger, sleep } from '@secret-momo/utils';
|
|
3
2
|
|
|
4
3
|
const NOTION_TLS_MAX_RETRIES = 5;
|
|
@@ -32,14 +31,5 @@ async function customFetch(input, init) {
|
|
|
32
31
|
}
|
|
33
32
|
throw new Error("Unexpected notion fetch retry failure");
|
|
34
33
|
}
|
|
35
|
-
function newNotionClient(authToken) {
|
|
36
|
-
const auth = authToken ?? process.env.NOTION_AUTH_TOKEN;
|
|
37
|
-
return new Client({
|
|
38
|
-
auth,
|
|
39
|
-
fetch: customFetch,
|
|
40
|
-
timeoutMs: 1e3 * 60 * 10
|
|
41
|
-
// 10 分钟超时的时间
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
34
|
|
|
45
|
-
export {
|
|
35
|
+
export { customFetch };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { PageObjectResponse } from '@notionhq/client';
|
|
2
|
+
/**
|
|
3
|
+
* 从 API 返回的 page(或任意带 `id` 的对象)拼出可在浏览器打开的 Notion 页面地址
|
|
4
|
+
* @param pageId - 页面 ID
|
|
5
|
+
* @returns 可在浏览器打开的 Notion 页面地址
|
|
6
|
+
*/
|
|
7
|
+
export declare function buildNotionPageWebUrl(pageId: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* 获取数据库页面的 ID
|
|
10
|
+
*/
|
|
11
|
+
export declare function getId(page: PageObjectResponse): number;
|
|
12
|
+
//# sourceMappingURL=page.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../../src/helpers/page.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAE3D;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAE5D;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,kBAAkB,GAAG,MAAM,CActD"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
function buildNotionPageWebUrl(pageId) {
|
|
2
|
+
return `https://www.notion.so/${pageId.replace(/-/g, "")}`;
|
|
3
|
+
}
|
|
4
|
+
function getId(page) {
|
|
5
|
+
const idProperty = page.properties["ID"];
|
|
6
|
+
if (idProperty?.type !== "unique_id") {
|
|
7
|
+
throw new Error("ID \u5C5E\u6027\u4E0D\u662F unique_id \u7C7B\u578B");
|
|
8
|
+
}
|
|
9
|
+
const id = idProperty.unique_id.number;
|
|
10
|
+
if (id === null) {
|
|
11
|
+
throw new Error("ID \u5C5E\u6027\u4E3A\u7A7A");
|
|
12
|
+
}
|
|
13
|
+
return id;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { buildNotionPageWebUrl, getId };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Notion 单段上传上限(二进制 MiB,与官方文档一致) */
|
|
2
|
+
export declare const SINGLE_PART_MAX_BYTES: number;
|
|
3
|
+
/** 多段上传每段建议 18 MiB;非末段须在 5-20 MiB 之间 */
|
|
4
|
+
export declare const MULTIPART_CHUNK_BYTES: number;
|
|
5
|
+
/** Notion multipart part 可并行发送;保守控制并发,避免触发 API rate limit */
|
|
6
|
+
export declare const MULTIPART_UPLOAD_CONCURRENCY = 3;
|
|
7
|
+
export declare const MIME_BY_EXT: Record<string, string>;
|
|
8
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/helpers/upload/config.ts"],"names":[],"mappings":"AAAA,qCAAqC;AACrC,eAAO,MAAM,qBAAqB,QAAmB,CAAC;AACtD,wCAAwC;AACxC,eAAO,MAAM,qBAAqB,QAAmB,CAAC;AACtD,6DAA6D;AAC7D,eAAO,MAAM,4BAA4B,IAAI,CAAC;AAE9C,eAAO,MAAM,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CA0B9C,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const SINGLE_PART_MAX_BYTES = 20 * 1024 * 1024;
|
|
2
|
+
const MULTIPART_CHUNK_BYTES = 18 * 1024 * 1024;
|
|
3
|
+
const MULTIPART_UPLOAD_CONCURRENCY = 3;
|
|
4
|
+
const MIME_BY_EXT = {
|
|
5
|
+
".avif": "image/avif",
|
|
6
|
+
".bmp": "image/bmp",
|
|
7
|
+
".gif": "image/gif",
|
|
8
|
+
".heic": "image/heic",
|
|
9
|
+
".heif": "image/heif",
|
|
10
|
+
".ico": "image/x-icon",
|
|
11
|
+
".jpeg": "image/jpeg",
|
|
12
|
+
".jpg": "image/jpeg",
|
|
13
|
+
".png": "image/png",
|
|
14
|
+
".svg": "image/svg+xml",
|
|
15
|
+
".webp": "image/webp",
|
|
16
|
+
".html": "text/html",
|
|
17
|
+
".htm": "text/html",
|
|
18
|
+
".mp4": "video/mp4",
|
|
19
|
+
".webm": "video/webm",
|
|
20
|
+
".mov": "video/quicktime",
|
|
21
|
+
".m4v": "video/x-m4v",
|
|
22
|
+
".mkv": "video/x-matroska",
|
|
23
|
+
".mpeg": "video/mpeg",
|
|
24
|
+
".mpg": "video/mpeg",
|
|
25
|
+
".avi": "video/x-msvideo",
|
|
26
|
+
".pdf": "application/pdf",
|
|
27
|
+
".zip": "application/zip",
|
|
28
|
+
".json": "application/json",
|
|
29
|
+
".txt": "text/plain"
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { MIME_BY_EXT, MULTIPART_CHUNK_BYTES, MULTIPART_UPLOAD_CONCURRENCY, SINGLE_PART_MAX_BYTES };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function contentTypeFromPath(absolutePath: string): string;
|
|
2
|
+
/**
|
|
3
|
+
* Notion 的 send 接口经 Cloudflare;原始 HTML(尤其公众号全文)易触发 WAF 403。
|
|
4
|
+
* 上传前 gzip,以 application/gzip + `.html.gz` 规避内容检测。
|
|
5
|
+
*/
|
|
6
|
+
export declare function prepareBufferForNotionUpload(absolutePath: string, fileBuffer: Buffer, displayName: string, mime: string): {
|
|
7
|
+
buffer: Buffer;
|
|
8
|
+
displayName: string;
|
|
9
|
+
mime: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function ensureLocalFileReadable(absolutePath: string): Promise<void>;
|
|
12
|
+
export declare function runWithConcurrency<T>(items: T[], concurrency: number, worker: (item: T) => Promise<void>): Promise<void>;
|
|
13
|
+
//# sourceMappingURL=helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../../../src/helpers/upload/helper.ts"],"names":[],"mappings":"AAMA,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAGhE;AAED;;;GAGG;AACH,wBAAgB,4BAA4B,CAC1C,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,EAClB,WAAW,EAAE,MAAM,EACnB,IAAI,EAAE,MAAM,GACX;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAWvD;AAED,wBAAsB,uBAAuB,CAAC,YAAY,EAAE,MAAM,iBAMjE;AAED,wBAAsB,kBAAkB,CAAC,CAAC,EACxC,KAAK,EAAE,CAAC,EAAE,EACV,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,iBAiBnC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { extname } from 'node:path';
|
|
2
|
+
import { access } from 'node:fs/promises';
|
|
3
|
+
import { gzipSync } from 'node:zlib';
|
|
4
|
+
import { MIME_BY_EXT } from './config.js';
|
|
5
|
+
|
|
6
|
+
function contentTypeFromPath(absolutePath) {
|
|
7
|
+
const ext = extname(absolutePath).toLowerCase();
|
|
8
|
+
return MIME_BY_EXT[ext] ?? "application/octet-stream";
|
|
9
|
+
}
|
|
10
|
+
function prepareBufferForNotionUpload(absolutePath, fileBuffer, displayName, mime) {
|
|
11
|
+
const ext = extname(absolutePath).toLowerCase();
|
|
12
|
+
if (ext !== ".html") {
|
|
13
|
+
return { buffer: fileBuffer, displayName, mime };
|
|
14
|
+
}
|
|
15
|
+
return {
|
|
16
|
+
buffer: gzipSync(fileBuffer),
|
|
17
|
+
displayName: displayName.endsWith(".gz") ? displayName : `${displayName}.gz`,
|
|
18
|
+
mime: "application/gzip"
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
async function ensureLocalFileReadable(absolutePath) {
|
|
22
|
+
try {
|
|
23
|
+
await access(absolutePath);
|
|
24
|
+
} catch {
|
|
25
|
+
throw new Error(`\u627E\u4E0D\u5230\u8981\u4E0A\u4F20\u7684\u6587\u4EF6: ${absolutePath}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function runWithConcurrency(items, concurrency, worker) {
|
|
29
|
+
let nextIndex = 0;
|
|
30
|
+
const workerCount = Math.min(concurrency, items.length);
|
|
31
|
+
await Promise.all(
|
|
32
|
+
Array.from({ length: workerCount }, async () => {
|
|
33
|
+
while (nextIndex < items.length) {
|
|
34
|
+
const currentIndex = nextIndex++;
|
|
35
|
+
const item = items[currentIndex];
|
|
36
|
+
if (item === void 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
await worker(item);
|
|
40
|
+
}
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { contentTypeFromPath, ensureLocalFileReadable, prepareBufferForNotionUpload, runWithConcurrency };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Client } from '@notionhq/client';
|
|
2
|
+
export declare function uploadFileToNotion(client: Client, absolutePath: string): Promise<string>;
|
|
3
|
+
export declare function uploadImageToNotion(client: Client, absolutePath: string): Promise<string>;
|
|
4
|
+
export declare function uploadVideoToNotion(client: Client, absolutePath: string): Promise<string>;
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/helpers/upload/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAsF/C,wBAAsB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,mBAkB5E;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAa/F;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,mBAW7E"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { basename } from 'node:path';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { SINGLE_PART_MAX_BYTES, MULTIPART_CHUNK_BYTES, MULTIPART_UPLOAD_CONCURRENCY } from './config.js';
|
|
4
|
+
import { ensureLocalFileReadable, contentTypeFromPath, prepareBufferForNotionUpload, runWithConcurrency } from './helper.js';
|
|
5
|
+
|
|
6
|
+
async function completeFileUpload({
|
|
7
|
+
client,
|
|
8
|
+
displayName,
|
|
9
|
+
contentType,
|
|
10
|
+
fileBuffer
|
|
11
|
+
}) {
|
|
12
|
+
if (fileBuffer.length <= SINGLE_PART_MAX_BYTES) {
|
|
13
|
+
const data = new Blob([fileBuffer], { type: contentType });
|
|
14
|
+
const created2 = await client.fileUploads.create({
|
|
15
|
+
mode: "single_part",
|
|
16
|
+
filename: displayName,
|
|
17
|
+
content_type: contentType
|
|
18
|
+
});
|
|
19
|
+
const afterSend = await client.fileUploads.send({
|
|
20
|
+
file_upload_id: created2.id,
|
|
21
|
+
file: { filename: displayName, data }
|
|
22
|
+
});
|
|
23
|
+
if (afterSend.status !== "uploaded") {
|
|
24
|
+
throw new Error(`\u6587\u4EF6\u4E0A\u4F20\u672A\u5C31\u7EEA\uFF0C\u72B6\u6001: ${afterSend.status}`);
|
|
25
|
+
}
|
|
26
|
+
return created2.id;
|
|
27
|
+
}
|
|
28
|
+
const number_of_parts = Math.ceil(fileBuffer.length / MULTIPART_CHUNK_BYTES);
|
|
29
|
+
const created = await client.fileUploads.create({
|
|
30
|
+
mode: "multi_part",
|
|
31
|
+
filename: displayName,
|
|
32
|
+
content_type: contentType,
|
|
33
|
+
number_of_parts
|
|
34
|
+
});
|
|
35
|
+
await runWithConcurrency(
|
|
36
|
+
Array.from({ length: number_of_parts }, (_, index) => index + 1),
|
|
37
|
+
MULTIPART_UPLOAD_CONCURRENCY,
|
|
38
|
+
async (part) => {
|
|
39
|
+
const start = (part - 1) * MULTIPART_CHUNK_BYTES;
|
|
40
|
+
const end = Math.min(start + MULTIPART_CHUNK_BYTES, fileBuffer.length);
|
|
41
|
+
const slice = fileBuffer.subarray(start, end);
|
|
42
|
+
const data = new Blob([slice], { type: contentType });
|
|
43
|
+
await client.fileUploads.send({
|
|
44
|
+
file_upload_id: created.id,
|
|
45
|
+
part_number: String(part),
|
|
46
|
+
file: { filename: displayName, data }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
const afterComplete = await client.fileUploads.complete({
|
|
51
|
+
file_upload_id: created.id
|
|
52
|
+
});
|
|
53
|
+
if (afterComplete.status !== "uploaded") {
|
|
54
|
+
throw new Error(`\u591A\u6BB5\u6587\u4EF6\u4E0A\u4F20\u672A\u5B8C\u6210\uFF0C\u72B6\u6001: ${afterComplete.status}`);
|
|
55
|
+
}
|
|
56
|
+
return created.id;
|
|
57
|
+
}
|
|
58
|
+
async function uploadFileToNotion(client, absolutePath) {
|
|
59
|
+
const displayName = basename(absolutePath);
|
|
60
|
+
await ensureLocalFileReadable(absolutePath);
|
|
61
|
+
const mime = contentTypeFromPath(absolutePath);
|
|
62
|
+
const rawBuffer = await readFile(absolutePath);
|
|
63
|
+
const {
|
|
64
|
+
buffer,
|
|
65
|
+
displayName: uploadName,
|
|
66
|
+
mime: uploadMime
|
|
67
|
+
} = prepareBufferForNotionUpload(absolutePath, rawBuffer, displayName, mime);
|
|
68
|
+
return completeFileUpload({
|
|
69
|
+
client,
|
|
70
|
+
displayName: uploadName,
|
|
71
|
+
contentType: uploadMime,
|
|
72
|
+
fileBuffer: buffer
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
async function uploadImageToNotion(client, absolutePath) {
|
|
76
|
+
await ensureLocalFileReadable(absolutePath);
|
|
77
|
+
const displayName = basename(absolutePath);
|
|
78
|
+
const contentType = contentTypeFromPath(absolutePath);
|
|
79
|
+
if (!contentType.startsWith("image/")) {
|
|
80
|
+
throw new Error(`\u4E0D\u662F\u56FE\u7247 MIME\uFF08${contentType}\uFF09\uFF0C\u8BF7\u4F20\u56FE\u7247\u8DEF\u5F84\u6216\u663E\u5F0F\u6307\u5B9A contentType`);
|
|
81
|
+
}
|
|
82
|
+
const fileBuffer = await readFile(absolutePath);
|
|
83
|
+
return completeFileUpload({ client, displayName, contentType, fileBuffer });
|
|
84
|
+
}
|
|
85
|
+
async function uploadVideoToNotion(client, absolutePath) {
|
|
86
|
+
const displayName = basename(absolutePath);
|
|
87
|
+
await ensureLocalFileReadable(absolutePath);
|
|
88
|
+
const contentType = contentTypeFromPath(absolutePath);
|
|
89
|
+
if (!contentType.startsWith("video/")) {
|
|
90
|
+
throw new Error(`\u4E0D\u662F\u89C6\u9891 MIME\uFF08${contentType}\uFF09\uFF0C\u8BF7\u4F20\u89C6\u9891\u8DEF\u5F84\u6216\u663E\u5F0F\u6307\u5B9A contentType`);
|
|
91
|
+
}
|
|
92
|
+
const fileBuffer = await readFile(absolutePath);
|
|
93
|
+
return completeFileUpload({ client, displayName, contentType, fileBuffer });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { uploadFileToNotion, uploadImageToNotion, uploadVideoToNotion };
|
package/lib/index.d.ts
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
1
|
import { type NumberPropertyItemObjectResponse, type PageObjectResponse, type RichTextItemResponse } from '@notionhq/client';
|
|
2
|
-
import { newNotionClient } from './client';
|
|
3
|
-
export { uploadLocalFileToPageBody, uploadLocalImageToPageBody, uploadLocalVideoToPageBody, uploadLocalImageToNotion, } from './upload';
|
|
4
|
-
export { newNotionClient };
|
|
5
2
|
export type { NumberPropertyItemObjectResponse, PageObjectResponse, RichTextItemResponse };
|
|
6
|
-
export
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
* 从 API 返回的 page(或任意带 `id` 的对象)拼出可在浏览器打开的 Notion 页面地址
|
|
10
|
-
* @param pageId - 页面 ID
|
|
11
|
-
* @returns 可在浏览器打开的 Notion 页面地址
|
|
12
|
-
*/
|
|
13
|
-
export declare function buildNotionPageWebUrl(pageId: string): string;
|
|
14
|
-
/**
|
|
15
|
-
* 获取数据库页面的 ID
|
|
16
|
-
*/
|
|
17
|
-
export declare function getId(page: PageObjectResponse): number;
|
|
3
|
+
export { n } from './utils';
|
|
4
|
+
export { buildNotionPageWebUrl, getId } from './helpers/page';
|
|
5
|
+
export { NotionClient } from './notion-client';
|
|
18
6
|
//# sourceMappingURL=index.d.ts.map
|
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,
|
|
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,YAAY,EAAE,gCAAgC,EAAE,kBAAkB,EAAE,oBAAoB,EAAE,CAAC;AAE3F,OAAO,EAAE,CAAC,EAAE,MAAM,SAAS,CAAC;AAC5B,OAAO,EAAE,qBAAqB,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC"}
|
package/lib/index.js
CHANGED
|
@@ -1,28 +1,4 @@
|
|
|
1
1
|
import '@notionhq/client';
|
|
2
|
-
|
|
3
|
-
export {
|
|
4
|
-
|
|
5
|
-
function n(num) {
|
|
6
|
-
return num ?? null;
|
|
7
|
-
}
|
|
8
|
-
async function traverseDataSource(cb, dataSourceId) {
|
|
9
|
-
const data_source_id = dataSourceId ?? process.env.NOTION_DATA_SOURCE_ID ?? "";
|
|
10
|
-
const data = await newNotionClient().dataSources.query({ data_source_id });
|
|
11
|
-
data.results.forEach((item) => cb(item));
|
|
12
|
-
}
|
|
13
|
-
function buildNotionPageWebUrl(pageId) {
|
|
14
|
-
return `https://www.notion.so/${pageId.replace(/-/g, "")}`;
|
|
15
|
-
}
|
|
16
|
-
function getId(page) {
|
|
17
|
-
const idProperty = page.properties["ID"];
|
|
18
|
-
if (idProperty?.type !== "unique_id") {
|
|
19
|
-
throw new Error("ID \u5C5E\u6027\u4E0D\u662F unique_id \u7C7B\u578B");
|
|
20
|
-
}
|
|
21
|
-
const id = idProperty.unique_id.number;
|
|
22
|
-
if (id === null) {
|
|
23
|
-
throw new Error("ID \u5C5E\u6027\u4E3A\u7A7A");
|
|
24
|
-
}
|
|
25
|
-
return id;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export { buildNotionPageWebUrl, getId, n, newNotionClient, traverseDataSource };
|
|
2
|
+
export { n } from './utils.js';
|
|
3
|
+
export { buildNotionPageWebUrl, getId } from './helpers/page.js';
|
|
4
|
+
export { NotionClient } from './notion-client.js';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type PageObjectResponse } from '@notionhq/client';
|
|
2
|
+
export declare class NotionClient {
|
|
3
|
+
private client;
|
|
4
|
+
constructor(authToken?: string);
|
|
5
|
+
traverseDataSource(cb: (item: PageObjectResponse) => void, dataSourceId?: string): Promise<void>;
|
|
6
|
+
appendDivider(blockId: string): Promise<void>;
|
|
7
|
+
appendMarkdown(pageId: string, markdown: string): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* 将本地文件经 Notion File Upload API 上传后,作为「文件」块挂到页面正文中。
|
|
10
|
+
* @param pageId - 页面 ID
|
|
11
|
+
* @param absolutePath - 本地文件路径
|
|
12
|
+
*/
|
|
13
|
+
uploadFileToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* 上传本地图片到 Notion(数据库中附件属性中需要使用到这个函数)
|
|
16
|
+
* @param absolutePath - 本地图片路径
|
|
17
|
+
* @returns - 文件上传 ID
|
|
18
|
+
*/
|
|
19
|
+
uploadImageToNotion(absolutePath: string): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* 上传本地图片并在页面正文插入「图片」块(`image`)。
|
|
22
|
+
*/
|
|
23
|
+
uploadImageToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* 上传本地视频并在页面正文插入「视频」块(`video`)。
|
|
26
|
+
*/
|
|
27
|
+
uploadVideoToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=notion-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"notion-client.d.ts","sourceRoot":"","sources":["../src/notion-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAU,KAAK,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAKnE,qBAAa,YAAY;IACvB,OAAO,CAAC,MAAM,CAAS;gBAEX,SAAS,CAAC,EAAE,MAAM;IAQxB,kBAAkB,CAAC,EAAE,EAAE,CAAC,IAAI,EAAE,kBAAkB,KAAK,IAAI,EAAE,YAAY,CAAC,EAAE,MAAM;IAOhF,aAAa,CAAC,OAAO,EAAE,MAAM;IAO7B,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IAQrD;;;;OAIG;IACG,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;IAiB/D;;;;OAIG;IACG,mBAAmB,CAAC,YAAY,EAAE,MAAM;IAI9C;;OAEG;IACG,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;IAiBhE;;OAEG;IACG,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM;CAUjE"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Client } from '@notionhq/client';
|
|
2
|
+
import { customFetch } from './helpers/custom-fetch.js';
|
|
3
|
+
import { uploadFileToNotion, uploadImageToNotion, uploadVideoToNotion } from './helpers/upload/index.js';
|
|
4
|
+
|
|
5
|
+
class NotionClient {
|
|
6
|
+
client;
|
|
7
|
+
constructor(authToken) {
|
|
8
|
+
this.client = new Client({
|
|
9
|
+
auth: authToken ?? process.env.NOTION_AUTH_TOKEN,
|
|
10
|
+
fetch: customFetch,
|
|
11
|
+
timeoutMs: 1e3 * 60 * 10
|
|
12
|
+
// 10 分钟超时的时间
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async traverseDataSource(cb, dataSourceId) {
|
|
16
|
+
const data_source_id = dataSourceId ?? process.env.NOTION_DATA_SOURCE_ID ?? "";
|
|
17
|
+
const data = await this.client.dataSources.query({ data_source_id });
|
|
18
|
+
data.results.forEach((item) => cb(item));
|
|
19
|
+
}
|
|
20
|
+
async appendDivider(blockId) {
|
|
21
|
+
await this.client.blocks.children.append({
|
|
22
|
+
block_id: blockId,
|
|
23
|
+
children: [{ type: "divider", divider: {} }]
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
async appendMarkdown(pageId, markdown) {
|
|
27
|
+
await this.client.pages.updateMarkdown({
|
|
28
|
+
page_id: pageId,
|
|
29
|
+
type: "insert_content",
|
|
30
|
+
insert_content: { content: markdown }
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 将本地文件经 Notion File Upload API 上传后,作为「文件」块挂到页面正文中。
|
|
35
|
+
* @param pageId - 页面 ID
|
|
36
|
+
* @param absolutePath - 本地文件路径
|
|
37
|
+
*/
|
|
38
|
+
async uploadFileToPageBody(pageId, absolutePath) {
|
|
39
|
+
const fileUploadId = await uploadFileToNotion(this.client, absolutePath);
|
|
40
|
+
await this.client.blocks.children.append({
|
|
41
|
+
block_id: pageId,
|
|
42
|
+
children: [
|
|
43
|
+
{
|
|
44
|
+
type: "file",
|
|
45
|
+
file: {
|
|
46
|
+
type: "file_upload",
|
|
47
|
+
file_upload: { id: fileUploadId }
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* 上传本地图片到 Notion(数据库中附件属性中需要使用到这个函数)
|
|
55
|
+
* @param absolutePath - 本地图片路径
|
|
56
|
+
* @returns - 文件上传 ID
|
|
57
|
+
*/
|
|
58
|
+
async uploadImageToNotion(absolutePath) {
|
|
59
|
+
return uploadImageToNotion(this.client, absolutePath);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* 上传本地图片并在页面正文插入「图片」块(`image`)。
|
|
63
|
+
*/
|
|
64
|
+
async uploadImageToPageBody(pageId, absolutePath) {
|
|
65
|
+
const fileUploadId = await uploadImageToNotion(this.client, absolutePath);
|
|
66
|
+
await this.client.blocks.children.append({
|
|
67
|
+
block_id: pageId,
|
|
68
|
+
children: [
|
|
69
|
+
{
|
|
70
|
+
type: "image",
|
|
71
|
+
image: {
|
|
72
|
+
type: "file_upload",
|
|
73
|
+
file_upload: { id: fileUploadId }
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
]
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* 上传本地视频并在页面正文插入「视频」块(`video`)。
|
|
81
|
+
*/
|
|
82
|
+
async uploadVideoToPageBody(pageId, absolutePath) {
|
|
83
|
+
const fileUploadId = await uploadVideoToNotion(this.client, absolutePath);
|
|
84
|
+
await this.client.blocks.children.append({
|
|
85
|
+
block_id: pageId,
|
|
86
|
+
children: [
|
|
87
|
+
{ type: "video", video: { type: "file_upload", file_upload: { id: fileUploadId } } }
|
|
88
|
+
]
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { NotionClient };
|
package/lib/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,wBAAgB,CAAC,CAAC,GAAG,CAAC,EAAE,MAAM,iBAE7B"}
|
package/lib/utils.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@secret-momo/notion",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"files": [
|
|
5
5
|
"lib"
|
|
6
6
|
],
|
|
@@ -19,30 +19,31 @@
|
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"postinstall": "bun outdated",
|
|
22
|
-
"prepublishOnly": "bun run build",
|
|
22
|
+
"prepublishOnly": "bun run typecheck && bun run build",
|
|
23
|
+
"typecheck": "tsc --noEmit",
|
|
23
24
|
"build": "rm -rf lib && rollup -c && tsc -p tsconfig.json"
|
|
24
25
|
},
|
|
25
26
|
"dependencies": {
|
|
26
|
-
"@notionhq/client": "5.
|
|
27
|
-
"@secret-momo/utils": "1.
|
|
27
|
+
"@notionhq/client": "5.23.2",
|
|
28
|
+
"@secret-momo/utils": "1.2.0"
|
|
28
29
|
},
|
|
29
30
|
"devDependencies": {
|
|
30
31
|
"@eslint/config-helpers": "0.6.0",
|
|
31
32
|
"@eslint/js": "10.0.1",
|
|
32
33
|
"@types/bun": "1.3.14",
|
|
33
|
-
"eslint": "10.
|
|
34
|
-
"eslint-plugin-import-x": "4.17.
|
|
34
|
+
"eslint": "10.7.0",
|
|
35
|
+
"eslint-plugin-import-x": "4.17.1",
|
|
35
36
|
"eslint-plugin-react": "7.37.5",
|
|
36
37
|
"eslint-plugin-react-hooks": "7.1.1",
|
|
37
38
|
"eslint-plugin-unused-imports": "4.4.1",
|
|
38
39
|
"husky": "9.1.7",
|
|
39
40
|
"jiti": "2.7.0",
|
|
40
41
|
"lint-staged": "17.0.8",
|
|
41
|
-
"prettier": "3.9.
|
|
42
|
+
"prettier": "3.9.5",
|
|
42
43
|
"rollup": "4.62.2",
|
|
43
44
|
"rollup-plugin-esbuild": "6.2.1",
|
|
44
45
|
"typescript": "6.0.3",
|
|
45
|
-
"typescript-eslint": "8.
|
|
46
|
+
"typescript-eslint": "8.64.0"
|
|
46
47
|
},
|
|
47
48
|
"lint-staged": {
|
|
48
49
|
"*.{ts,tsx,js,jsx}": [
|
package/lib/client.d.ts
DELETED
package/lib/client.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAoD1C,wBAAgB,eAAe,CAAC,SAAS,CAAC,EAAE,MAAM,UAQjD"}
|
package/lib/upload.d.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 将本地文件经 Notion File Upload API 上传后,作为「文件」块挂到页面正文中。
|
|
3
|
-
* @param pageId - 页面 ID
|
|
4
|
-
* @param absolutePath - 本地文件路径
|
|
5
|
-
* @param contentType - 可选;默认按扩展名推断,未知则为 application/octet-stream
|
|
6
|
-
*/
|
|
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
|
-
/**
|
|
15
|
-
* 上传本地图片并在页面正文插入「图片」块(`image`)。
|
|
16
|
-
* @param contentType - 可选;默认按扩展名推断,须为 image/*
|
|
17
|
-
*/
|
|
18
|
-
export declare function uploadLocalImageToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* 上传本地视频并在页面正文插入「视频」块(`video`)。
|
|
21
|
-
* @param contentType - 可选;默认按扩展名推断,须为 video/*
|
|
22
|
-
*/
|
|
23
|
-
export declare function uploadLocalVideoToPageBody(pageId: string, absolutePath: string): Promise<void>;
|
|
24
|
-
//# sourceMappingURL=upload.d.ts.map
|
package/lib/upload.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { basename, extname } from 'node:path';
|
|
2
|
-
import { readFile, access } from 'node:fs/promises';
|
|
3
|
-
import { gzipSync } from 'node:zlib';
|
|
4
|
-
import '@notionhq/client';
|
|
5
|
-
import { newNotionClient } from './client.js';
|
|
6
|
-
|
|
7
|
-
const SINGLE_PART_MAX_BYTES = 20 * 1024 * 1024;
|
|
8
|
-
const MULTIPART_CHUNK_BYTES = 16 * 1024 * 1024;
|
|
9
|
-
const MULTIPART_UPLOAD_CONCURRENCY = 2;
|
|
10
|
-
const MIME_BY_EXT = {
|
|
11
|
-
".avif": "image/avif",
|
|
12
|
-
".bmp": "image/bmp",
|
|
13
|
-
".gif": "image/gif",
|
|
14
|
-
".heic": "image/heic",
|
|
15
|
-
".heif": "image/heif",
|
|
16
|
-
".ico": "image/x-icon",
|
|
17
|
-
".jpeg": "image/jpeg",
|
|
18
|
-
".jpg": "image/jpeg",
|
|
19
|
-
".png": "image/png",
|
|
20
|
-
".svg": "image/svg+xml",
|
|
21
|
-
".webp": "image/webp",
|
|
22
|
-
".html": "text/html",
|
|
23
|
-
".htm": "text/html",
|
|
24
|
-
".mp4": "video/mp4",
|
|
25
|
-
".webm": "video/webm",
|
|
26
|
-
".mov": "video/quicktime",
|
|
27
|
-
".m4v": "video/x-m4v",
|
|
28
|
-
".mkv": "video/x-matroska",
|
|
29
|
-
".mpeg": "video/mpeg",
|
|
30
|
-
".mpg": "video/mpeg",
|
|
31
|
-
".avi": "video/x-msvideo",
|
|
32
|
-
".pdf": "application/pdf",
|
|
33
|
-
".zip": "application/zip",
|
|
34
|
-
".json": "application/json",
|
|
35
|
-
".txt": "text/plain"
|
|
36
|
-
};
|
|
37
|
-
function contentTypeFromPath(absolutePath) {
|
|
38
|
-
const ext = extname(absolutePath).toLowerCase();
|
|
39
|
-
return MIME_BY_EXT[ext] ?? "application/octet-stream";
|
|
40
|
-
}
|
|
41
|
-
function prepareBufferForNotionUpload(absolutePath, fileBuffer, displayName, mime) {
|
|
42
|
-
const ext = extname(absolutePath).toLowerCase();
|
|
43
|
-
if (ext !== ".html") {
|
|
44
|
-
return { buffer: fileBuffer, displayName, mime };
|
|
45
|
-
}
|
|
46
|
-
return {
|
|
47
|
-
buffer: gzipSync(fileBuffer),
|
|
48
|
-
displayName: displayName.endsWith(".gz") ? displayName : `${displayName}.gz`,
|
|
49
|
-
mime: "application/gzip"
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
async function ensureLocalFileReadable(absolutePath) {
|
|
53
|
-
try {
|
|
54
|
-
await access(absolutePath);
|
|
55
|
-
} catch {
|
|
56
|
-
throw new Error(`\u627E\u4E0D\u5230\u8981\u4E0A\u4F20\u7684\u6587\u4EF6: ${absolutePath}`);
|
|
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
|
-
}
|
|
75
|
-
async function completeFileUpload(displayName, contentType, fileBuffer) {
|
|
76
|
-
const notion = newNotionClient();
|
|
77
|
-
if (fileBuffer.length <= SINGLE_PART_MAX_BYTES) {
|
|
78
|
-
const data = new Blob([fileBuffer], { type: contentType });
|
|
79
|
-
const created2 = await notion.fileUploads.create({
|
|
80
|
-
mode: "single_part",
|
|
81
|
-
filename: displayName,
|
|
82
|
-
content_type: contentType
|
|
83
|
-
});
|
|
84
|
-
const afterSend = await notion.fileUploads.send({
|
|
85
|
-
file_upload_id: created2.id,
|
|
86
|
-
file: { filename: displayName, data }
|
|
87
|
-
});
|
|
88
|
-
if (afterSend.status !== "uploaded") {
|
|
89
|
-
throw new Error(`\u6587\u4EF6\u4E0A\u4F20\u672A\u5C31\u7EEA\uFF0C\u72B6\u6001: ${afterSend.status}`);
|
|
90
|
-
}
|
|
91
|
-
return created2.id;
|
|
92
|
-
}
|
|
93
|
-
const number_of_parts = Math.ceil(fileBuffer.length / MULTIPART_CHUNK_BYTES);
|
|
94
|
-
const created = await notion.fileUploads.create({
|
|
95
|
-
mode: "multi_part",
|
|
96
|
-
filename: displayName,
|
|
97
|
-
content_type: contentType,
|
|
98
|
-
number_of_parts
|
|
99
|
-
});
|
|
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
|
-
);
|
|
115
|
-
const afterComplete = await notion.fileUploads.complete({
|
|
116
|
-
file_upload_id: created.id
|
|
117
|
-
});
|
|
118
|
-
if (afterComplete.status !== "uploaded") {
|
|
119
|
-
throw new Error(`\u591A\u6BB5\u6587\u4EF6\u4E0A\u4F20\u672A\u5B8C\u6210\uFF0C\u72B6\u6001: ${afterComplete.status}`);
|
|
120
|
-
}
|
|
121
|
-
return created.id;
|
|
122
|
-
}
|
|
123
|
-
async function appendBlocksToPage(pageId, children) {
|
|
124
|
-
await newNotionClient().blocks.children.append({ block_id: pageId, children });
|
|
125
|
-
}
|
|
126
|
-
async function uploadLocalFileToPageBody(pageId, absolutePath) {
|
|
127
|
-
const displayName = basename(absolutePath);
|
|
128
|
-
await ensureLocalFileReadable(absolutePath);
|
|
129
|
-
const mime = contentTypeFromPath(absolutePath);
|
|
130
|
-
const rawBuffer = await readFile(absolutePath);
|
|
131
|
-
const {
|
|
132
|
-
buffer,
|
|
133
|
-
displayName: uploadName,
|
|
134
|
-
mime: uploadMime
|
|
135
|
-
} = prepareBufferForNotionUpload(absolutePath, rawBuffer, displayName, mime);
|
|
136
|
-
const fileUploadId = await completeFileUpload(uploadName, uploadMime, buffer);
|
|
137
|
-
await appendBlocksToPage(pageId, [
|
|
138
|
-
{
|
|
139
|
-
type: "file",
|
|
140
|
-
file: {
|
|
141
|
-
type: "file_upload",
|
|
142
|
-
file_upload: { id: fileUploadId },
|
|
143
|
-
name: uploadName
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
]);
|
|
147
|
-
}
|
|
148
|
-
async function uploadLocalImageToNotion(absolutePath) {
|
|
149
|
-
const displayName = basename(absolutePath);
|
|
150
|
-
await ensureLocalFileReadable(absolutePath);
|
|
151
|
-
const mime = contentTypeFromPath(absolutePath);
|
|
152
|
-
if (!mime.startsWith("image/")) {
|
|
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`);
|
|
154
|
-
}
|
|
155
|
-
const fileBuffer = await readFile(absolutePath);
|
|
156
|
-
return completeFileUpload(displayName, mime, fileBuffer);
|
|
157
|
-
}
|
|
158
|
-
async function uploadLocalImageToPageBody(pageId, absolutePath) {
|
|
159
|
-
const fileUploadId = await uploadLocalImageToNotion(absolutePath);
|
|
160
|
-
await appendBlocksToPage(pageId, [
|
|
161
|
-
{
|
|
162
|
-
type: "image",
|
|
163
|
-
image: {
|
|
164
|
-
type: "file_upload",
|
|
165
|
-
file_upload: { id: fileUploadId }
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
]);
|
|
169
|
-
}
|
|
170
|
-
async function uploadLocalVideoToPageBody(pageId, absolutePath) {
|
|
171
|
-
const displayName = basename(absolutePath);
|
|
172
|
-
await ensureLocalFileReadable(absolutePath);
|
|
173
|
-
const mime = contentTypeFromPath(absolutePath);
|
|
174
|
-
if (!mime.startsWith("video/")) {
|
|
175
|
-
throw new Error(`\u4E0D\u662F\u89C6\u9891 MIME\uFF08${mime}\uFF09\uFF0C\u8BF7\u4F20\u89C6\u9891\u8DEF\u5F84\u6216\u663E\u5F0F\u6307\u5B9A contentType`);
|
|
176
|
-
}
|
|
177
|
-
const fileBuffer = await readFile(absolutePath);
|
|
178
|
-
const fileUploadId = await completeFileUpload(displayName, mime, fileBuffer);
|
|
179
|
-
await appendBlocksToPage(pageId, [
|
|
180
|
-
{
|
|
181
|
-
type: "video",
|
|
182
|
-
video: {
|
|
183
|
-
type: "file_upload",
|
|
184
|
-
file_upload: { id: fileUploadId }
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
]);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
export { uploadLocalFileToPageBody, uploadLocalImageToNotion, uploadLocalImageToPageBody, uploadLocalVideoToPageBody };
|