X-Git-Url: https://git.p6c8.net/jirafeau_mojo42.git/blobdiff_plain/f89474388394d7a7fd7dbb41f089956454e4957f..c5571d92378b2dc0529d05b8f6ad3349b2d128cc:/lib/functions.js diff --git a/lib/functions.js b/lib/functions.js old mode 100755 new mode 100644 index fee0b50..f25ee6f --- a/lib/functions.js +++ b/lib/functions.js @@ -1,6 +1,6 @@ /* * Jirafeau, your web file repository - * Copyright (C) 2012 Jerome Jutteau + * Copyright (C) 2015 Jerome Jutteau * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -16,15 +16,81 @@ * along with this program. If not, see . */ -function show_link (url, reference, delete_code, date) +function show_link (url, reference, delete_code, crypt_key, date) { - var download_link = url + 'file.php?h=' + reference; - var delete_link = download_link + '&d=' + delete_code; - var delete_link_href = download_link + '&d=' + delete_code; - document.getElementById('upload_link').innerHTML = download_link; - document.getElementById('upload_link').href = download_link; + // Download page if element exists + var download_link = url + 'f.php?h=' + reference; + var download_link_href = url + 'f.php?h=' + reference; + if (crypt_key.length > 0) + { + download_link += '&k=' + crypt_key; + download_link_href += '&k=' + crypt_key; + } + if (!!document.getElementById('upload_finished_download_page')) + { + document.getElementById('upload_link').innerHTML = download_link; + document.getElementById('upload_link').href = download_link_href; + } + + // Is the preview allowed ? + if (!!document.getElementById('preview_link')) + { + document.getElementById('upload_finished_preview').style.display = 'none'; + var preview_link = url + 'f.php?h=' + reference + '&p=1'; + var preview_link_href = url + 'f.php?h=' + reference + '&p=1'; + if (crypt_key.length > 0) + { + preview_link += '&k=' + crypt_key; + preview_link_href += '&k=' + crypt_key; + } + + // Test if content can be previewed + type = document.getElementById('file_select').files[0].type; + if (type.indexOf("image") > -1 || + type.indexOf("audio") > -1 || + type.indexOf("text") > -1 || + type.indexOf("video") > -1) + { + document.getElementById('preview_link').innerHTML = preview_link; + document.getElementById('preview_link').href = preview_link_href; + document.getElementById('upload_finished_preview').style.display = ''; + } + } + + // Only show link to password page if password is set + document.getElementById('upload_password_page').style.display = 'none'; + if (document.getElementById('input_key').value.length > 0) + { + if (!!document.getElementById('upload_finished_download_page')) + document.getElementById('upload_finished_download_page').style.display = 'none'; + document.getElementById('upload_password_page').style.display = ''; + if (!!document.getElementById('upload_finished_preview')) + document.getElementById('upload_finished_preview').style.display = 'none'; + document.getElementById('upload_direct_download').style.display = 'none'; + + document.getElementById('password_link').innerHTML = download_link; + document.getElementById('password_link').href = download_link_href; + } + // Direct download link + else + { + var direct_download_link = url + 'f.php?h=' + reference + '&d=1'; + var direct_download_link_href = url + 'f.php?h=' + reference + '&d=1'; + if (crypt_key.length > 0) + { + direct_download_link += '&k=' + crypt_key; + direct_download_link_href += '&k=' + crypt_key; + } + document.getElementById('direct_link').innerHTML = direct_download_link; + document.getElementById('direct_link').href = direct_download_link_href; + } + + // Delete link + var delete_link = url + 'f.php?h=' + reference + '&d=' + delete_code; + var delete_link_href = url + 'f.php?h=' + reference + '&d=' + delete_code; document.getElementById('delete_link').innerHTML = delete_link; document.getElementById('delete_link').href = delete_link_href; + if (date) { document.getElementById('date').innerHTML = date; @@ -36,6 +102,7 @@ function show_link (url, reference, delete_code, date) document.getElementById('uploading').style.display = 'none'; document.getElementById('upload').style.display = 'none'; document.getElementById('upload_finished').style.display = ''; + document.title = 'Jirafeau - 100%'; } function show_upload_progression (p) @@ -51,29 +118,59 @@ function upload_progress (e) /* Show the user the operation do not reach 100%, the server need time * to give a response before providing the link. */ - var p = Math.round (e.loaded * 99 / e.total); - show_upload_progression (p.toString() + '%'); + var p = Math.round (e.loaded * 100 / e.total); + if (p == 100) + show_upload_progression (' '); + else + show_upload_progression (p.toString() + '%'); } -function upload_failed (e) +function control_selected_file_size(max_size, error_str) { - /* Todo: Considere showing a error div. */ - alert ('Sorry, upload failed'); + f_size = document.getElementById('file_select').files[0].size; + if (max_size > 0 && f_size > max_size * 1024 * 1024) + { + pop_failure(error_str); + document.getElementById('send').style.display = 'none'; + } + else + { + document.getElementById('options').style.display = ''; + document.getElementById('send').style.display = ''; + document.getElementById('error_pop').style.display = 'none'; + } } -function classic_upload (url, file, time, password, one_time) +function pop_failure (e) +{ + var text = "An error occured"; + if (typeof e !== 'undefined') + text = e; + text = "

" + text + "

