Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add max upload size to image uploader #293

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified src/components/admin/AppController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/ArticleController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/ArticleListController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/AuthorController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/AuthorListController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/EditAuthorsForm.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/ImageArchive.jsx
100644 → 100755
Empty file.
20 changes: 19 additions & 1 deletion src/components/admin/ImageController.jsx
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,17 @@ export default class ImageUploader extends BaseComponent {
handleInputChange(e) {
e.preventDefault();
const files = e.target.files;
const fileArray = _.map(files, file => ({ file, metaData: { name: file.name } }));
const tooLargeFileNames = [];
const filteredFiles = _.filter(files, file => {
if ((file.size / 1024) > 300) {
tooLargeFileNames.push(file);
return false;
}
return true;
});

const fileArray = filteredFiles.map(file => ({ file, metaData: { name: file.name } }));

let newFiles = this.state.files.concat(fileArray);
newFiles = _.uniqBy(newFiles, fileObject => fileObject.metaData.name);
this.safeSetState({
Expand All @@ -138,6 +148,14 @@ export default class ImageUploader extends BaseComponent {
this.addImagePreviewUrl(file);
});

if (tooLargeFileNames.length !== 0) {
let names = '\n';
tooLargeFileNames.forEach((file) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, good job! But there's a smoother way we can do it, like this:

const alertMessage = tooLargeFileNames.reduce((currentMessage, filename) => {
  return `${currentMessage}\n${file.name} (${sizeCalculation) KB)`;
}, 'The following files exceeded the maximum upload size:\n');

See these docs for an explanation of the reduce function

names = names.concat(` ${file.name} (${(file.size / 1024).toFixed(2)} KB)\n`);
});
alert(`The following exceeded the maximum upload size: ${names}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just make this the following files... (adding in "files").

Also, it would generally be a lot more natural to add the newline in here instead of as the first part of the string above, so just making it size:\n${names}), but with my above approach you can do it all in one and just do alert(alertMessage) here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of alert, let's use a Material UI dialog box. This falls in line with the rest of the Admin Interface and is just a little less jarring than an alert message.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right, let's keep things pretty! I'll try not to fall into my old habits ;)

}

// Reset to "No Files Chosen" in the input element instead of saying "10 files" or so
e.target.parentNode.parentNode.parentNode.parentNode.parentNode.reset();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a more elegant way to do this. What are all the .parentNodes referring to here??

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remember correctly, it was just really hard getting a ref to it or something like that, it is without a doubt ugly though

}
Expand Down
Empty file modified src/components/admin/ImagePreview.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/ImagePreviewList.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/IssueArticleController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/IssueCategoryController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/IssueListController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/List.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/Login.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/MainIssueController.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/Navigation.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/NotFound.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/Readme.jsx
100644 → 100755
Empty file.
Empty file modified src/components/admin/SearchBar.jsx
100644 → 100755
Empty file.
Empty file.
Empty file modified src/components/admin/lib/form-field-updaters.js
100644 → 100755
Empty file.