11const FormData = require ( "form-data" ) ;
22const path = require ( "path" ) ;
33const basePath = process . cwd ( ) ;
4- const fs = require ( "fs" ) ;
4+ const fs = require ( "graceful- fs" ) ;
55
6- const { fetchWithRetry } = require ( `${ basePath } /utils/functions/fetchWithRetry.js` ) ;
6+ const { fetchNoRetry } = require ( `${ basePath } /utils/functions/fetchWithRetry.js` ) ;
77
88const { GENERIC } = require ( `${ basePath } /src/config.js` ) ;
99
@@ -16,29 +16,37 @@ if (!fs.existsSync(path.join(`${basePath}/build`, "/ipfsMetas"))) {
1616let readDir = `${ basePath } /build/json` ;
1717let writeDir = `${ basePath } /build/ipfsMetas` ;
1818
19- async function main ( ) {
20- console . log ( `Starting upload of metadata...` ) ;
19+ function getFileStreamForJSONFiles ( ) {
20+ const jsonArray = [ ] ;
2121 const files = fs . readdirSync ( readDir ) ;
2222 files . sort ( function ( a , b ) {
2323 return a . split ( "." ) [ 0 ] - b . split ( "." ) [ 0 ] ;
2424 } ) ;
25- const formData = new FormData ( ) ;
26-
27- for ( const file of files ) {
28- if ( regex . test ( file ) ) {
29- const fileStream = fs . createReadStream ( `${ readDir } /${ file } ` ) ;
30- formData . append ( "metadata_files" , fileStream ) ;
31- }
32- }
25+ files . forEach ( ( file ) => {
26+ if ( ! regex . test ( file ) ) return ;
27+ const fileData = fs . createReadStream ( path . join ( readDir , file ) ) ;
28+ jsonArray . push ( fileData ) ;
29+ } ) ;
30+ return jsonArray ;
31+ }
3332
33+ async function main ( ) {
34+ console . log ( `Starting upload of metadata...` ) ;
3435 try {
36+ const metadataFileStreams = getFileStreamForJSONFiles ( ) ;
37+ const formData = new FormData ( ) ;
38+ metadataFileStreams . forEach ( ( file ) => {
39+ formData . append ( "metadata_files" , file ) ;
40+ } ) ;
41+
3542 const url = "https://api.nftport.xyz/v0/metadata/directory" ;
3643 const options = {
3744 method : "POST" ,
3845 headers : { } ,
3946 body : formData ,
4047 } ;
41- const response = await fetchWithRetry ( url , options ) ;
48+ const response = await fetchNoRetry ( url , options ) ;
49+
4250 fs . writeFileSync (
4351 `${ writeDir } /_ipfsMetasResponse.json` ,
4452 JSON . stringify ( response , null , 2 )
@@ -81,7 +89,7 @@ async function main() {
8189 } ,
8290 body : JSON . stringify ( genericObject ) ,
8391 } ;
84- const response = await fetchWithRetry ( url , options ) ;
92+ const response = await fetchNoRetry ( url , options ) ;
8593 fs . writeFileSync ( uploadedMeta , JSON . stringify ( response , null , 2 ) ) ;
8694 console . log ( `Generic metadata uploaded!` ) ;
8795 } catch ( err ) {
0 commit comments