"; + document.getElementById('error_pop').innerHTML = e; + + document.getElementById('uploading').style.display = 'none'; + document.getElementById('error_pop').style.display = ''; + document.getElementById('upload').style.display = ''; + document.getElementById('send').style.display = ''; +} + +function classic_upload (url, file, time, password, one_time, upload_password) { var req = new XMLHttpRequest (); req.upload.addEventListener ("progress", upload_progress, false); - req.addEventListener ("error", upload_failed, false); - req.addEventListener ("abort", upload_failed, false); + req.addEventListener ("error", pop_failure, false); + req.addEventListener ("abort", pop_failure, false); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { var res = req.responseText; if (res == "Error") + { + pop_failure (); return; + } res = res.split ("\n"); if (time != 'none') { @@ -88,12 +185,14 @@ function classic_upload (url, file, time, password, one_time) d.setSeconds (d.getSeconds() + 604800); else if (time == 'month') d.setSeconds (d.getSeconds() + 2419200); + else if (time == 'year') + d.setSeconds (d.getSeconds() + 29030400); else return; - show_link (url, res[0], res[1], d.toString()); + show_link (url, res[0], res[1], res[2], d.toString()); } else - show_link (url, res[0], res[1]); + show_link (url, res[0], res[1], res[2]); } } req.open ("POST", url + 'script.php' , true); @@ -106,6 +205,9 @@ function classic_upload (url, file, time, password, one_time) form.append ("key", password); if (one_time) form.append ("one_time_download", '1'); + if (upload_password.length > 0) + form.append ("upload_password", upload_password); + req.send (form); } @@ -124,7 +226,7 @@ var async_global_max_size = 0; var async_global_time; var async_global_transfering = 0; -function async_upload_start (url, max_size, file, time, password, one_time) +function async_upload_start (url, max_size, file, time, password, one_time, upload_password) { async_global_transfered = 0; async_global_url = url; @@ -133,15 +235,18 @@ function async_upload_start (url, max_size, file, time, password, one_time) async_global_time = time; var req = new XMLHttpRequest (); - req.addEventListener ("error", upload_failed, false); - req.addEventListener ("abort", upload_failed, false); + req.addEventListener ("error", pop_failure, false); + req.addEventListener ("abort", pop_failure, false); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { var res = req.responseText; if (res == "Error") + { + pop_failure (); return; + } res = res.split ("\n"); async_global_ref = res[0]; var code = res[1]; @@ -159,6 +264,9 @@ function async_upload_start (url, max_size, file, time, password, one_time) form.append ("key", password); if (one_time) form.append ("one_time_download", '1'); + if (upload_password.length > 0) + form.append ("upload_password", upload_password); + req.send (form); } @@ -166,8 +274,11 @@ function async_upload_progress (e) { if (!e.lengthComputable && async_global_file.size != 0) return; - var p = Math.round ((e.loaded + async_global_transfered) * 99 / (async_global_file.size)); - show_upload_progression (p.toString() + '%'); + var p = Math.round ((e.loaded + async_global_transfered) * 100 / (async_global_file.size)); + if (p == 100) + show_upload_progression (' '); + else + show_upload_progression (p.toString() + '%'); } function async_upload_push (code) @@ -179,15 +290,18 @@ function async_upload_push (code) } var req = new XMLHttpRequest (); req.upload.addEventListener ("progress", async_upload_progress, false); - req.addEventListener ("error", upload_failed, false); - req.addEventListener ("abort", upload_failed, false); + req.addEventListener ("error", pop_failure, false); + req.addEventListener ("abort", pop_failure, false); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { var res = req.responseText; if (res == "Error") + { + pop_failure (); return; + } res = res.split ("\n"); var code = res[0] async_global_transfered = async_global_transfering; @@ -196,7 +310,7 @@ function async_upload_push (code) } req.open ("POST", async_global_url + 'script.php?push_async' , true); - var chunk_size = parseInt (async_global_max_size * 0.90); + var chunk_size = parseInt (async_global_max_size * 0.50); var start = async_global_transfered; var end = start + chunk_size; if (end >= async_global_file.size) @@ -214,15 +328,18 @@ function async_upload_push (code) function async_upload_end (code) { var req = new XMLHttpRequest (); - req.addEventListener ("error", upload_failed, false); - req.addEventListener ("abort", upload_failed, false); + req.addEventListener ("error", pop_failure, false); + req.addEventListener ("abort", pop_failure, false); req.onreadystatechange = function () { if (req.readyState == 4 && req.status == 200) { var res = req.responseText; if (res == "Error") + { + pop_failure (); return; + } res = res.split ("\n"); if (async_global_time != 'none') { @@ -237,12 +354,14 @@ function async_upload_end (code) d.setSeconds (d.getSeconds() + 604800); else if (async_global_time == 'month') d.setSeconds (d.getSeconds() + 2419200); + else if (async_global_time == 'year') + d.setSeconds (d.getSeconds() + 29030400); else return; - show_link (async_global_url, res[0], res[1], d.toString()); + show_link (async_global_url, res[0], res[1], res[2], d.toString()); } else - show_link (async_global_url, res[0], res[1]); + show_link (async_global_url, res[0], res[1], res[2]); } } req.open ("POST", async_global_url + 'script.php?end_async' , true); @@ -263,7 +382,8 @@ function upload (url, max_size) document.getElementById('file_select').files[0], document.getElementById('select_time').value, document.getElementById('input_key').value, - document.getElementById('one_time_download').checked + document.getElementById('one_time_download').checked, + document.getElementById('upload_password').value ); } else @@ -272,7 +392,8 @@ function upload (url, max_size) document.getElementById('file_select').files[0], document.getElementById('select_time').value, document.getElementById('input_key').value, - document.getElementById('one_time_download').checked + document.getElementById('one_time_download').checked, + document.getElementById('upload_password').value ); } }