Skip to content

Commit

Permalink
🆙 add upload to handle files
Browse files Browse the repository at this point in the history
  • Loading branch information
juanesarango committed Dec 20, 2023
1 parent d491803 commit 2d70d49
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 28 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ test/data/*-out.*
test.csv

# Serverless artifacts
.serverless
.serverless

# API uploads
uploads
126 changes: 107 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
"vitest": "^0.34.4"
},
"dependencies": {
"@types/multer": "^1.4.11",
"exceljs": "^4.3.0",
"express": "^4.18.2",
"ipssm": "^1.0.4",
"joi": "^17.11.0",
"multer": "^1.4.5-lts.1",
"papaparse": "^5.4.1",
"serverless-http": "^3.2.0",
"yargs": "^17.7.2"
Expand Down
35 changes: 27 additions & 8 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import path from 'path'
import { annotateFile, ipssm, ipssr } from './index'
import { validatePatientForIpssm, validatePatientForIpssr } from './utils/validation'
import serverless from 'serverless-http'
import multer from 'multer'
import fs from 'fs'

const app = express()
const port: number = process.env.PORT ? parseInt(process.env.PORT) : 3000
Expand All @@ -18,8 +20,8 @@ app.use((req, _res, next) => {

// Serve the redoc swagger documentation
app.get('/', (_req, res) => {
res.sendFile(path.join(__dirname, '..', 'redoc-static.html'));
});
res.sendFile(path.join(__dirname, '..', 'redoc-static.html'))
})

// Endpoint for Ipssm
app.post('/ipssm', validatePatientForIpssm, async (req: Request, res: Response) => {
Expand Down Expand Up @@ -66,15 +68,32 @@ app.post('/ipssr', validatePatientForIpssr, async (req: Request, res: Response)
}
})

// Set up multer to store uploaded files in memory
const upload = multer({ dest: 'uploads/' })

// Endpoint for annotating a tsv/xlsx file
app.get('/annotateFile', async (req: Request, res: Response) => {
const inputFile: string = req.query.inputFile as string
const outputFile: string = req.query.outputFile as string

app.post('/annotateFile', upload.single('file'), async (req: Request, res: Response) => {
console.log(`Output File format: ${req.body.outputFormat}`)
const outputFilePath: string = `uploads/Annotated.IPSSM.${req.body.outputFormat as string || 'csv'}`
try {
await annotateFile(inputFile, outputFile)
res.send('File annotated successfully.')
if (req.file) {
const originalFilename = req.file.originalname
const originalExtension = path.extname(originalFilename)
const inputFilePath = req.file.path + originalExtension

// Rename the uploaded file to include the original extension
fs.renameSync(req.file.path, inputFilePath)

await annotateFile(inputFilePath, outputFilePath)
const outputFileContent = fs.readFileSync(outputFilePath, 'utf8')

// Send the file content as the response
res.send(outputFileContent)
} else {
throw new Error('No file uploaded.')
}
} catch (error) {
console.log((error as Error).stack)
res.status(500).send('An error occurred while annotating the file.')
}
})
Expand Down

0 comments on commit 2d70d49

Please sign in to comment.