From 167c58d127beb41e8e29630c4e018f5a476a83ba Mon Sep 17 00:00:00 2001 From: Ville Rantanen Date: Thu, 15 Mar 2018 16:04:24 +0200 Subject: [PATCH] urlo upload as js function --- code/app.py | 4 +- code/static/css/styles.css | 1 + code/static/js/scripts.js | 111 ++++++++++++++++++++++++++----------- code/templates/list.html | 4 +- code/utils/utils.py | 10 +++- 5 files changed, 92 insertions(+), 38 deletions(-) diff --git a/code/app.py b/code/app.py index 8efc49e..52c6a0e 100644 --- a/code/app.py +++ b/code/app.py @@ -184,7 +184,9 @@ def upload_url(): ) if os.path.exists(filename): file_versionize(filename) - download_url(url, filename) + ok,error = download_url(url, filename) + if not ok: + return error set_rights(filename) notify({ "recipient": get_or_none('recipient', share), diff --git a/code/static/css/styles.css b/code/static/css/styles.css index a7eaf49..8e85857 100644 --- a/code/static/css/styles.css +++ b/code/static/css/styles.css @@ -110,6 +110,7 @@ tr:nth-child(odd) { } #list_url_upload_button { float: right; + cursor: pointer; } #list_info_toggle { diff --git a/code/static/js/scripts.js b/code/static/js/scripts.js index 056ab25..019b6c6 100644 --- a/code/static/js/scripts.js +++ b/code/static/js/scripts.js @@ -8,15 +8,15 @@ function clear_text(id,defaultValue) { function index_form_enter(event) { - if (event.which || event.keyCode) { - if ((event.which == 13) || (event.keyCode == 13)) { - index_form_submit(); - } - }; + if (event.which || event.keyCode) { + if ((event.which == 13) || (event.keyCode == 13)) { + index_form_submit(); + } + }; } function index_form_submit() { - window.location = window.location + "/list/" + + window.location = window.location + "/list/" + document.getElementById("index_form_name").value; } @@ -39,25 +39,28 @@ function UploadFile(file,file_no,files_total) { return } var file_counter = "(" + (file_no + 1) + "/" + files_total + ") "; - var xhr = new XMLHttpRequest(); - if (xhr.upload) { - - var o = document.getElementById("progress"); - xhr.upload.addEventListener("progress", function(e) { - var pc = parseInt((e.loaded / e.total * 100)); - o.className = ""; - o.innerHTML = "Uploading: " + file_counter + pc + "%"; - if (pc == 100) { - o.innerHTML = "Finishing up " + file_counter + "wait ..."; - } - // upload works, hide button - document.getElementById("list_upload_button").hidden = true; - }, false); + var xhr = new XMLHttpRequest(); + if (xhr.upload) { + var o = document.getElementById("progress"); + xhr.upload.addEventListener( + "progress", + function(e) { + var pc = parseInt((e.loaded / e.total * 100)); + o.className = ""; + o.innerHTML = "Uploading: " + file_counter + pc + "%"; + if (pc == 100) { + o.innerHTML = "Finishing up " + file_counter + "wait ..."; + } + // upload works, hide button + document.getElementById("list_upload_button").hidden = true; + }, + false + ); // file received/failed - xhr.onreadystatechange = function(e) { - if (xhr.readyState == 4) { - o.className = (xhr.status == 200 ? "success" : "failure"); + xhr.onreadystatechange = function(e) { + if (xhr.readyState == 4) { + o.className = (xhr.status == 200 ? "success" : "failure"); if (xhr.status == 200) { if (file_no + 1 == files_total) { location = location; @@ -66,32 +69,74 @@ function UploadFile(file,file_no,files_total) { o.innerHTML = xhr.response; } uploadTurn += 1; - } - }; + } + }; // start upload - xhr.open("POST", document.getElementById("upload_form").action, true); - xhr.setRequestHeader("X-FILENAME", file.name); + xhr.open("POST", document.getElementById("upload_form").action, true); + xhr.setRequestHeader("X-FILENAME", file.name); var formData = new FormData(); formData.append('name', document.getElementById("list_upload_name").value); formData.append('from_gui', 'true'); formData.append('file', file); xhr.send(formData); - } + } } // file selection function FileSelectHandler(e) { - // fetch FileList object - var files = e.target.files || e.dataTransfer.files; + // fetch FileList object + var files = e.target.files || e.dataTransfer.files; uploadTurn = 0; - // process all File objects - for (var i = 0, f; f = files[i]; i++) { + // process all File objects + for (var i = 0, f; f = files[i]; i++) { UploadFile(f,i,files.length); - } + } } // Variable to stop parallel uploads var uploadTurn = -1; +function UploadURL() { + var URL = document.getElementById("list_url_upload_text").value; + var xhr = new XMLHttpRequest(); + if (xhr.upload) { + var o = document.getElementById("progress"); + xhr.upload.addEventListener( + "progress", + function(e) { + o.className = ""; + o.innerHTML = "Uploading..."; + console.log(e); + // upload works, hide button + document.getElementById("list_url_upload_button").setAttribute("disabled", "disabled"); + }, + false + ); + + // file received/failed + xhr.onreadystatechange = function(e) { + if (xhr.readyState == 4) { + o.className = (xhr.status == 200 ? "success" : "failure"); + if (xhr.status == 200) { + // refresh page at upload finish + location = location; + } else { + o.innerHTML = xhr.response; + document.getElementById("list_url_upload_button").removeAttribute("disabled"); + } + } + }; + + // start upload + xhr.open("POST", document.getElementById("url_upload_form").action, true); + var formData = new FormData(); + formData.append('name', document.getElementById("list_upload_name").value); + formData.append('from_gui', 'true'); + formData.append('url', URL); + xhr.send(formData); + + } +} + diff --git a/code/templates/list.html b/code/templates/list.html index e041589..fcc5045 100644 --- a/code/templates/list.html +++ b/code/templates/list.html @@ -26,9 +26,9 @@
- -
+ +