Skip to content

Commit 77a6d4b

Browse files
authored
Merge pull request #8 from typeoneerror/notion-3.1.x
Notion 3.1.x
2 parents 36e137b + 44afcfb commit 77a6d4b

File tree

3 files changed

+31
-49
lines changed

3 files changed

+31
-49
lines changed

examples/shared/files.js

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const axios = require('axios');
2-
const FormData = require('form-data');
31
const fs = require('fs');
42
const path = require('path');
53
const splitFile = require('split-file');
@@ -25,14 +23,7 @@ const JSON_HEADERS = {
2523

2624
async function createFileUpload(options = { mode: 'single_part' }) {
2725
try {
28-
const response = await axios({
29-
method: 'POST',
30-
url: NOTION_FILE_UPLOAD_URL,
31-
headers: JSON_HEADERS,
32-
data: options,
33-
});
34-
35-
return response.data;
26+
return await notion.fileUploads.create(options);
3627
} catch (error) {
3728
console.error('Error creating file upload:', error);
3829
throw error;
@@ -125,45 +116,35 @@ async function getFileSize(filePath) {
125116
return stats.size;
126117
}
127118

128-
async function uploadPart(fileId, partBuffer, partNumber = null) {
129-
const formData = new FormData();
130-
formData.append('file', partBuffer);
119+
async function uploadPart(file, blob, partNumber = null) {
120+
const params = {
121+
file_upload_id: file.id,
122+
file: {
123+
data: blob,
124+
filename: file.filename,
125+
},
126+
};
131127

132128
if (partNumber) {
133129
console.log('uploading part', partNumber);
134-
formData.append('part_number', partNumber.toString());
130+
// Minor issue with the API, part_number must be a string
131+
params.part_number = partNumber.toString();
135132
}
136133

137-
const response = await axios({
138-
method: 'POST',
139-
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/send`,
140-
data: formData,
141-
headers: {
142-
...NOTION_HEADERS,
143-
'Content-Type': 'multipart/form-data',
144-
},
145-
...(!partNumber && { maxContentLength: SINGLE_PART_LIMIT }),
146-
});
147-
148-
return response.data;
134+
return await notion.fileUploads.send(params);
149135
}
150136

151-
async function completeMultiPartUpload(fileId) {
137+
async function completeMultiPartUpload(file) {
152138
console.log('completing upload');
153139

154-
const response = await axios({
155-
method: 'POST',
156-
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/complete`,
157-
headers: JSON_HEADERS,
140+
return await notion.fileUploads.complete({
141+
file_upload_id: file.id,
158142
});
159-
160-
return response.data;
161143
}
162144

163145
async function uploadFile(filePath, fileName = path.basename(filePath)) {
164146
const fileSize = await getFileSize(filePath);
165147
const needsMultiPart = fileSize > SINGLE_PART_LIMIT;
166-
167148
const contentType = getContentType(fileName);
168149

169150
if (!contentType) {
@@ -191,17 +172,19 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
191172
});
192173

193174
for (let i = 1; i <= parts.length; i++) {
194-
const fileStream = fs.createReadStream(parts[i - 1]);
195-
upload = await uploadPart(file.id, fileStream, i);
175+
const buffer = await fs.promises.readFile(parts[i - 1]);
176+
const blob = new Blob([buffer], { type: contentType });
177+
upload = await uploadPart(file, blob, i);
196178
}
197179

198180
// Complete the upload
199-
upload = await completeMultiPartUpload(file.id);
181+
upload = await completeMultiPartUpload(file);
200182
} else {
201183
// Single-part upload
202-
const fileStream = fs.createReadStream(filePath);
184+
const buffer = await fs.promises.readFile(filePath);
185+
const blob = new Blob([buffer], { type: contentType });
203186
file = await createFileUpload();
204-
upload = await uploadPart(file.id, fileStream);
187+
upload = await uploadPart(file, blob);
205188
}
206189

207190
return { file, upload };

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
"author": "Benjamin Borowski <[email protected]>",
77
"license": "MIT",
88
"dependencies": {
9-
"@notionhq/client": "^3.0.1",
9+
"@notionhq/client": "^3.1.1",
1010
"@notionhq/notion-mcp-server": "^1.8.0",
1111
"async-sema": "^3.1.1",
1212
"axios": "^1.4.0",
1313
"date-fns": "^2.28.0",
1414
"date-fns-tz": "^1.3.5",
1515
"dotenv": "^16.3.1",
16-
"form-data": "^4.0.2",
1716
"lodash": "^4.17.21",
1817
"split-file": "^2.3.0",
1918
"yargs": "^17.5.1"

0 commit comments

Comments
 (0)