Skip to content
Snippets Groups Projects
Commit ce80a99d authored by Alf Eaton's avatar Alf Eaton
Browse files

Apply linter

parent a35cb761
No related branches found
No related tags found
No related merge requests found
...@@ -12,7 +12,7 @@ const Pusher = require('pusher-js') ...@@ -12,7 +12,7 @@ const Pusher = require('pusher-js')
const inkConfig = config.get('pubsweet-component-ink-backend') const inkConfig = config.get('pubsweet-component-ink-backend')
// Generate the absolute URL // Generate the absolute URL
const inkUrl = path => inkConfig.inkEndpoint + 'api/' + path const inkUrl = path => `${inkConfig.inkEndpoint}api/${path}`
// Connect to the INK Pusher/Slanger endpoint // Connect to the INK Pusher/Slanger endpoint
const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options) const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options)
...@@ -20,63 +20,67 @@ const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options) ...@@ -20,63 +20,67 @@ const connectToPusher = ({ appKey, ...options }) => new Pusher(appKey, options)
const pusher = connectToPusher(inkConfig.pusher) const pusher = connectToPusher(inkConfig.pusher)
// Sign in // Sign in
const authorize = () => rp({ const authorize = () =>
method: 'POST', rp({
uri: inkUrl('auth/sign_in'), method: 'POST',
formData: { uri: inkUrl('auth/sign_in'),
email: inkConfig.email, formData: {
password: inkConfig.password email: inkConfig.email,
}, password: inkConfig.password,
headers: { },
'Accept': 'application/vnd.ink.1' headers: {
}, Accept: 'application/vnd.ink.1',
resolveWithFullResponse: true },
}).then(res => ({ resolveWithFullResponse: true,
'client': res.headers['client'], }).then(res => ({
'access-token': res.headers['access-token'] client: res.headers.client,
})) 'access-token': res.headers['access-token'],
}))
// Upload file to INK and execute the recipe // Upload file to INK and execute the recipe
const upload = (recipeId, inputFile, auth) => rp({ const upload = (recipeId, inputFile, auth) =>
method: 'POST', rp({
uri: inkUrl('recipes/' + recipeId + '/execute'), method: 'POST',
headers: { uri: inkUrl(`recipes/${recipeId}/execute`),
uid: inkConfig.email, headers: {
...auth uid: inkConfig.email,
}, ...auth,
formData: { },
input_files: [inputFile] formData: {
}, input_files: [inputFile],
json: true, },
timeout: 60 * 60 * 1000 // 3600 seconds json: true,
}) timeout: 60 * 60 * 1000, // 3600 seconds
})
// Download the output file // Download the output file
const download = (chainId, auth, outputFileName) => rp({ const download = (chainId, auth, outputFileName) =>
uri: inkUrl('process_chains/' + chainId + '/download_output_file'), rp({
qs: { uri: inkUrl(`process_chains/${chainId}/download_output_file`),
relative_path: outputFileName qs: {
}, relative_path: outputFileName,
headers: { },
uid: inkConfig.email, headers: {
...auth uid: inkConfig.email,
} ...auth,
}) },
})
// Find the ID of a recipe by name // Find the ID of a recipe by name
const findRecipeId = (name = 'Editoria Typescript', auth) => rp({ const findRecipeId = (name = 'Editoria Typescript', auth) =>
method: 'GET', rp({
uri: inkUrl('recipes'), method: 'GET',
headers: { uri: inkUrl('recipes'),
uid: inkConfig.email, headers: {
...auth uid: inkConfig.email,
}, ...auth,
json: true },
}).then(data => { json: true,
const recipe = data.recipes.find(recipe => recipe.name === name) }).then(data => {
const recipe = data.recipes.find(recipe => recipe.name === name)
return recipe ? recipe.id : null
}) return recipe ? recipe.id : null
})
const process = async (inputFile, options) => { const process = async (inputFile, options) => {
const auth = await authorize().catch(err => { const auth = await authorize().catch(err => {
...@@ -85,7 +89,9 @@ const process = async (inputFile, options) => { ...@@ -85,7 +89,9 @@ const process = async (inputFile, options) => {
}) })
// either use the recipe id from the configuration or search for it by name // either use the recipe id from the configuration or search for it by name
const recipeId = inkConfig.recipes[options.recipe] || await findRecipeId(options.recipe, auth) const recipeId =
inkConfig.recipes[options.recipe] ||
(await findRecipeId(options.recipe, auth))
if (!recipeId) throw new Error('Unknown recipe') if (!recipeId) throw new Error('Unknown recipe')
const response = await upload(recipeId, inputFile, auth).catch(err => { const response = await upload(recipeId, inputFile, auth).catch(err => {
...@@ -115,23 +121,36 @@ const process = async (inputFile, options) => { ...@@ -115,23 +121,36 @@ const process = async (inputFile, options) => {
const manifest = chain.input_file_manifest const manifest = chain.input_file_manifest
if (manifest.length === 0) { if (manifest.length === 0) {
reject(new Error('The INK server gave a malformed response (no input files in the process chain)')) reject(
new Error(
'The INK server gave a malformed response (no input files in the process chain)',
),
)
} }
// backwards compatibility // backwards compatibility
if (!options.outputFileName) { if (!options.outputFileName) {
options.outputFileName = path.basename(manifest[0].path, '.docx') + '.html' options.outputFileName = `${path.basename(
manifest[0].path,
'.docx',
)}.html`
} }
// download the output file // download the output file
logger.info(`Downloading output file ${options.outputFileName} from chain ${chain.id}`) logger.info(
`Downloading output file ${options.outputFileName} from chain ${
download(chain.id, auth, options.outputFileName).then(result => { chain.id
resolve(result) }`,
}).catch(error => { )
logger.error('Error downloading from INK:', error.message)
reject(error) download(chain.id, auth, options.outputFileName)
}) .then(result => {
resolve(result)
})
.catch(error => {
logger.error('Error downloading from INK:', error.message)
reject(error)
})
} }
// handle "processing completed" events on this channel // handle "processing completed" events on this channel
...@@ -145,41 +164,46 @@ const process = async (inputFile, options) => { ...@@ -145,41 +164,46 @@ const process = async (inputFile, options) => {
}) })
} }
const InkBackend = function (app) { const InkBackend = app => {
// TODO: authentication on this route // TODO: authentication on this route
app.use('/api/ink', (req, res, next) => { app.use('/api/ink', (req, res, next) => {
const fileStream = new Busboy({ headers: req.headers }) const fileStream = new Busboy({ headers: req.headers })
fileStream.on('file', (fieldname, file, filename, encoding, contentType) => { fileStream.on(
const stream = temp.createWriteStream() 'file',
(fieldname, file, filename, encoding, contentType) => {
stream.on('finish', () => { const stream = temp.createWriteStream()
const inputFile = {
value: fs.createReadStream(stream.path), stream.on('finish', () => {
options: { filename, contentType } const inputFile = {
} value: fs.createReadStream(stream.path),
options: { filename, contentType },
logger.info(`Uploading file to INK for processing`) }
process(inputFile, req.query).then(converted => { logger.info(`Uploading file to INK for processing`)
res.json({ converted })
process(inputFile, req.query)
// clean up temp file .then(converted => {
fs.unlink(stream.path, () => { res.json({ converted })
logger.info('Deleted temporary file', stream.path)
}) // clean up temp file
}).catch(err => { fs.unlink(stream.path, () => {
logger.error('ERROR CONVERTING WITH INK:', err.message) logger.info('Deleted temporary file', stream.path)
next(err) })
})
.catch(err => {
logger.error('ERROR CONVERTING WITH INK:', err.message)
next(err)
})
}) })
})
file.pipe(stream) file.pipe(stream)
file.on('end', () => { file.on('end', () => {
stream.end() stream.end()
}) })
}) },
)
fileStream.on('error', err => { fileStream.on('error', err => {
logger.error(err) logger.error(err)
......
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