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

SAK-49199 Mailsender check for exceeding attachment size client side #11838

Merged
merged 1 commit into from
Jun 3, 2024

Conversation

mylescarey2019
Copy link
Contributor

Adds client side attachment file size testing vs sakai.properties mail.sendfromsakai.maxsize limit (or the default value of 25MB). If sum of attachments exceeds the limit a warning banner is presented instructing user to remove one or more file until below the limit. Along with warning banner, control to add more files is disabled.

@mylescarey2019 mylescarey2019 force-pushed the SAK-49199 branch 2 times, most recently from 9bb161a to bfbf26e Compare August 21, 2023 22:36
Copy link
Contributor

@adrianfish adrianfish left a comment

Choose a reason for hiding this comment

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

I'd like to see vanilla js used here instead of jquery. I realise jquery is used elsewhere in these files, but it is a dying library. So many of jquery's awesome utilities are now available in vanilla js. Also, try and use const more often in the JS. It makes code a lot easier to understand.

public long getUploadMax()
{
long uploadMax = (long) (serverConfigurationService.getInt(EmailService.MAIL_SENDFROMSAKAI_MAXSIZE, EmailService.DEFAULT_MAXSIZE));
return uploadMax;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just return an int here. Why cast to a long when the SCS returns an int? Also, just make it one line, you may as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

*
* @return
*/
long getUploadMax();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd just return int here. Also, I'd remove the comment and just call the method getMaxUploadFileSize. No doc needed then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

@@ -168,3 +168,16 @@ h3.insBorder {
.user-label {
font-weight: normal;
}
.disableAttachMoreIcon {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd use hyphen separation here. Our more recent css uses that convention. So, .disable-attach-more-icon

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

opacity: 0.5;
}

.disableAttachMoreLink {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same hyphen approach here, imo.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed


function formatFileSize(bytes) {
if(bytes === 0) {
return '0 Bytes';
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this be internationalised?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i];
}

function msg(s) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These s and m names could be expanded a little to make this code more understandable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed


function accumulateAndCheckAttachmentSize() {
let wasOverUploadSize = accumulatedFileSize > maxFileUploadSize ;
let isOverUploadSize = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let isOverUploadSize = false;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

accumulatedFileSize += $(this)[0].files[0].size;
});

isOverUploadSize = accumulatedFileSize > maxFileUploadSize ;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
isOverUploadSize = accumulatedFileSize > maxFileUploadSize ;
const isOverUploadSize = accumulatedFileSize > maxFileUploadSize ;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

Comment on lines 26 to 31
accumulatedFileSize = 0;

let targetFiles = $('.emailattachment').filter(function(i, ele) { return ele.files.length > 0;});
targetFiles.each(function (i) {
accumulatedFileSize += $(this)[0].files[0].size;
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
accumulatedFileSize = 0;
let targetFiles = $('.emailattachment').filter(function(i, ele) { return ele.files.length > 0;});
targetFiles.each(function (i) {
accumulatedFileSize += $(this)[0].files[0].size;
});
accumulatedFileSize = [...document.querySelectorAll(".emailattachment")].filter(el => el.files.length > 0).reduce((acc, el) => acc + el.files[0].size, 0);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

Comment on lines 36 to 39
$('#attach-size-error').hide();
$("input[type=submit]").removeAttr('disabled');
$('#attachOuter img[alt="attachment_img"]').removeClass("disableAttachMoreIcon");
$('#attachMoreLink button').removeClass("disableAttachMoreLink");
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$('#attach-size-error').hide();
$("input[type=submit]").removeAttr('disabled');
$('#attachOuter img[alt="attachment_img"]').removeClass("disableAttachMoreIcon");
$('#attachMoreLink button').removeClass("disableAttachMoreLink");
document.getElementById("attach-size-error").style.display = "none";
document.querySelectorAll("input[type=submit]").forEach(el => el.disabled = false;
document.querySelector("#attachOuter img[alt='attachment_img']").classList.remove("disableAttachMoreIcon");
document.querySelector('#attachMoreLink button').classList.remove("disableAttachMoreLink");

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

Comment on lines 1 to 2
var accumulatedFileSize = 0;
var maxFileUploadSize;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @mylescarey2019 can you use let or const here vs var?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

Copy link
Contributor

@adrianfish adrianfish left a comment

Choose a reason for hiding this comment

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

Could you move those functions into a mailsender specific scope? If you don't they could clash with other scripts in Sakai.

}
}

function emailattachmentChange() {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should move this function definition into the MailSender function so it is scoped at that level, and not at the global level. Same thing for all these functions - they are all being declared on the window or globalThis object and could be masked by other js loaded by Sakai.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion completed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@adrianfish - fyi - latest suggestions have been implemented

@mylescarey2019
Copy link
Contributor Author

@ern - fyi - latest suggestions have been implemented

@ern ern changed the title SAK-49199 Mailsender: Check for exceeding attachment size Client Side SAK-49199 Mailsender check for exceeding attachment size client side Jun 3, 2024
@ern ern merged commit dbf28af into sakaiproject:master Jun 3, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants