diff --git a/media/js/utils.js b/media/js/utils.js index 584b745cfa..f168b9dcb1 100644 --- a/media/js/utils.js +++ b/media/js/utils.js @@ -80,8 +80,7 @@ function filesizeformat(bytes, precision) var gigabyte = megabyte * 1024; var terabyte = gigabyte * 1024; - if (precision === undefined) - precision = 0; + precision = precision || 0; if ((bytes >= 0) && (bytes < kilobyte)) { return bytes + ' B'; @@ -106,3 +105,25 @@ function filesizeformat(bytes, precision) function e(str) { return encodeURIComponent(str); } + +function prepareCSRFToken(xhr, settings) { + function getCookie(name) { + var cookieValue = null; + if (document.cookie && document.cookie != '') { + var cookies = document.cookie.split(';'); + for (var i = 0; i < cookies.length; i++) { + var cookie = jQuery.trim(cookies[i]); + // Does this cookie string begin with the name we want? + if (cookie.substring(0, name.length + 1) == (name + '=')) { + cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); + break; + } + } + } + return cookieValue; + } + if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { + // Only send the token to relative URLs i.e. locally. + xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); + } +} diff --git a/templates/myhome.html b/templates/myhome.html index 82eeb3aabb..8b0d167648 100644 --- a/templates/myhome.html +++ b/templates/myhome.html @@ -287,37 +287,13 @@ $('#repo-create-submit').click(function() { } } - // prepare django csrf token - $.ajaxSetup({ - beforeSend: function(xhr, settings) { - function getCookie(name) { - var cookieValue = null; - if (document.cookie && document.cookie != '') { - var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { - var cookie = jQuery.trim(cookies[i]); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) == (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; - } - if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { - // Only send the token to relative URLs i.e. locally. - xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); - } - } - }); - $.ajax({ url: '{{ SITE_ROOT }}repo/create/', type: 'POST', dataType: 'json', cache: 'false', contentType: 'application/json; charset=utf-8', + beforeSend: prepareCSRFToken, data: { 'repo_name': $('#repo-name').val(), 'repo_desc': $('#repo-desc').val(), diff --git a/templates/repo_view_file.html b/templates/repo_view_file.html index cad181ea62..355f574701 100644 --- a/templates/repo_view_file.html +++ b/templates/repo_view_file.html @@ -135,7 +135,6 @@ $('#rm-shared-link').click(function() { }); $('#send-shared-link').click(function() { - $('#error, #sending, #success').attr('class', 'hide'); $("#link-send-form").modal({appendTo: "#main"}); }); @@ -155,36 +154,13 @@ $("#link-send-form").submit(function(event) { $('#simplemodal-container').css('height', $('#link-send-form').height()); if (email.length <= 512) { - // prepare django csrf token - $.ajaxSetup({ - beforeSend: function(xhr, settings) { - function getCookie(name) { - var cookieValue = null; - if (document.cookie && document.cookie != '') { - var cookies = document.cookie.split(';'); - for (var i = 0; i < cookies.length; i++) { - var cookie = jQuery.trim(cookies[i]); - // Does this cookie string begin with the name we want? - if (cookie.substring(0, name.length + 1) == (name + '=')) { - cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); - break; - } - } - } - return cookieValue; - } - if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) { - // Only send the token to relative URLs i.e. locally. - xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); - } - } - }); $.ajax({ type: "POST", url: "{{ SITE_ROOT }}sharedlink/send/", dataType: 'json', cache: false, contentType: 'application/json; charset=utf-8', + beforeSend: prepareCSRFToken, data: {file_shared_link: file_shared_link, email: email}, success: function(data) { $('#sending').attr('class', 'hide');