Skip to content
Snippets Groups Projects
Commit 5f745dc6 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

feat(mts): upload package to S3 as backup

parent b03852ef
No related branches found
No related tags found
2 merge requests!21Sprint #16 features,!15MTS integration
......@@ -8,7 +8,7 @@ const logger = require('@pubsweet/logger')
const filterByType = (fileTypes = []) => ({ Metadata: { filetype } }) =>
fileTypes.length > 0 ? fileTypes.includes(filetype) : true
const zipAWSFiles = (s3Config, archiver = nodeArchiver) => {
const createFilesPackage = (s3Config, archiver = nodeArchiver) => {
AWS.config.update({
secretAccessKey: s3Config.secretAccessKey,
accessKeyId: s3Config.accessKeyId,
......@@ -40,8 +40,8 @@ const zipAWSFiles = (s3Config, archiver = nodeArchiver) => {
if (s3Files) {
const packageOutput = fs.createWriteStream(`${manuscriptName}.zip`)
const archive = archiver('zip')
archive.pipe(packageOutput)
archive.pipe(packageOutput)
archive.append(xmlFile.content, { name: xmlFile.name })
s3Files.filter(filterByType(fileTypes)).forEach(f => {
......@@ -52,9 +52,18 @@ const zipAWSFiles = (s3Config, archiver = nodeArchiver) => {
name: isManuscript ? `${manuscriptName}.${extension}` : name,
})
})
archive.on('error', err => {
throw err
})
archive.on('end', err => {
if (err) throw err
uploadToS3({
s3,
filename: `${manuscriptName}.zip`,
Bucket: s3Config.bucket,
})
})
archive.finalize()
}
} else {
......@@ -68,4 +77,42 @@ const zipAWSFiles = (s3Config, archiver = nodeArchiver) => {
}
}
module.exports = zipAWSFiles
const readFile = filename =>
new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) reject(err)
resolve(data)
})
})
const uploadToS3 = async ({ s3, filename, Bucket }) => {
const data = await readFile(filename)
const params = {
Bucket,
Body: data,
Key: `mts/${filename}`,
}
s3.upload(params, (err, data) => {
deleteFile(filename)
if (err) {
throw err
} else {
logger.info(`Successfully uploaded ${filename}`)
}
})
}
const deleteFile = filename => {
fs.access(filename, fs.constants.F_OK, err => {
if (err) {
if (err) throw err
} else {
fs.unlink(filename, err => {
if (err) throw err
logger.info(`Deleted ${filename}`)
})
}
})
}
module.exports = createFilesPackage
const convert = require('xml-js')
const { set, get } = require('lodash')
const zipAWSFiles = require('./AWS')
const createFilesPackage = require('./AWS')
const mts = require('./mts-json-template')
const {
defaultConfig,
......@@ -206,9 +206,9 @@ class MTS {
return this.convertToXML(this.composeJson(fragment))
}
createMTSPackage(fragment = {}) {
async createMTSPackage(fragment = {}) {
const xmlFile = this.convertFragmentToXML(fragment)
zipAWSFiles(this.s3Config)({ fragment, xmlFile })
await createFilesPackage(this.s3Config)({ fragment, xmlFile })
}
}
......
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