Skip to content

Commit

Permalink
added archiveOptions for determining fileExt and getting studentName
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgtho committed Jul 1, 2022
1 parent c7e005f commit 5b4face
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
54 changes: 52 additions & 2 deletions lib/archive.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/run-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down

0 comments on commit 5b4face

Please sign in to comment.