]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - js/util.js
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into inline-comments
[quix0rs-gnu-social.git] / js / util.js
index 40fffa5d996b364e6d0d067e9bfce34e9d689a53..19992646e4883ddabc4381261a6a8b35a9a2a053 100644 (file)
@@ -857,8 +857,16 @@ var SN = { // StatusNet
          */
         NoticeDataAttach: function() {
             NDA = $('#'+SN.C.S.NoticeDataAttach);
-            NDA.change(function() {
-                S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
+            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 = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+filename+'</code> <button class="close">&#215;</button></div>';
                 NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
                 if (NDAS.length > 0) {
                     NDAS.replaceWith(S);
@@ -881,6 +889,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.
@@ -1357,6 +1381,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;
+                    }
+                }
+            });
         }
     }
 };
@@ -1369,6 +1419,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();
     }