@@ -237,9 +237,17 @@ async function startingDirAssests(destinationStackId: string) {
237237 }
238238}
239239
240- async function saveAsset ( assets : any , retryCount : number , affix : string , destinationStackId : string , projectId : string ) {
240+ function toCheckUrl ( url : string , baseSiteUrl : string ) {
241+
242+ const validPattern = / ^ ( h t t p s ? : \/ \/ | w w w \. ) / ;
243+ return validPattern . test ( url )
244+ ? url
245+ : `${ baseSiteUrl } ${ url . replace ( / ^ \/ + / , "" ) } ` ;
246+ }
247+
248+ async function saveAsset ( assets : any , retryCount : number , affix : string , destinationStackId : string , projectId : string , baseSiteUrl :string ) {
241249 const srcFunc = 'saveAsset' ;
242- const url = encodeURI ( assets [ "wp:attachment_url" ] ) ;
250+ const url = encodeURI ( toCheckUrl ( assets [ "wp:attachment_url" ] , baseSiteUrl ) ) ;
243251 const name = url . split ( "/" ) . pop ( ) || "" ;
244252
245253 let description =
@@ -355,7 +363,7 @@ async function saveAsset(assets: any, retryCount: number, affix: string, destina
355363 await writeFileAsync ( failedJSONFilePath , failedJSON , 4 ) ;
356364
357365 if ( retryCount === 0 ) {
358- return saveAsset ( assets , 1 , affix , destinationStackId , projectId ) ;
366+ return saveAsset ( assets , 1 , affix , destinationStackId , projectId , baseSiteUrl ) ;
359367 } else {
360368 const message = getLogMessage (
361369 srcFunc ,
@@ -369,7 +377,7 @@ async function saveAsset(assets: any, retryCount: number, affix: string, destina
369377 }
370378}
371379
372- async function getAsset ( attachments : any [ ] , affix : string , destinationStackId : string , projectId : string ) {
380+ async function getAsset ( attachments : any [ ] , affix : string , destinationStackId : string , projectId : string , baseSiteUrl : string ) {
373381 const BATCH_SIZE = 5 ; // 5 promises at a time
374382 const results = [ ] ;
375383
@@ -378,7 +386,7 @@ async function getAsset(attachments: any[], affix: string, destinationStackId: s
378386
379387 const batchResults = await Promise . allSettled (
380388 batch . map ( ( data ) => {
381- saveAsset ( data , 0 , affix , destinationStackId , projectId )
389+ saveAsset ( data , 0 , affix , destinationStackId , projectId , baseSiteUrl )
382390 } )
383391 ) ;
384392 results . push ( ...batchResults ) ;
@@ -402,6 +410,9 @@ async function getAllAssets(
402410 await startingDirAssests ( destinationStackId ) ;
403411 const alldata : any = await fs . promises . readFile ( packagePath , "utf8" ) ;
404412 const alldataParsed = JSON . parse ( alldata ) ;
413+ const baseSiteUrl =
414+ alldataParsed ?. rss ?. channel ?. [ "wp:base_site_url" ] ||
415+ alldataParsed ?. channel ?. [ "wp:base_site_url" ] ;
405416 const assets : Asset [ ] =
406417 alldataParsed ?. rss ?. channel ?. item ?? alldataParsed ?. channel ?. item ;
407418 // await writeFileAsync(path.join(assetsSave, MIGRATION_DATA_CONFIG.ASSETS_FILE_NAME), assets, 4);
@@ -420,7 +431,7 @@ async function getAllAssets(
420431 ( { "wp:post_type" : postType } ) => postType === "attachment"
421432 ) ;
422433 if ( attachments . length > 0 ) {
423- await getAsset ( attachments , affix , destinationStackId , projectId ) ;
434+ await getAsset ( attachments , affix , destinationStackId , projectId , baseSiteUrl ) ;
424435 }
425436 return ;
426437 } catch ( error ) {
0 commit comments