Fix problems with uploading files too large

This commit is contained in:
Robbie Antenesse 2018-12-27 16:13:48 -07:00
parent e2a265783e
commit aaa5a2c0e0
2 changed files with 39 additions and 33 deletions

View File

@ -32,7 +32,6 @@ function Server () {
this.server.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
this.server.use(fileUpload({ // support file uploads
abortOnLimit: true,
limits: {
fileSize: (settings.maxFileSize > 0 ? settings.maxFileSize * 1024 * 1024 : Infinity), // filesize in bytes (settings accepts MB)
},
@ -62,11 +61,11 @@ function Server () {
});
this.server.post('/give', (req, res) => {
const resourcePath = (req.url.substr(-1) === '/' ? '../' : './');
const { title, author, summary, contributor } = req.body;
if (Object.keys(req.files).length > 0
&& req.body.hasOwnProperty('title') && req.body.title.trim() !== ''
&& req.body.hasOwnProperty('summary') && req.body.summary.trim() !== '') {
&& req.body.hasOwnProperty('title') && title.trim() !== ''
&& req.body.hasOwnProperty('summary') && summary.trim() !== '') {
const { book } = req.files;
const { title, author, summary, contributor } = req.body;
const fileType = book.name.substr(book.name.lastIndexOf('.'));
this.addBook({ book, title, author, summary, contributor, fileType }, () => {
const messageBox = this.fillTemplate('./templates/elements/messageBox.html', {
@ -78,7 +77,7 @@ function Server () {
isActive: 'is-active',
content: messageBox,
});
const body = this.fillTemplate('./templates/pages/uploadForm.html');
const body = this.fillTemplate('./templates/pages/uploadForm.html', { resourcePath });
const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal });
res.send(html);
}, (err) => {
@ -91,7 +90,7 @@ function Server () {
isActive: 'is-active',
content: messageBox,
});
const body = this.fillTemplate('./templates/pages/uploadForm.html');
const body = this.fillTemplate('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor });
const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, modal });
res.send(html);
});
@ -111,7 +110,7 @@ function Server () {
header: 'Missing Required Fields',
message: errorMessage,
});
const body = this.fillTemplate('./templates/pages/uploadForm.html');
const body = this.fillTemplate('./templates/pages/uploadForm.html', { resourcePath, title, author, summary, contributor });
const html = this.fillTemplate('./templates/htmlContainer.html', { title: 'Give a Book', resourcePath, body, message });
res.send(html);
}
@ -278,6 +277,13 @@ Server.prototype.start = function () {
Server.prototype.addBook = function (uploadData = {}, success = () => {}, error = () => {}) {
const { book } = uploadData;
// If the file is too big, error out.
if (book.truncated === true) {
delete book;
return error('The file provided is too big');
}
const bookId = this.uuid4();
const bookPath = path.resolve(this.fileLocation, bookId);

View File

@ -1,30 +1,4 @@
<form action="{{resourcePath}}give" method="post" enctype="multipart/form-data">
<div class="field">
<label class="label" for="title">Book Title:</label>
<div class="control">
<input type="text" class="input" name="title" id="title">
</div>
</div>
<div class="field">
<label class="label" for="author"><span class="is-italic has-text-weight-normal has-text-grey">(Optional)</span> Book Author:</label>
<div class="control">
<input type="text" class="input" name="author" id="author">
</div>
</div>
<div class="field">
<label class="label" for="summary">Why you're sharing it:</label>
<div class="control">
<div class="help">Markdown Enabled</div>
<textarea class="textarea" name="summary" id="summary"></textarea>
</div>
</div>
<div class="field">
<label class="label" for="contributor"><span class="is-italic has-text-weight-normal has-text-grey">(Optional)</span> Your name:</label>
<div class="control">
<input type="text" class="input" name="contributor" id="contributor">
</div>
</div>
<div class="field">
<div class="file is-boxed has-name">
<label class="file-label">
@ -44,6 +18,32 @@
</div>
</div>
<div class="field">
<label class="label" for="title">Book Title:</label>
<div class="control">
<input type="text" class="input" name="title" id="title" value="{{title}}">
</div>
</div>
<div class="field">
<label class="label" for="author"><span class="is-italic has-text-weight-normal has-text-grey">(Optional)</span> Book Author:</label>
<div class="control">
<input type="text" class="input" name="author" id="author" value="{{author}}">
</div>
</div>
<div class="field">
<label class="label" for="summary">Why you're sharing it:</label>
<div class="control">
<div class="help">Markdown Enabled</div>
<textarea class="textarea" name="summary" id="summary">{{summary}}</textarea>
</div>
</div>
<div class="field">
<label class="label" for="contributor"><span class="is-italic has-text-weight-normal has-text-grey">(Optional)</span> Your name:</label>
<div class="control">
<input type="text" class="input" name="contributor" id="contributor" value="{{contributor}}">
</div>
</div>
<div class="field">
<input class="button" type="submit" value="Give Book" name="submit">
</div>