1- const axios = require ( 'axios' ) ;
2- const FormData = require ( 'form-data' ) ;
31const fs = require ( 'fs' ) ;
42const path = require ( 'path' ) ;
53const splitFile = require ( 'split-file' ) ;
@@ -25,14 +23,7 @@ const JSON_HEADERS = {
2523
2624async 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
163145async 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 } ;
0 commit comments