X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;ds=sidebyside;f=js%2Futil.js;h=56ecdbcb830d8cfbb0007eb69f0fed59e57ba528;hb=99db745f9d8746e3170bf779746d855c44f0b951;hp=eace1778e203362cc84980a6cc1b925abf06eaa0;hpb=6e894c010fc0e7ddaaafa8795634d6343019aafb;p=quix0rs-gnu-social.git diff --git a/js/util.js b/js/util.js index eace1778e2..56ecdbcb83 100644 --- a/js/util.js +++ b/js/util.js @@ -718,8 +718,16 @@ var SN = { // StatusNet */ NoticeDataAttach: function() { NDA = $('#'+SN.C.S.NoticeDataAttach); - NDA.change(function() { - S = '
'+$(this).val()+'
'; + NDA.change(function(event) { + var filename = $(this).val(); + if (!filename) { + // No file -- we've been tricked! + $('#'+SN.C.S.NoticeDataAttachSelected).remove(); + return false; + } + + // @fixme appending filename straight in is potentially unsafe + S = '
'+filename+'
'; NDAS = $('#'+SN.C.S.NoticeDataAttachSelected); if (NDAS.length > 0) { NDAS.replaceWith(S); @@ -742,6 +750,22 @@ var SN = { // StatusNet }); }, + /** + * Get PHP's MAX_FILE_SIZE setting for this form; + * used to apply client-side file size limit checks. + * + * @param {jQuery} form + * @return int max size in bytes; 0 or negative means no limit + */ + maxFileSize: function(form) { + var max = $(form).find('input[name=MAX_FILE_SIZE]').attr('value'); + if (max) { + return parseInt(max); + } else { + return 0; + } + }, + /** * For browsers with FileAPI support: make a thumbnail if possible, * and append it into the attachment display widget. @@ -1217,6 +1241,32 @@ var SN = { // StatusNet SN.U.StatusNetInstance.Set({Nickname: $('#form_login #nickname').val()}); return true; }); + }, + + /** + * Add logic to any file upload forms to handle file size limits, + * on browsers that support basic FileAPI. + */ + UploadForms: function () { + $('input[type=file]').change(function(event) { + if (typeof this.files == "object" && this.files.length > 0) { + var size = 0; + for (var i = 0; i < this.files.length; i++) { + size += this.files[i].size; + } + + var max = SN.U.maxFileSize($(this.form)); + if (max > 0 && size > max) { + var msg = 'File too large: maximum upload size is %d bytes.'; + alert(msg.replace('%d', max)); + + // Clear the files. + $(this).val(''); + event.preventDefault(); + return false; + } + } + }); } } }; @@ -1229,6 +1279,7 @@ var SN = { // StatusNet * don't start them loading until after DOM-ready time! */ $(document).ready(function(){ + SN.Init.UploadForms(); if ($('.'+SN.C.S.FormNotice).length > 0) { SN.Init.NoticeForm(); }