Skip to content

Commit f6d97a2

Browse files
committed
refactor:file-processing conditions for all file types
1 parent d878c86 commit f6d97a2

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

upload-api/src/routes/index.ts

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,34 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
115115
file_details: config
116116
});
117117
});
118-
if (fileExt === 'zip') {
118+
if (fileExt === 'xml') {
119+
let xmlData = '';
120+
121+
// Collect the data from the stream as a string
122+
bodyStream.on('data', (chunk) => {
123+
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) {
124+
throw new Error('Expected chunk to be a string or a Buffer');
125+
} else {
126+
// Convert chunk to string (if it's a Buffer)
127+
xmlData += chunk.toString();
128+
}
129+
});
130+
131+
// When the stream ends, process the XML data
132+
bodyStream.on('end', async () => {
133+
if (!xmlData) {
134+
throw new Error('No data collected from the stream.');
135+
}
136+
137+
const data = await handleFileProcessing(fileExt, xmlData, cmsType,name);
138+
res.status(data?.status || 200).json(data);
139+
if (data?.status === 200) {
140+
const filePath = path.join(__dirname, '..', '..', 'extracted_files', "data.json");
141+
createMapper(filePath, projectId, app_token, affix, config);
142+
}
143+
});
144+
}
145+
else{
119146
// Create a writable stream to save the downloaded zip file
120147
let zipBuffer = Buffer.alloc(0);
121148

@@ -140,35 +167,7 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
140167
createMapper(filePath, projectId, app_token, affix, config);
141168
}
142169
});
143-
} else if (fileExt === 'xml') {
144-
let xmlData = '';
145-
146-
// Collect the data from the stream as a string
147-
bodyStream.on('data', (chunk) => {
148-
if (typeof chunk !== 'string' && !Buffer.isBuffer(chunk)) {
149-
throw new Error('Expected chunk to be a string or a Buffer');
150-
} else {
151-
// Convert chunk to string (if it's a Buffer)
152-
xmlData += chunk.toString();
153-
}
154-
});
155-
156-
// When the stream ends, process the XML data
157-
bodyStream.on('end', async () => {
158-
if (!xmlData) {
159-
throw new Error('No data collected from the stream.');
160-
}
161-
162-
const data = await handleFileProcessing(fileExt, xmlData, cmsType,name);
163-
res.status(data?.status || 200).json(data);
164-
if (data?.status === 200) {
165-
const filePath = path.join(__dirname, '..', '..', 'extracted_files', "data.json");
166-
createMapper(filePath, projectId, app_token, affix, config);
167-
}
168-
});
169-
}
170-
return;
171-
}
170+
}
172171
} else {
173172
const params = {
174173
Bucket: config?.awsData?.bucketName,
@@ -221,7 +220,8 @@ router.get('/validator', express.json(), fileOperationLimiter, async function (r
221220
});
222221
}
223222

224-
} catch (err: any) {
223+
} }
224+
catch (err: any) {
225225
console.error('🚀 ~ router.get ~ err:', err);
226226
}
227227
});

0 commit comments

Comments
 (0)