diff --git a/amd/build/chunkupload.min.js b/amd/build/chunkupload.min.js index e1570aa..09c9f7b 100644 --- a/amd/build/chunkupload.min.js +++ b/amd/build/chunkupload.min.js @@ -1,2 +1,10 @@ -define ("local_chunkupload/chunkupload",["exports","jquery","core/str","core/notification"],function(a,b,c,d){"use strict";Object.defineProperty(a,"__esModule",{value:!0});a.init=f;b=e(b);d=e(d);function e(a){return a&&a.__esModule?a:{default:a}}function f(a,e,f,g,h,i){var o,p,q,r,s,t,u,v;q=(0,b.default)("#"+a+"_file");v=(0,b.default)("#"+a).val();var w=q.next();r=w.find(".chunkupload-filename");s=w.find(".chunkupload-progress");t=w.find(".chunkupload-icon");u=w.next();o=g;p=h;q.change(function(){l();var a=q.get(0).files[0],b=".";if(-1!==a.name.indexOf(".")){var c=a.name.split(".");b="."+c[c.length-1].toLowerCase()}if(!("*"===e||e instanceof Array&&(-1!==e.indexOf(b)||-1!==e.indexOf("*")))){q.val(null);n({key:"invalidfiletype",component:"core_repository",param:b});return}else if(-1!==f&&a.size>f){q.val(null);n({key:"errorpostmaxsize",component:"core_repository"});return}r.text(a.name);j(a)});u.on("click",function(a){l();var c={id:v},d=new XMLHttpRequest;d.open("post",o+"/local/chunkupload/deleteupload_ajax.php?"+b.default.param(c));d.send(null);r.text(i);q.val(null);a.stopPropagation()});function j(a){var c=p{setProgress(e.loaded,file.size)},xhr.onreadystatechange=()=>{if(4===xhr.readyState&&200===xhr.status){let response=JSON.parse(xhr.responseText);void 0!==response.error?notifyError(response.error):end{setProgress(e.loaded+start,file.size)},xhr.onreadystatechange=()=>{if(4===xhr.readyState&&200===xhr.status){let response=JSON.parse(xhr.responseText);void 0!==response.error?notifyError(response.error):end{reset(),_notification.default.alert("Error","Failure while uploading!","Ok")},xhr.setRequestHeader("Content-Type","application/octet-stream"),xhr.send(slice)}function reset(){setProgress(0,1),filename.text("")}function setProgress(loaded,total){loaded===total?progress.css("width","0"):progress.css("width",100*loaded/total+"%"),progressicon.prop("hidden",loaded!==total),deleteicon.prop("hidden",loaded!==total)}function notifyError(errorstring){reset(),"string"==typeof errorstring?(0,_str.get_strings)([{key:"error"},{key:"ok"}]).done((function(s){_notification.default.alert(s[0],errorstring,s[1])})).fail(_notification.default.exception):(0,_str.get_strings)([{key:"error"},errorstring,{key:"ok"}]).done((function(s){_notification.default.alert(s[0],s[1],s[2])})).fail(_notification.default.exception)}filename=parentelem.find(".chunkupload-filename"),progress=parentelem.find(".chunkupload-progress"),progressicon=parentelem.find(".chunkupload-icon"),deleteicon=parentelem.next(),wwwRoot=wwwroot,chunkSize=chunksize,fileinput.change((()=>{reset();let file=fileinput.get(0).files[0],fileextension=".";if(-1!==file.name.indexOf(".")){let splits=file.name.split(".");fileextension="."+splits[splits.length-1].toLowerCase()}return"*"===acceptedTypes||acceptedTypes instanceof Array&&(-1!==acceptedTypes.indexOf(fileextension)||-1!==acceptedTypes.indexOf("*"))?-1!==maxBytes&&file.size>maxBytes?(fileinput.val(null),void notifyError({key:"errorpostmaxsize",component:"core_repository"})):(filename.text(file.name),void startUpload(file)):(fileinput.val(null),void notifyError({key:"invalidfiletype",component:"core_repository",param:fileextension}))})),deleteicon.on("click",(event=>{reset();let params={id:token},xhr=new XMLHttpRequest;xhr.open("post",wwwRoot+"/local/chunkupload/deleteupload_ajax.php?"+_jquery.default.param(params)),xhr.send(null),filename.text(browsetext),fileinput.val(null),event.stopPropagation()}))},_jquery=_interopRequireDefault(_jquery),_notification=_interopRequireDefault(_notification)})); + +//# sourceMappingURL=chunkupload.min.js.map \ No newline at end of file diff --git a/amd/build/chunkupload.min.js.map b/amd/build/chunkupload.min.js.map index fd09723..0968036 100644 --- a/amd/build/chunkupload.min.js.map +++ b/amd/build/chunkupload.min.js.map @@ -1 +1 @@ -{"version":3,"sources":["../src/chunkupload.js"],"names":["init","elementid","acceptedTypes","maxBytes","wwwroot","chunksize","browsetext","wwwRoot","chunkSize","fileinput","filename","progress","progressicon","deleteicon","token","val","parentelem","next","find","change","reset","file","get","files","fileextension","name","indexOf","splits","split","length","toLowerCase","Array","notifyError","key","component","param","size","text","startUpload","on","event","params","id","xhr","XMLHttpRequest","open","$","send","stopPropagation","end","start","slice","upload","onprogress","e","setProgress","loaded","onreadystatechange","readyState","status","response","JSON","parse","responseText","error","proceedUpload","setRequestHeader","onerror","notification","alert","total","css","prop","errorstring","done","s","fail","exception"],"mappings":"qLAsBA,OAEA,O,mDAWO,QAASA,CAAAA,CAAT,CAAcC,CAAd,CAAyBC,CAAzB,CAAwCC,CAAxC,CAAkDC,CAAlD,CAA2DC,CAA3D,CAAsEC,CAAtE,CAAkF,IACjFC,CAAAA,CADiF,CAEjFC,CAFiF,CAIjFC,CAJiF,CAItEC,CAJsE,CAI5DC,CAJ4D,CAIlDC,CAJkD,CAIpCC,CAJoC,CAMjFC,CANiF,CAQrFL,CAAS,CAAG,cAAE,IAAMR,CAAN,CAAkB,OAApB,CAAZ,CACAa,CAAK,CAAG,cAAE,IAAMb,CAAR,EAAmBc,GAAnB,EAAR,CACA,GAAIC,CAAAA,CAAU,CAAGP,CAAS,CAACQ,IAAV,EAAjB,CACAP,CAAQ,CAAGM,CAAU,CAACE,IAAX,CAAgB,uBAAhB,CAAX,CACAP,CAAQ,CAAGK,CAAU,CAACE,IAAX,CAAgB,uBAAhB,CAAX,CACAN,CAAY,CAAGI,CAAU,CAACE,IAAX,CAAgB,mBAAhB,CAAf,CACAL,CAAU,CAAGG,CAAU,CAACC,IAAX,EAAb,CACAV,CAAO,CAAGH,CAAV,CACAI,CAAS,CAAGH,CAAZ,CACAI,CAAS,CAACU,MAAV,CAAiB,UAAM,CACnBC,CAAK,GADc,GAEfC,CAAAA,CAAI,CAAGZ,CAAS,CAACa,GAAV,CAAc,CAAd,EAAiBC,KAAjB,CAAuB,CAAvB,CAFQ,CAGfC,CAAa,CAAG,GAHD,CAInB,GAA+B,CAAC,CAA5B,GAAAH,CAAI,CAACI,IAAL,CAAUC,OAAV,CAAkB,GAAlB,CAAJ,CAAmC,CAC/B,GAAI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,UAAaS,SANpB,CAOH,CARD,IAQO,CACH,kBAAW,CACP,CAAC5C,GAAG,CAAE,OAAN,CADO,CAEPwC,CAFO,CAGP,CAACxC,GAAG,CAAE,IAAN,CAHO,CAAX,EAIGyC,IAJH,CAIQ,SAASC,CAAT,CAAY,CACZP,UAAaC,KAAb,CAAmBM,CAAC,CAAC,CAAD,CAApB,CAAyBA,CAAC,CAAC,CAAD,CAA1B,CAA+BA,CAAC,CAAC,CAAD,CAAhC,CACH,CANL,EAOEC,IAPF,CAOOR,UAAaS,SAPpB,CAQH,CACJ,CACJ,C","sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Manage the upload in chunks.\n *\n * @module local_chunkupload\n * @copyright 2020 Justus Dieckmann WWU\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport $ from 'jquery';\nimport {get_strings as getStrings} from 'core/str';\nimport notification from 'core/notification';\n\n/**\n * Init\n * @param {String} elementid string The id of the input element\n * @param {String|String[]} acceptedTypes The accepted Types\n * @param {int} maxBytes The maximal allowed amount of bytes\n * @param {string} wwwroot The wwwroot\n * @param {int} chunksize The chunksize in bytes\n * @param {string} browsetext Text to display when no file is uploaded.\n */\nexport function init(elementid, acceptedTypes, maxBytes, wwwroot, chunksize, browsetext) {\n let wwwRoot,\n chunkSize;\n\n let fileinput, filename, progress, progressicon, deleteicon;\n\n let token;\n\n fileinput = $('#' + elementid + \"_file\");\n token = $('#' + elementid).val();\n let parentelem = fileinput.next();\n filename = parentelem.find('.chunkupload-filename');\n progress = parentelem.find('.chunkupload-progress');\n progressicon = parentelem.find('.chunkupload-icon');\n deleteicon = parentelem.next();\n wwwRoot = wwwroot;\n chunkSize = chunksize;\n fileinput.change(() => {\n reset();\n let file = fileinput.get(0).files[0];\n let fileextension = \".\";\n if (file.name.indexOf(\".\") !== -1) {\n let splits = file.name.split(\".\");\n fileextension = \".\" + splits[splits.length - 1].toLowerCase();\n }\n if (!(acceptedTypes === '*' ||\n acceptedTypes instanceof Array && (acceptedTypes.indexOf(fileextension) !== -1 || acceptedTypes.indexOf('*') !== -1))) {\n fileinput.val(null);\n notifyError({key: 'invalidfiletype', component: 'core_repository', param: fileextension});\n return;\n } else if (maxBytes !== -1 && file.size > maxBytes) {\n fileinput.val(null);\n notifyError({key: 'errorpostmaxsize', component: 'core_repository'});\n return;\n }\n filename.text(file.name);\n startUpload(file);\n });\n\n deleteicon.on('click', (event) => {\n reset();\n let params = {\n id: token\n };\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/deleteupload_ajax.php?\" + $.param(params));\n xhr.send(null);\n filename.text(browsetext);\n fileinput.val(null);\n event.stopPropagation();\n });\n\n /**\n * Start the Upload\n * @param {File} file The File to upload.\n */\n function startUpload(file) {\n let end = chunkSize < file.size ? chunkSize : file.size;\n let params = {\n start: 0,\n end: end,\n length: file.size,\n filename: file.name,\n id: token\n };\n let slice = file.slice(0, end);\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/startupload_ajax.php?\" + $.param(params));\n xhr.upload.onprogress = (e) => {\n setProgress(e.loaded, file.size);\n };\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4) {\n if (xhr.status === 200) {\n let response = JSON.parse(xhr.responseText);\n if (response.error !== undefined) {\n notifyError(response.error);\n } else {\n if (end < file.size) {\n proceedUpload(file, chunkSize);\n }\n }\n }\n }\n };\n xhr.setRequestHeader('Content-Type', 'application/octet-stream');\n xhr.send(slice);\n }\n\n /**\n * Proceed the upload\n * @param {File} file\n * @param {int} start from where to proceed the upload.\n */\n function proceedUpload(file, start) {\n let end = start + chunkSize < file.size ? start + chunkSize : file.size;\n let params = {\n start: start,\n end: end,\n id: token\n };\n let slice = file.slice(start, end);\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/proceedupload_ajax.php?\" + $.param(params));\n xhr.upload.onprogress = (e) => {\n setProgress(e.loaded + start, file.size);\n };\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4) {\n if (xhr.status === 200) {\n let response = JSON.parse(xhr.responseText);\n if (response.error !== undefined) {\n notifyError(response.error);\n } else {\n if (end < file.size) {\n proceedUpload(file, end);\n }\n }\n }\n }\n };\n xhr.onerror = () => {\n reset();\n // Doesn't make sense to try to fetch strings when having internet problems.\n notification.alert(\"Error\", \"Failure while uploading!\", \"Ok\");\n };\n xhr.setRequestHeader('Content-Type', 'application/octet-stream');\n xhr.send(slice);\n }\n\n /**\n * Resets the Progress and the Filepicker name.\n */\n function reset() {\n setProgress(0, 1);\n filename.text(\"\");\n }\n\n /**\n * Sets the progressbar\n * @param {int} loaded\n * @param {int} total\n */\n function setProgress(loaded, total) {\n if (loaded === total) {\n // Hide progressbar on finish.\n progress.css('width', '0');\n } else {\n progress.css('width', loaded * 100 / total + \"%\");\n }\n progressicon.prop('hidden', loaded !== total);\n deleteicon.prop('hidden', loaded !== total);\n }\n\n /**\n * Notify error\n * @param {object|string} errorstring Either Object as accepted by getString, or a string, to describe the error.\n */\n function notifyError(errorstring) {\n reset();\n if (typeof errorstring === \"string\") {\n getStrings([\n {key: 'error'},\n {key: 'ok'},\n ]).done(function(s) {\n notification.alert(s[0], errorstring, s[1]);\n }\n ).fail(notification.exception);\n } else {\n getStrings([\n {key: 'error'},\n errorstring,\n {key: 'ok'},\n ]).done(function(s) {\n notification.alert(s[0], s[1], s[2]);\n }\n ).fail(notification.exception);\n }\n }\n}"],"file":"chunkupload.min.js"} \ No newline at end of file +{"version":3,"file":"chunkupload.min.js","sources":["../src/chunkupload.js"],"sourcesContent":["// This file is part of Moodle - http://moodle.org/\n//\n// Moodle is free software: you can redistribute it and/or modify\n// it under the terms of the GNU General Public License as published by\n// the Free Software Foundation, either version 3 of the License, or\n// (at your option) any later version.\n//\n// Moodle is distributed in the hope that it will be useful,\n// but WITHOUT ANY WARRANTY; without even the implied warranty of\n// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n// GNU General Public License for more details.\n//\n// You should have received a copy of the GNU General Public License\n// along with Moodle. If not, see .\n\n/**\n * Manage the upload in chunks.\n *\n * @module local_chunkupload\n * @copyright 2020 Justus Dieckmann WWU\n * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later\n */\nimport $ from 'jquery';\nimport {get_strings as getStrings} from 'core/str';\nimport notification from 'core/notification';\n\n/**\n * Init\n * @param {String} elementid string The id of the input element\n * @param {String|String[]} acceptedTypes The accepted Types\n * @param {int} maxBytes The maximal allowed amount of bytes\n * @param {string} wwwroot The wwwroot\n * @param {int} chunksize The chunksize in bytes\n * @param {string} browsetext Text to display when no file is uploaded.\n */\nexport function init(elementid, acceptedTypes, maxBytes, wwwroot, chunksize, browsetext) {\n let wwwRoot,\n chunkSize;\n\n let fileinput, filename, progress, progressicon, deleteicon;\n\n let token;\n\n fileinput = $('#' + elementid + \"_file\");\n token = $('#' + elementid).val();\n let parentelem = fileinput.next();\n filename = parentelem.find('.chunkupload-filename');\n progress = parentelem.find('.chunkupload-progress');\n progressicon = parentelem.find('.chunkupload-icon');\n deleteicon = parentelem.next();\n wwwRoot = wwwroot;\n chunkSize = chunksize;\n fileinput.change(() => {\n reset();\n let file = fileinput.get(0).files[0];\n let fileextension = \".\";\n if (file.name.indexOf(\".\") !== -1) {\n let splits = file.name.split(\".\");\n fileextension = \".\" + splits[splits.length - 1].toLowerCase();\n }\n if (!(acceptedTypes === '*' ||\n acceptedTypes instanceof Array && (acceptedTypes.indexOf(fileextension) !== -1 || acceptedTypes.indexOf('*') !== -1))) {\n fileinput.val(null);\n notifyError({key: 'invalidfiletype', component: 'core_repository', param: fileextension});\n return;\n } else if (maxBytes !== -1 && file.size > maxBytes) {\n fileinput.val(null);\n notifyError({key: 'errorpostmaxsize', component: 'core_repository'});\n return;\n }\n filename.text(file.name);\n startUpload(file);\n });\n\n deleteicon.on('click', (event) => {\n reset();\n let params = {\n id: token\n };\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/deleteupload_ajax.php?\" + $.param(params));\n xhr.send(null);\n filename.text(browsetext);\n fileinput.val(null);\n event.stopPropagation();\n });\n\n /**\n * Start the Upload\n * @param {File} file The File to upload.\n */\n function startUpload(file) {\n let end = chunkSize < file.size ? chunkSize : file.size;\n let params = {\n start: 0,\n end: end,\n length: file.size,\n filename: file.name,\n id: token\n };\n let slice = file.slice(0, end);\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/startupload_ajax.php?\" + $.param(params));\n xhr.upload.onprogress = (e) => {\n setProgress(e.loaded, file.size);\n };\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4) {\n if (xhr.status === 200) {\n let response = JSON.parse(xhr.responseText);\n if (response.error !== undefined) {\n notifyError(response.error);\n } else {\n if (end < file.size) {\n proceedUpload(file, chunkSize);\n }\n }\n }\n }\n };\n xhr.setRequestHeader('Content-Type', 'application/octet-stream');\n xhr.send(slice);\n }\n\n /**\n * Proceed the upload\n * @param {File} file\n * @param {int} start from where to proceed the upload.\n */\n function proceedUpload(file, start) {\n let end = start + chunkSize < file.size ? start + chunkSize : file.size;\n let params = {\n start: start,\n end: end,\n id: token\n };\n let slice = file.slice(start, end);\n let xhr = new XMLHttpRequest();\n xhr.open('post', wwwRoot + \"/local/chunkupload/proceedupload_ajax.php?\" + $.param(params));\n xhr.upload.onprogress = (e) => {\n setProgress(e.loaded + start, file.size);\n };\n xhr.onreadystatechange = () => {\n if (xhr.readyState === 4) {\n if (xhr.status === 200) {\n let response = JSON.parse(xhr.responseText);\n if (response.error !== undefined) {\n notifyError(response.error);\n } else {\n if (end < file.size) {\n proceedUpload(file, end);\n }\n }\n }\n }\n };\n xhr.onerror = () => {\n reset();\n // Doesn't make sense to try to fetch strings when having internet problems.\n notification.alert(\"Error\", \"Failure while uploading!\", \"Ok\");\n };\n xhr.setRequestHeader('Content-Type', 'application/octet-stream');\n xhr.send(slice);\n }\n\n /**\n * Resets the Progress and the Filepicker name.\n */\n function reset() {\n setProgress(0, 1);\n filename.text(\"\");\n }\n\n /**\n * Sets the progressbar\n * @param {int} loaded\n * @param {int} total\n */\n function setProgress(loaded, total) {\n if (loaded === total) {\n // Hide progressbar on finish.\n progress.css('width', '0');\n } else {\n progress.css('width', loaded * 100 / total + \"%\");\n }\n progressicon.prop('hidden', loaded !== total);\n deleteicon.prop('hidden', loaded !== total);\n }\n\n /**\n * Notify error\n * @param {object|string} errorstring Either Object as accepted by getString, or a string, to describe the error.\n */\n function notifyError(errorstring) {\n reset();\n if (typeof errorstring === \"string\") {\n getStrings([\n {key: 'error'},\n {key: 'ok'},\n ]).done(function(s) {\n notification.alert(s[0], errorstring, s[1]);\n }\n ).fail(notification.exception);\n } else {\n getStrings([\n {key: 'error'},\n errorstring,\n {key: 'ok'},\n ]).done(function(s) {\n notification.alert(s[0], s[1], s[2]);\n }\n ).fail(notification.exception);\n }\n }\n}"],"names":["elementid","acceptedTypes","maxBytes","wwwroot","chunksize","browsetext","wwwRoot","chunkSize","fileinput","filename","progress","progressicon","deleteicon","token","val","parentelem","next","startUpload","file","end","size","params","start","length","name","id","slice","xhr","XMLHttpRequest","open","$","param","upload","onprogress","e","setProgress","loaded","onreadystatechange","readyState","status","response","JSON","parse","responseText","undefined","error","notifyError","proceedUpload","setRequestHeader","send","onerror","reset","alert","text","total","css","prop","errorstring","key","done","s","fail","notification","exception","find","change","get","files","fileextension","indexOf","splits","split","toLowerCase","Array","component","on","event","stopPropagation"],"mappings":";;;;;;;oFAmCqBA,UAAWC,cAAeC,SAAUC,QAASC,UA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