|
1 | | -const axios = require('axios'); |
2 | | -const FormData = require('form-data'); |
3 | 1 | const fs = require('fs'); |
4 | 2 | const path = require('path'); |
5 | 3 | const splitFile = require('split-file'); |
@@ -118,45 +116,35 @@ async function getFileSize(filePath) { |
118 | 116 | return stats.size; |
119 | 117 | } |
120 | 118 |
|
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 | + }; |
124 | 127 |
|
125 | 128 | if (partNumber) { |
126 | 129 | 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(); |
128 | 132 | } |
129 | 133 |
|
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); |
142 | 135 | } |
143 | 136 |
|
144 | | -async function completeMultiPartUpload(fileId) { |
| 137 | +async function completeMultiPartUpload(file) { |
145 | 138 | console.log('completing upload'); |
146 | 139 |
|
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, |
151 | 142 | }); |
152 | | - |
153 | | - return response.data; |
154 | 143 | } |
155 | 144 |
|
156 | 145 | async function uploadFile(filePath, fileName = path.basename(filePath)) { |
157 | 146 | const fileSize = await getFileSize(filePath); |
158 | 147 | const needsMultiPart = fileSize > SINGLE_PART_LIMIT; |
159 | | - |
160 | 148 | const contentType = getContentType(fileName); |
161 | 149 |
|
162 | 150 | if (!contentType) { |
@@ -184,17 +172,19 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) { |
184 | 172 | }); |
185 | 173 |
|
186 | 174 | 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); |
189 | 178 | } |
190 | 179 |
|
191 | 180 | // Complete the upload |
192 | | - upload = await completeMultiPartUpload(file.id); |
| 181 | + upload = await completeMultiPartUpload(file); |
193 | 182 | } else { |
194 | 183 | // 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 }); |
196 | 186 | file = await createFileUpload(); |
197 | | - upload = await uploadPart(file.id, fileStream); |
| 187 | + upload = await uploadPart(file, blob); |
198 | 188 | } |
199 | 189 |
|
200 | 190 | return { file, upload }; |
|
0 commit comments