Skip to content

Commit

Permalink
SAK-33512 Added image upload size check
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianfish authored and Miguel Pellicer committed Oct 16, 2018
1 parent b6bbdfd commit c5967d1
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ link_to = Link to:
load_thumbnail = Load thumbnail
link_button_tooltip = Insert a link
image_button_tooltip = Insert an image
choose_image_file = Choose an image file. It must be a png, gif, or a jpeg:
choose_image_file = Choose an image file. It must be a png, gif, or a jpeg, no bigger than {max}MB.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.sakaiproject.commons.api.CommonsManager;
import org.sakaiproject.commons.api.SakaiProxy;
import org.sakaiproject.component.api.ComponentManager;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.tool.api.Session;
import org.sakaiproject.util.RequestFilter;
import org.sakaiproject.util.ResourceLoader;
Expand All @@ -42,6 +43,7 @@ public class CommonsTool extends HttpServlet {

private CommonsManager commonsManager;
private SakaiProxy sakaiProxy;
private ServerConfigurationService serverConfigurationService;

public void init(ServletConfig config) throws ServletException {

Expand All @@ -53,6 +55,7 @@ public void init(ServletConfig config) throws ServletException {
ComponentManager componentManager = org.sakaiproject.component.cover.ComponentManager.getInstance();
sakaiProxy = (SakaiProxy) componentManager.get(SakaiProxy.class);
commonsManager = (CommonsManager) componentManager.get(CommonsManager.class);
serverConfigurationService = (ServerConfigurationService) componentManager.get(ServerConfigurationService.class);
} catch (Throwable t) {
throw new ServletException("Failed to initialise CommonsTool servlet.", t);
}
Expand Down Expand Up @@ -111,6 +114,8 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
request.setAttribute("isUserSite", isUserSite);
request.setAttribute("embedder", isUserSite ? CommonsConstants.SOCIAL : CommonsConstants.SITE);
request.setAttribute("commonsId", isUserSite ? CommonsConstants.SOCIAL : siteId);
String maxUploadSize = serverConfigurationService.getString("content.upload.max", "20");
request.setAttribute("maxUploadSize", maxUploadSize);

String pathInfo = request.getPathInfo();

Expand Down
3 changes: 2 additions & 1 deletion commons/tool/src/webapp/WEB-INF/bootstrap.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
embedder: '${embedder}',
userId: '${userId}',
isUserSite: ${isUserSite},
postId: '${postId}'
postId: '${postId}',
maxUploadSize: '${maxUploadSize}'
};
</script>
Expand Down
3 changes: 2 additions & 1 deletion commons/tool/src/webapp/WEB-INF/templates/posts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@
<div class="commons-dialog-container">
<div id="commons-link-dialog-table">
<div class="commons-link-dialog-row">
<label for="commons-image-dialog-file" id="commons-imagefile-instruction">{{tr 'commons' 'choose_image_file'}}</label>
<label for="commons-image-dialog-file" id="commons-imagefile-instruction">{{tr 'commons' 'choose_image_file' max=maxUploadSize}}</label>
<input type="file" id="commons-image-dialog-file" accept=".png,.jpg,.jpeg,.gif,image/png,image/jpeg,image/jpg,image/gif" />
</div>
</div>
<button id="commons-image-dialog-insert-button" disabled="true">{{tr 'commons' 'insert'}}</button>
<button id="commons-image-dialog-cancel-button">{{tr 'commons' 'cancel'}}</button>
<span id="commons-image-dialog-message"></span>
</div>
</div>
13 changes: 11 additions & 2 deletions commons/tool/src/webapp/js/commons.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ commons.switchState = function (state, arg) {

var templateData = {
currentUserId: commons.userId,
isUserSite: commons.isUserSite
isUserSite: commons.isUserSite,
maxUploadSize: commons.maxUploadSize
};

// renderPageOfPosts uses this. Set it to the start page
Expand Down Expand Up @@ -215,6 +216,7 @@ commons.switchState = function (state, arg) {

var fileInsertButton = $('#commons-image-dialog-insert-button');
var fileField = $('#commons-image-dialog-file');
var fileMessage = $('#commons-image-dialog-message');

fileInsertButton.click(function (e) {

Expand All @@ -236,12 +238,19 @@ commons.switchState = function (state, arg) {
});

fileField.change(function (e) {
fileInsertButton.prop('disabled', false);

var file = fileField[0].files[0];
if ((file.size/1000000) > parseInt(commons.maxUploadSize)) {
fileMessage.html('File too big');
} else {
fileInsertButton.prop('disabled', false);
}
});

$('#commons-image-dialog-cancel-button').click(function (e) {

fileInsertButton.prop('disabled', true);
fileMessage.html('');
fileField.val('');
editorImageButton.qtip('api').hide();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,8 @@
font-size: larger;
margin-bottom: 5px;
}
#commons-image-dialog-message {
font-weight: bold;
font-size: larger;
color: red;
}

0 comments on commit c5967d1

Please sign in to comment.