From 5b4facefd798e5ca1934a7a50dc1a606cca1cc7a Mon Sep 17 00:00:00 2001 From: jorgtho Date: Fri, 1 Jul 2022 13:13:20 +0200 Subject: [PATCH] added archiveOptions for determining fileExt and getting studentName --- lib/archive.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++-- lib/run-flow.js | 2 +- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/lib/archive.js b/lib/archive.js index 17e77ea..36418d8 100644 --- a/lib/archive.js +++ b/lib/archive.js @@ -1,8 +1,36 @@ const { logger } = require('@vtfk/logger') const axios = require('axios').default -const { ARCHIVE_URL, ARCHIVE_KEY } = require('../config') +const { fromBuffer } = require('file-type') +const path = require('path') +const { ARCHIVE_URL, ARCHIVE_KEY, FILE_FORMATS } = require('../config') const { createTask, createJob } = require('./e18') +const verifyExt = (ext) => { + const valid = FILE_FORMATS.includes(ext.toLowerCase()) || FILE_FORMATS.includes(ext.toUpperCase()) + return valid +} + +const getExtFromInfo = (infoStr) => { + const unknownFileExt = 'UF' + if (typeof infoStr !== 'string') { + logger('warn', [`Infostring was not string! Setting filetype to Unknown Format (${unknownFileExt})`]) + return unknownFileExt + } + const exts = [] + const infos = infoStr.split(';').map(i => path.extname(i).replace('.', '')).filter(i => verifyExt(i)) // Get only elements with fileExt, remove "." then filter on only valid fileExt for P360 + for (const ext of infos) { + if (!exts.includes(ext)) exts.push(ext) // Get only unique values + } + if (exts.length > 1) { + logger('warn', ['Found several fileExt in infostring', exts, `Setting filetype to Unknown Format (${unknownFileExt})`]) + return unknownFileExt + } else if (exts.length === 0) { + logger('warn', ['Could not determine fileExt from infostring', infoStr, `Setting filetype to Unknown Format (${unknownFileExt})`]) + return unknownFileExt + } + return exts[0] +} + const callArchive = async (payload, e18JobId, e18TaskId, endpoint) => { try { const headers = { @@ -36,11 +64,12 @@ const getAddress = streetAddress => { return 'Ukjent adresse' } -module.exports.archive = async blobContent => { +module.exports.archive = async (blobContent, archiveOptions) => { try { blobContent.flow.archiveTaskId = blobContent.flow.archiveTaskId || await createTask(blobContent.e18JobId, { system: 'p360', method: 'archive' }) logger('info', ['archive', 'archive']) + const payload = { system: 'vigo', template: blobContent.Dokumentelement.Dokumenttype, @@ -51,6 +80,27 @@ module.exports.archive = async blobContent => { base64: blobContent.Dokumentelement.Dokumentfil } } + + if (archiveOptions) { + if (archiveOptions.useStudentName) { + payload.parameter.studentName = `${blobContent.flow.syncElevmappa.privatePerson.firstName} ${blobContent.flow.syncElevmappa.privatePerson.lastName}` + } + if (archiveOptions.determineFileExt) { + let ext = false + const fileType = await fromBuffer(Buffer.from(blobContent.Dokumentelement.Dokumentfil, 'base64')) // Check if we have file ext from base64 + if (fileType && fileType.ext && verifyExt(fileType.ext)) { + ext = fileType.ext + logger('info', ['Found file type from base64', ext]) + } + if (!ext) { + logger('info', ['Could not find file type from base64, will try to use infostring']) + ext = getExtFromInfo(blobContent.Dokumentelement.Info) // get file ext from blobContent.Dokumentelement.Info + logger('info', [`File type from infoString: "${ext}"`]) + } + payload.parameter.fileExt = ext // add to payload + } + } + const res = await callArchive(payload, blobContent.e18JobId, blobContent.flow.archiveTaskId, 'archive') if (typeof res === 'object') { blobContent.flow.archive = res diff --git a/lib/run-flow.js b/lib/run-flow.js index 6445619..321c05f 100644 --- a/lib/run-flow.js +++ b/lib/run-flow.js @@ -37,7 +37,7 @@ module.exports = async (blobContent, flowDef) => { // Archive if (flowDef.archive) { if (!blobContent.flow.archive || blobContent.flow.archive.status !== 'finished') { - blobContent = await archive(blobContent) + blobContent = await archive(blobContent, flowDef.archiveOptions) } if (blobContent.flow.archive.status !== 'finished') {