Skip to content

Commit 70e53a2

Browse files
committed
refactor:the logic for saving wordpress xml file with dynamic name and validating condition
1 parent 5567b82 commit 70e53a2

File tree

2 files changed

+52
-50
lines changed

2 files changed

+52
-50
lines changed

upload-api/src/routes/index.ts

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,17 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
9797
const cmsType = config?.cmsType?.toLowerCase();
9898

9999
if (config?.isLocalPath) {
100-
const fileName = config?.localPath?.split?.('/')?.pop?.();
100+
const fileName = path.basename(config?.localPath || "");
101+
//const fileName = config?.localPath?.replace(/\/$/, "")?.split?.('/')?.pop?.();
102+
101103
if (!fileName) {
102104
res.send('Filename could not be determined from the local path.');
103105
}
104106

105107
if (fileName) {
106108
const name = fileName?.split?.('.')?.[0];
107109
const fileExt = fileName?.split('.')?.pop() ?? '';
108-
const bodyStream = createReadStream(config?.localPath);
110+
const bodyStream = createReadStream(config?.localPath?.replace(/\/$/, ""));
109111

110112
bodyStream.on('error', (error: any) => {
111113
console.error(error);
@@ -137,7 +139,7 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
137139
const data = await handleFileProcessing(fileExt, xmlData, cmsType,name);
138140
res.status(data?.status || 200).json(data);
139141
if (data?.status === 200) {
140-
const filePath = path.join(__dirname, '..', '..', 'extracted_files', "data.json");
142+
const filePath = path.join(__dirname, '..', '..', 'extracted_files', `${name}.json`);
141143
createMapper(filePath, projectId, app_token, affix, config);
142144
}
143145
});
@@ -168,59 +170,59 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
168170
}
169171
});
170172
}
171-
} else {
172-
const params = {
173-
Bucket: config?.awsData?.bucketName,
174-
Key: config?.awsData?.buketKey
175-
};
176-
const getObjectCommand = new GetObjectCommand(params);
177-
// Get the object from S3
178-
const s3File = await client.send(getObjectCommand);
179-
//file Name From key
180-
const fileName = params?.Key?.split?.('/')?.pop?.() ?? '';
181-
//file ext from fileName
182-
const fileExt = fileName?.split?.('.')?.pop?.() ?? 'test';
183-
184-
if (!s3File.Body) {
185-
throw new Error('Empty response body from S3');
186-
}
187-
188-
const bodyStream: Readable = s3File.Body as Readable;
173+
}
174+
}else {
175+
const params = {
176+
Bucket: config?.awsData?.bucketName,
177+
Key: config?.awsData?.buketKey
178+
};
179+
const getObjectCommand = new GetObjectCommand(params);
180+
// Get the object from S3
181+
const s3File = await client.send(getObjectCommand);
182+
//file Name From key
183+
const fileName = params?.Key?.split?.('/')?.pop?.() ?? '';
184+
//file ext from fileName
185+
const fileExt = fileName?.split?.('.')?.pop?.() ?? 'test';
186+
187+
if (!s3File.Body) {
188+
throw new Error('Empty response body from S3');
189+
}
189190

190-
// Create a writable stream to save the downloaded zip file
191-
const zipFileStream = createWriteStream(`${fileName}`);
191+
const bodyStream: Readable = s3File.Body as Readable;
192192

193-
// // Pipe the S3 object's body to the writable stream
194-
bodyStream.pipe(zipFileStream);
193+
// Create a writable stream to save the downloaded zip file
194+
const zipFileStream = createWriteStream(`${fileName}`);
195195

196-
// Create a writable stream to save the downloaded zip file
197-
let zipBuffer: Buffer | null = null;
196+
// // Pipe the S3 object's body to the writable stream
197+
bodyStream.pipe(zipFileStream);
198198

199-
// Collect the data from the stream into a buffer
200-
bodyStream.on('data', (chunk) => {
201-
if (zipBuffer === null) {
202-
zipBuffer = chunk;
203-
} else {
204-
zipBuffer = Buffer.concat([zipBuffer, chunk]);
205-
}
206-
});
199+
// Create a writable stream to save the downloaded zip file
200+
let zipBuffer: Buffer | null = null;
207201

208-
//buffer fully stremd
209-
bodyStream.on('end', async () => {
210-
if (!zipBuffer) {
211-
throw new Error('No data collected from the stream.');
212-
}
202+
// Collect the data from the stream into a buffer
203+
bodyStream.on('data', (chunk) => {
204+
if (zipBuffer === null) {
205+
zipBuffer = chunk;
206+
} else {
207+
zipBuffer = Buffer.concat([zipBuffer, chunk]);
208+
}
209+
});
213210

214-
const data = await handleFileProcessing(fileExt, zipBuffer, cmsType,fileName);
215-
res.json(data);
216-
res.send('file valited sucessfully.');
217-
const filePath = path.join(__dirname, '..', '..', 'extracted_files', fileName);
218-
console.log("🚀 ~ bodyStream.on ~ filePath:", filePath)
219-
createMapper(filePath, projectId, app_token, affix, config);
220-
});
221-
}
211+
//buffer fully stremd
212+
bodyStream.on('end', async () => {
213+
if (!zipBuffer) {
214+
throw new Error('No data collected from the stream.');
215+
}
222216

223-
} }
217+
const data = await handleFileProcessing(fileExt, zipBuffer, cmsType,fileName);
218+
res.json(data);
219+
res.send('file valited sucessfully.');
220+
const filePath = path.join(__dirname, '..', '..', 'extracted_files', fileName);
221+
console.log("🚀 ~ bodyStream.on ~ filePath:", filePath)
222+
createMapper(filePath, projectId, app_token, affix, config);
223+
});
224+
}
225+
}
224226
catch (err: any) {
225227
console.error('🚀 ~ router.get ~ err:', err);
226228
}

upload-api/src/services/fileProcessing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const handleFileProcessing = async (fileExt: string, zipBuffer: any, cmsType: st
3939
const $ = Cheerio.load(zipBuffer, { xmlMode: true });
4040
const fixedXml = $.xml();
4141
const parsedJson = await parseXmlToJson(fixedXml);
42-
const isSaved = await saveJson(parsedJson,"data.json");
42+
const isSaved = await saveJson(parsedJson,`${name}.json`);
4343
if (isSaved) {
4444
logger.info('Validation success:', {
4545
status: HTTP_CODES?.OK,

0 commit comments

Comments
 (0)