Skip to content

Commit

Permalink
Исправлена ошибка парсинга xml файлов на бэке
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiontik committed Jun 10, 2024
1 parent f7d7c48 commit 0fa409d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/backend/helpers/request.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import uriTool from './uri.mjs';
import gitlab from './gitlab.mjs';
import bitbucket from './bitbucket.mjs';
import logger from '../utils/logger.mjs';
import xml from '../../global/helpers/xmlparser.mjs';

const REQUEST_TAG = 'request';

Expand Down Expand Up @@ -40,14 +41,20 @@ function isAvailablePath(path) {
return path.startsWith(`${$paths.file_storage}/`);
}

const CONTENT_TYPE_YAML = 'application/x-yaml';
const CONTENT_TYPE_JSON = 'application/json';
const CONTENT_TYPE_XML = 'application/xhtml+xml';

// Определяет тип контента
function getContentType(url) {
let contentType = null;
const uri = url.split('?')[0];
if (uri.endsWith('.yaml') || uri.endsWith('.yml') || (uri.indexOf('.yaml/raw') >= 0) || (uri.indexOf('.yml/raw') >= 0)) {
contentType = 'application/x-yaml';
contentType = CONTENT_TYPE_YAML;
} else if (uri.endsWith('.json') || (uri.indexOf('.json/raw') >= 0)) {
contentType = 'application/json';
contentType = CONTENT_TYPE_JSON;
} else if (uri.endsWith('.xml') || (uri.indexOf('.xml/raw') >= 0)) {
contentType = CONTENT_TYPE_XML;
}
return contentType;
}
Expand Down Expand Up @@ -79,11 +86,13 @@ async function request(url, baseURI, response) {
const result = {
data: fs.readFileSync(fileName, { encoding: 'utf8', flag: 'r' })
};
if (contentType === 'application/x-yaml') {
if (contentType === CONTENT_TYPE_YAML) {
result.data = yaml.parse(result.data);
} else if (contentType === 'application/json') {
} else if (contentType === CONTENT_TYPE_JSON) {
result.data = JSON.parse(result.data);
}
} else if (contentType === CONTENT_TYPE_XML) {
result.data = xml.parse(result.data);
}
return result;
}
} // Если запрос по http / https
Expand Down
16 changes: 3 additions & 13 deletions src/frontend/helpers/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,7 @@ import crc16 from '@global/helpers/crc16';
import gitlab from '@front/helpers/gitlab';
import uriTool from '@front/helpers/uri';
import { Buffer } from 'buffer';

const XML = {
parse(xml) {
let result = null;
require('xml2js').parseString(xml, (err, json) => {
if (err) throw err;
result = json;
});
return result;
}
};
import xml from '@global/helpers/xmlparser';

import env, { Plugins } from './env';
import { responseCacheInterceptor, requestCacheInterceptor } from './cache';
Expand Down Expand Up @@ -69,7 +59,7 @@ axios.interceptors.response.use(async(response) => {
(url.indexOf('.xml/raw') >= 0)
|| (url.endsWith('.xml'))
|| (response?.headers || {})['content-type'] === 'application/xml')
response.data = XML.parse(response.data);
response.data = xml.parse(response.data);
}
}

Expand All @@ -93,7 +83,7 @@ function injectPAPIMiddleware() {
switch (type) {
case 'yaml': response.data = YAML.parse(response.data); break;
case 'json': response.data = JSON.parse(response.data); break;
case 'xml': !request.raw && (response.data = XML.parse(response.data)); break;
case 'xml': !request.raw && (response.data = xml.parse(response.data)); break;
case 'jpg':
type = 'jpeg';
// eslint-disable-next-line no-fallthrough
Expand Down
12 changes: 12 additions & 0 deletions src/global/helpers/xmlparser.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import parser from 'xml2js';

export default {
parse(xml) {
let result = null;
parser.parseString(xml, (err, json) => {
if (err) throw err;
result = json;
});
return result;
}
};

0 comments on commit 0fa409d

Please sign in to comment.