Goal
In Classic UI of AEM 6 SP1 Restrict Upload of Large Files (or file sizes that do not fall in a min-max range)
Large Uploads using Drag and Drop is currently not restricted... wip
Please leave a comment if you find bug, have fix...
Demo | Package Install
Upload File Limit Set to 2MB
Error shown when > 2 MB files are uploaded
Upload File Limit Set to Min 2KB, Max 2 MB
Error shown when > 2 MB or < 2 KB files are uploaded
Solution
1) Login to CRXDE Lite (http://localhost:4502/crx/de) and create folder /apps/classic-ui-dam-set-upload-limit
2) Create node /apps/classic-ui-dam-set-upload-limit/clientlib of type cq:ClientLibraryFolder and add a String property categories with value cq.widgets
3) Create file (nt:file) /apps/classic-ui-dam-set-upload-limit/clientlib/js.txt and add
upload-limit.js
4) Create file (nt:file) /apps/classic-ui-dam-set-upload-limit/clientlib/upload-limit.js and add the following code.
(function(){
if(window.location.pathname !== "/damadmin"){
return;
}
//id set in /libs/wcm/core/content/damadmin
var DAM_ADMIN_ID = "cq-damadmin";
var CREATE_FILE_ICON = "cq-damadmin-create-file-icon";
var PROP_ALLOWED_FILE_SIZE_BYTES = "allowedFileSizeBytes";
var addFileSizeHandler = function(button){
var attach = function(uploadWin, afsb){
var isRange = afsb.indexOf("[") !== -1, min, max;
if(isRange){
afsb = afsb.replace("[", "").replace("]", "");
min = parseInt(afsb.substring(0,afsb.lastIndexOf("-")), 10);
max = parseInt(afsb.substring(afsb.lastIndexOf("-") + 1), 10);
}else{
min = 0;
max = parseInt(afsb, 10);
}
uploadWin.on('fileselected', function(uploadField, files){
var message = "", errorFiles = [];
for (var i = 0; i < files.length; i++) {
if(files[i].size > max){
message = message + "File " + files[i].name
+ " size must be less than " + max + " bytes (" + (max/1024) + " kb)" + "<br>";
errorFiles.push(files[i].name);
}else if(files[i].size < min){
message = message + "File " + files[i].name
+ " size must be greater than " + min + " bytes (" + (min/1024) + " kb)" + "<br>";
errorFiles.push(files[i].name);
}
}
if(errorFiles.length == 0){
return;
}
CQ.Ext.Msg.alert("Error", message, function(){
var uploadFields = uploadWin.findByType("html5fileuploadfield");
for (var i = 0; i < uploadFields.length; i++) {
if(!uploadFields[i].file){
continue;
}
if(errorFiles.indexOf(uploadFields[i].file.name) != -1){
uploadWin.onFileRemoved(uploadFields[i].file.name);
uploadFields[i].clearFile();
}
}
});
});
};
button.on("click", function(){
var wMgr = CQ.Ext.WindowMgr, uWin;
var W_INTERVAL = setInterval(function(){
try{
wMgr.each(function(win){
if(win.xtype !== "html5uploaddialog"){
return;
}
clearInterval(W_INTERVAL);
//make sure you get the last (active) upload dialog window, if there are multiple
uWin = win;
});
CQ.Ext.Ajax.request({
url: uWin.displayPath + ".json",
success: function(response) {
var obj = $.parseJSON(response.responseText);
if(!obj[PROP_ALLOWED_FILE_SIZE_BYTES]){
return;
}
attach(uWin, obj[PROP_ALLOWED_FILE_SIZE_BYTES].trim());
}
});
}catch(err){}
}, 250);
});
};
var addToNewButton = function(grid){
var toolBar = grid.getTopToolbar();
var newMenu = toolBar.findBy(function(comp){
return comp["iconCls"] == CREATE_FILE_ICON;
}, toolBar);
if(!newMenu || newMenu.length == 0){
return;
}
addFileSizeHandler(newMenu[0]);
var newFileButton = newMenu[0].menu.findBy(function(comp){
return comp["iconCls"] == CREATE_FILE_ICON;
}, toolBar);
if(!newFileButton || newFileButton.length == 0){
return;
}
addFileSizeHandler(newFileButton[0]);
};
var INTERVAL = setInterval(function(){
var grid = CQ.Ext.getCmp(DAM_ADMIN_ID + "-grid");
if(!grid){
return;
}
clearInterval(INTERVAL);
addToNewButton(grid);
}, 250);
})();