Skip to content

Commit 44afcfb

Browse files
committed
Use blobs over streams
Also removes form-data dep as it’s no longer necessary
1 parent 3dd1ca9 commit 44afcfb

File tree

3 files changed

+21
-33
lines changed

3 files changed

+21
-33
lines changed

examples/shared/files.js

Lines changed: 21 additions & 31 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');
@@ -118,45 +116,35 @@ async function getFileSize(filePath) {
118116
return stats.size;
119117
}
120118

121-
async function uploadPart(fileId, partBuffer, partNumber = null) {
122-
const formData = new FormData();
123-
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+
};
124127

125128
if (partNumber) {
126129
console.log('uploading part', partNumber);
127-
formData.append('part_number', partNumber.toString());
130+
// Minor issue with the API, part_number must be a string
131+
params.part_number = partNumber.toString();
128132
}
129133

130-
const response = await axios({
131-
method: 'POST',
132-
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/send`,
133-
data: formData,
134-
headers: {
135-
...NOTION_HEADERS,
136-
'Content-Type': 'multipart/form-data',
137-
},
138-
...(!partNumber && { maxContentLength: SINGLE_PART_LIMIT }),
139-
});
140-
141-
return response.data;
134+
return await notion.fileUploads.send(params);
142135
}
143136

144-
async function completeMultiPartUpload(fileId) {
137+
async function completeMultiPartUpload(file) {
145138
console.log('completing upload');
146139

147-
const response = await axios({
148-
method: 'POST',
149-
url: `${NOTION_FILE_UPLOAD_URL}/${fileId}/complete`,
150-
headers: JSON_HEADERS,
140+
return await notion.fileUploads.complete({
141+
file_upload_id: file.id,
151142
});
152-
153-
return response.data;
154143
}
155144

156145
async function uploadFile(filePath, fileName = path.basename(filePath)) {
157146
const fileSize = await getFileSize(filePath);
158147
const needsMultiPart = fileSize > SINGLE_PART_LIMIT;
159-
160148
const contentType = getContentType(fileName);
161149

162150
if (!contentType) {
@@ -184,17 +172,19 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
184172
});
185173

186174
for (let i = 1; i <= parts.length; i++) {
187-
const fileStream = fs.createReadStream(parts[i - 1]);
188-
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);
189178
}
190179

191180
// Complete the upload
192-
upload = await completeMultiPartUpload(file.id);
181+
upload = await completeMultiPartUpload(file);
193182
} else {
194183
// Single-part upload
195-
const fileStream = fs.createReadStream(filePath);
184+
const buffer = await fs.promises.readFile(filePath);
185+
const blob = new Blob([buffer], { type: contentType });
196186
file = await createFileUpload();
197-
upload = await uploadPart(file.id, fileStream);
187+
upload = await uploadPart(file, blob);
198188
}
199189

200190
return { file, upload };

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
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)