Skip to content
Snippets Groups Projects
Commit 65855356 authored by Yogmatee Roochun's avatar Yogmatee Roochun
Browse files

xml triggers

parent 00e1042c
No related branches found
No related tags found
2 merge requests!60Dev,!58Shared data model
......@@ -5,6 +5,8 @@ const readline = require('readline')
const path = require('path')
const mime = require('mime-types')
const uuidv4 = require('uuid/v4')
const download = require('download')
const fetch = require('node-fetch')
const minioClient = rfr('server/modules/pubsweet-component-minio/minio-client')
......@@ -113,6 +115,44 @@ module.exports.renameFile = function renameFile(path) {
})
}
module.exports.downloadFile = function downloadFile(url, tmpPath) {
return new Promise((resolve, reject) => {
download(url, tmpPath)
.then(() => {
resolve(`${tmpPath}/${fs.readdirSync(tmpPath)[0]}`)
})
.catch(err => {
reject(err)
})
})
}
module.exports.readData = function readData(url) {
return new Promise((resolve, reject) => {
fs.readFile(url, 'utf8', (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
module.exports.fetchFile = function fetchFile(fileUrl) {
return new Promise((resolve, reject) => {
fetch(fileUrl, {
method: 'GET',
headers: {
Accept: 'application/xml',
},
})
.then(data => {
resolve(data.text())
})
.catch(err => {
reject(err)
})
})
}
function uploadFileToMinio(filename, originalFileName, filePath, mimeType) {
return new Promise((resolve, reject) => {
minioClient.uploadFile(
......
const path = require('path')
const mime = require('mime-types')
const libxml = require('libxmljs')
const fetch = require('node-fetch')
const xslt4node = require('xslt4node')
const fs = require('fs')
const uuidv4 = require('uuid/v4')
const tar = require('../ftp-integration/functions/unTar.js')
const download = require('download')
const getUser = require('../ftp-integration/functions/user.js')
const files = require('../ftp-integration/functions/files.js')
const db = require('../ftp-integration/functions/db.js')
const logger = require('@pubsweet/logger')
const Manuscript = require('../xpub-model/entities/manuscript/data-access')
xslt4node.addLibrary('./saxon9he.jar')
xslt4node.addOptions('-Xmx1g')
/*
pushXML(
'http://localhost:3000/download/afa255c0-2af0-11e9-b3b7-33043ebc220c.xml',
'EMS80002',
)
*/
module.exports.pushXML = async function pushXML(url, manuscriptId) {
async function pushXML(url, manuscriptId) {
try {
const xml = await fetchFile(url)
const xsdBlue = await readData(
const xml = await files.fetchFile(url)
const xsdBlue = await files.readData(
path.resolve(__dirname, 'xsl/xsd/publishing/journalpublishing3.xsd'),
)
const xsdGreen = await readData(
const xsdGreen = await files.readData(
path.resolve(__dirname, 'xsl/xsd/archiving/archivearticle3.xsd'),
)
......@@ -59,7 +56,8 @@ module.exports.pushXML = async function pushXML(url, manuscriptId) {
errString += `\n\n`
}
})
logger.info(errString)
await Manuscript.updateErrorMsg(manuscriptId, errString)
logger.error(errString)
} else {
const nxml = await transformXML(
xml,
......@@ -82,7 +80,8 @@ module.exports.pushXML = async function pushXML(url, manuscriptId) {
errString += `\n\n`
}
})
logger.info(errString)
await Manuscript.updateErrorMsg(manuscriptId, errString)
logger.error(errString)
} else {
// Check NXML against the stylechecker
const checked = await transformXML(
......@@ -97,7 +96,7 @@ module.exports.pushXML = async function pushXML(url, manuscriptId) {
if (styleErrors.length === 0) {
const user = await getUser.getAdminUser()
const tmpPath = await tar.createTempDir()
const file = await downloadFile(url, tmpPath)
const file = await files.downloadFile(url, tmpPath)
const filename = file.substring(url.lastIndexOf('/') + 1)
const fileType = 'xml'
const fileLabel = '0'
......@@ -137,53 +136,17 @@ module.exports.pushXML = async function pushXML(url, manuscriptId) {
styleErrString += `<br/><br/>`
}
})
logger.info(styleErrString)
await Manuscript.updateErrorMsg(manuscriptId, errString)
logger.error(styleErrString)
}
}
}
} catch (err) {
logger.error('Error', err.message)
await Manuscript.updateErrorMsg(manuscriptId, err.message)
logger.error(err.message)
}
}
function downloadFile(url, tmpPath) {
return new Promise((resolve, reject) => {
download(url, tmpPath)
.then(() => {
resolve(`${tmpPath}/${fs.readdirSync(tmpPath)[0]}`)
})
.catch(err => {
reject(err)
})
})
}
function readData(url) {
return new Promise((resolve, reject) => {
fs.readFile(url, 'utf8', (err, data) => {
if (err) reject(err)
resolve(data)
})
})
}
function fetchFile(fileUrl) {
return new Promise((resolve, reject) => {
fetch(fileUrl, {
method: 'GET',
headers: {
Accept: 'application/xml',
},
})
.then(data => {
resolve(data.text())
})
.catch(err => {
reject(err)
})
})
}
function transformXML(xmlString, xslPath, params) {
xmlString = xmlString.replace(/<!DOCTYPE[^>[]*(\\[[^]]*\\])?>/, '')
const config = {
......
......@@ -462,6 +462,13 @@ class Manuscript extends EpmcBaseModel {
})
.returning('*')
}
static async updateErrorMsg(id, errorMsg) {
const manuscriptUpdated = await Manuscript.query()
.patch({ formState: errorMsg, status: 'xml-triage' })
.where('id', id)
return manuscriptUpdated
}
}
module.exports = Manuscript
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment