UNPKG

@defra/wls-eps-web-service

Version:

The web service for wildlife licencing of European Protected Species

33 lines (26 loc) 1 kB
import fs from 'fs' import { AWS } from '@defra/wls-connectors-lib' import { boomify } from '@hapi/boom' import { v4 as uuidv4 } from 'uuid' import db from 'debug' import { APIRequests } from './api-requests.js' const debug = db('web-service:s3') const { writeFileStream } = AWS.S3() export const s3FileUpload = (context, filename, filepath, filetype) => async id => { const fileReadStream = fs.createReadStream(filepath) // The filename will be recorded by the API const objectKey = uuidv4() try { await writeFileStream(objectKey, fileReadStream) debug(`Wrote file ${filename} with key: ${objectKey}`) // Record the file upload on the API await APIRequests.FILE_UPLOAD[context].record(id, filename, filetype, objectKey) // Remove the temporary file fs.unlinkSync(filepath) debug(`Removed temporary file ${filepath}`) } catch (err) { console.error(`Cannot write data with key: ${objectKey}`, err) boomify(err, { statusCode: 500 }) throw err } }