Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 22 additions & 39 deletions examples/shared/files.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const axios = require('axios');
const FormData = require('form-data');
const fs = require('fs');
const path = require('path');
const splitFile = require('split-file');
Expand All @@ -25,14 +23,7 @@ const JSON_HEADERS = {

async function createFileUpload(options = { mode: 'single_part' }) {
try {
const response = await axios({
method: 'POST',
url: NOTION_FILE_UPLOAD_URL,
headers: JSON_HEADERS,
data: options,
});

return response.data;
return await notion.fileUploads.create(options);
} catch (error) {
console.error('Error creating file upload:', error);
throw error;
Expand Down Expand Up @@ -125,45 +116,35 @@ async function getFileSize(filePath) {
return stats.size;
}

async function uploadPart(fileId, partBuffer, partNumber = null) {
const formData = new FormData();
formData.append('file', partBuffer);
async function uploadPart(file, blob, partNumber = null) {
const params = {
file_upload_id: file.id,
file: {
data: blob,
filename: file.filename,
},
};

if (partNumber) {
console.log('uploading part', partNumber);
formData.append('part_number', partNumber.toString());
// Minor issue with the API, part_number must be a string
params.part_number = partNumber.toString();
}

const response = await axios({
method: 'POST',
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/send`,
data: formData,
headers: {
...NOTION_HEADERS,
'Content-Type': 'multipart/form-data',
},
...(!partNumber && { maxContentLength: SINGLE_PART_LIMIT }),
});

return response.data;
return await notion.fileUploads.send(params);
}

async function completeMultiPartUpload(fileId) {
async function completeMultiPartUpload(file) {
console.log('completing upload');

const response = await axios({
method: 'POST',
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/complete`,
headers: JSON_HEADERS,
return await notion.fileUploads.complete({
file_upload_id: file.id,
});

return response.data;
}

async function uploadFile(filePath, fileName = path.basename(filePath)) {
const fileSize = await getFileSize(filePath);
const needsMultiPart = fileSize > SINGLE_PART_LIMIT;

const contentType = getContentType(fileName);

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

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

// Complete the upload
upload = await completeMultiPartUpload(file.id);
upload = await completeMultiPartUpload(file);
} else {
// Single-part upload
const fileStream = fs.createReadStream(filePath);
const buffer = await fs.promises.readFile(filePath);
const blob = new Blob([buffer], { type: contentType });
file = await createFileUpload();
upload = await uploadPart(file.id, fileStream);
upload = await uploadPart(file, blob);
}

return { file, upload };
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
"author": "Benjamin Borowski <ben@weareokidoki.com>",
"license": "MIT",
"dependencies": {
"@notionhq/client": "^3.0.1",
"@notionhq/client": "^3.1.1",
"@notionhq/notion-mcp-server": "^1.8.0",
"async-sema": "^3.1.1",
"axios": "^1.4.0",
"date-fns": "^2.28.0",
"date-fns-tz": "^1.3.5",
"dotenv": "^16.3.1",
"form-data": "^4.0.2",
"lodash": "^4.17.21",
"split-file": "^2.3.0",
"yargs": "^17.5.1"
Expand Down