-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from formio/fix-and-refactor
Fix formidable upload issue, fix error handling, do structural refactor
- Loading branch information
Showing
41 changed files
with
182 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict'; | ||
|
||
const debugScopes = { | ||
DEFAULT: 'pdfLibs.default', | ||
CONVERT_TO_HTML: 'pdfLibs.html', | ||
FORMFIELDS: 'pdfLibs.formfields' | ||
}; | ||
|
||
module.exports = debugScopes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
...require('./validation-error') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
class ValidationError extends Error { | ||
constructor (message, errors) { | ||
super(message); | ||
this.errors = errors; | ||
this.statusCode = 400; | ||
} | ||
|
||
withStatus (statusCode) { | ||
this.statusCode = statusCode; | ||
return this; | ||
} | ||
} | ||
|
||
module.exports = {ValidationError}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const logDebug = require('debug'); | ||
|
||
const debug = (namespace) => { | ||
const log = logDebug(namespace); | ||
return (req, __res, next) => { | ||
req.debug = (msg) => { | ||
log(`${msg}`); | ||
}; | ||
next(); | ||
}; | ||
}; | ||
|
||
module.exports = {debug}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const {ValidationError} = require('../errors'); | ||
|
||
const errorHandler = (err, req, res, __next) => { | ||
req.debug?.(`Error: ${err.message || err}`); | ||
if (err instanceof ValidationError) { | ||
return res.status(err.statusCode).send(err.message); | ||
} | ||
res.status(500).send(err.message ?? err); | ||
}; | ||
|
||
module.exports = {errorHandler}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict'; | ||
|
||
const formidable = require('formidable'); | ||
const {ValidationError} = require('../errors'); | ||
|
||
const getFileFromFormData = (req, __res, next) => { | ||
const form = new formidable.IncomingForm(); | ||
req.cleanup = []; | ||
form.parse(req, async (err, __, files) => { | ||
if (err) { | ||
return next(new ValidationError(err?.message ?? err)); | ||
} | ||
if (!files.pdf) { | ||
return next(new ValidationError('No files were uploaded')); | ||
} | ||
req.filePath = files.pdf[0].filepath; | ||
req.cleanup.push(req.file); | ||
next(); | ||
}); | ||
}; | ||
|
||
module.exports = {getFileFromFormData}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
...require('./pdf'), | ||
...require('./get-file-from-formdata'), | ||
...require('./cleanup'), | ||
...require('./debug'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const {extractFormfields} = require('../../services/pdf/formfields'); | ||
|
||
const getFormfields = async (req, res, next) => { | ||
try { | ||
const jsonOutput = await extractFormfields(req.filePath); | ||
res.json(jsonOutput); | ||
} | ||
catch (err) { | ||
return next(err); | ||
} | ||
}; | ||
|
||
module.exports = {getFormfields}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
...require('./get-formfields'), | ||
...require('./optimize-pdf'), | ||
...require('./hide-formfields'), | ||
...require('./convert-to-html'), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.