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' ) ;
@@ -25,14 +23,7 @@ const JSON_HEADERS = {
25
23
26
24
async function createFileUpload ( options = { mode : 'single_part' } ) {
27
25
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 ) ;
36
27
} catch ( error ) {
37
28
console . error ( 'Error creating file upload:' , error ) ;
38
29
throw error ;
@@ -125,45 +116,35 @@ async function getFileSize(filePath) {
125
116
return stats . size ;
126
117
}
127
118
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
+ } ;
131
127
132
128
if ( partNumber ) {
133
129
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 ( ) ;
135
132
}
136
133
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 ) ;
149
135
}
150
136
151
- async function completeMultiPartUpload ( fileId ) {
137
+ async function completeMultiPartUpload ( file ) {
152
138
console . log ( 'completing upload' ) ;
153
139
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 ,
158
142
} ) ;
159
-
160
- return response . data ;
161
143
}
162
144
163
145
async function uploadFile ( filePath , fileName = path . basename ( filePath ) ) {
164
146
const fileSize = await getFileSize ( filePath ) ;
165
147
const needsMultiPart = fileSize > SINGLE_PART_LIMIT ;
166
-
167
148
const contentType = getContentType ( fileName ) ;
168
149
169
150
if ( ! contentType ) {
@@ -191,17 +172,19 @@ async function uploadFile(filePath, fileName = path.basename(filePath)) {
191
172
} ) ;
192
173
193
174
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 ) ;
196
178
}
197
179
198
180
// Complete the upload
199
- upload = await completeMultiPartUpload ( file . id ) ;
181
+ upload = await completeMultiPartUpload ( file ) ;
200
182
} else {
201
183
// 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 } ) ;
203
186
file = await createFileUpload ( ) ;
204
- upload = await uploadPart ( file . id , fileStream ) ;
187
+ upload = await uploadPart ( file , blob ) ;
205
188
}
206
189
207
190
return { file, upload } ;
0 commit comments