Skip to content

Commit 21be5a8

Browse files
committed
refactor:added pre-processing for xmldata and site_url for asset if incomplete asset url
1 parent 6be3666 commit 21be5a8

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

api/src/services/wordpress.service.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = /^(https?:\/\/|www\.)/;
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) {

upload-api/src/helper/index.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,30 @@ const saveJson = async (jsonContent: string, fileName: string) => {
7979
}
8080
};
8181

82+
const cleanXml = (xml: string): string => {
83+
return xml
84+
.replace(/<!--[\s\S]*?-->/g, '')
85+
.replace(/<!DOCTYPE[^>]*>/g, '')
86+
.trim();
87+
};
88+
89+
90+
8291
// parse xml to json
8392
const parseXmlToJson = async (xml: any) => {
8493
try {
94+
95+
const xmldata = cleanXml(xml)
96+
8597
const parser = new xml2js.Parser({
8698
attrkey: "attributes",
8799
charkey: "text",
88100
explicitArray: false,
101+
trim: true,
102+
normalize: true,
103+
normalizeTags: true,
89104
});
90-
const data = await parser.parseStringPromise(xml);
105+
const data = await parser.parseStringPromise(xmldata);
91106
return data
92107
} catch (err) {
93108
console.error(err);

upload-api/src/services/fileProcessing.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import JSZip from 'jszip';
44
import validator from '../validators';
55
import config from '../config/index';
66
import logger from '../utils/logger.js';
7-
7+
import * as Cheerio from 'cheerio';
88

99
const handleFileProcessing = async (fileExt: string, zipBuffer: any, cmsType: string, name :string) => {
1010
if (fileExt === 'zip') {
@@ -36,7 +36,9 @@ const handleFileProcessing = async (fileExt: string, zipBuffer: any, cmsType: st
3636
}
3737
} else if (fileExt === 'xml') {
3838
if (await validator({ data: zipBuffer, type: cmsType, extension: fileExt }) ) {
39-
const parsedJson = await parseXmlToJson(zipBuffer);
39+
const $ = Cheerio.load(zipBuffer, { xmlMode: true });
40+
const fixedXml = $.xml();
41+
const parsedJson = await parseXmlToJson(fixedXml);
4042
const isSaved = await saveJson(parsedJson,"data.json");
4143
if (isSaved) {
4244
logger.info('Validation success:', {

0 commit comments

Comments
 (0)