file_server.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function setpath() {
  2. var fileserverpraefix = "/fileserver";
  3. var anz_zeichen_fileserver = fileserverpraefix.length;
  4. var default_path = window.location.pathname.substring(anz_zeichen_fileserver) + document.getElementById("newfile").files[0].name;
  5. document.getElementById("filepath").value = default_path;
  6. }
  7. function dirup() {
  8. var str = window.location.href;
  9. str = str.substring(0, str.length-1);
  10. var zw = str.indexOf("/");
  11. var found = zw;
  12. while (zw >= 0) {
  13. zw = str.indexOf("/", found+1);
  14. if (zw >= 0) {
  15. found = zw;
  16. }
  17. }
  18. var res = str.substring(0, found+1);
  19. window.location.href = res;
  20. }
  21. function upload() {
  22. var filePath = document.getElementById("filepath").value;
  23. var upload_path = "/upload/" + filePath;
  24. var fileInput = document.getElementById("newfile").files;
  25. // Max size of an individual file. Make sure this value is same as that set in file_server.c
  26. var MAX_FILE_SIZE = 8000*1024;
  27. var MAX_FILE_SIZE_STR = "8000KB";
  28. if (fileInput.length == 0) {
  29. firework.launch('No file selected!', 'danger', 30000);
  30. } else if (filePath.length == 0) {
  31. firework.launch('File path on server is not set!', 'danger', 30000);
  32. } else if (filePath.length > 100) {
  33. firework.launch('Filename is to long! Max 100 characters.', 'danger', 30000);
  34. } else if (filePath.indexOf(' ') >= 0) {
  35. firework.launch('File path on server cannot have spaces!', 'danger', 30000);
  36. } else if (filePath[filePath.length-1] == '/') {
  37. firework.launch('File name not specified after path!', 'danger', 30000);
  38. } else if (fileInput[0].size > MAX_FILE_SIZE) {
  39. firework.launch("File size must be less than " + MAX_FILE_SIZE_STR + "!", 'danger', 30000);
  40. } else {
  41. document.getElementById("newfile").disabled = true;
  42. document.getElementById("filepath").disabled = true;
  43. document.getElementById("upload").disabled = true;
  44. var file = fileInput[0];
  45. var xhttp = new XMLHttpRequest();
  46. xhttp.onreadystatechange = function() {
  47. if (xhttp.readyState == 4) {
  48. if (xhttp.status == 200) {
  49. document.open();
  50. document.write(xhttp.responseText);
  51. document.close();
  52. firework.launch('File upload completed', 'success', 5000);
  53. } else if (xhttp.status == 0) {
  54. firework.launch('Server closed the connection abruptly!', 'danger', 30000);
  55. UpdatePage(false);
  56. } else {
  57. firework.launch('An error occured: ' + xhttp.responseText, 'danger', 30000);
  58. UpdatePage(false);
  59. }
  60. }
  61. };
  62. xhttp.open("POST", upload_path, true);
  63. xhttp.send(file);
  64. }
  65. }
  66. function checkAtRootLevel(res) {
  67. if (getPath() == "/fileserver/") {
  68. // Already at root level
  69. document.getElementById("dirup").disabled = true;
  70. console.log("Already on sd-card root level!");
  71. return true;
  72. }
  73. document.getElementById("dirup").disabled = false;
  74. return false;
  75. }
  76. function getPath() {
  77. return window.location.pathname.replace(/\/+$/, '') + "/"
  78. }
  79. function initFileServer() {
  80. checkAtRootLevel();
  81. console.log("Current path: " + getPath().replace("/fileserver", ""));
  82. document.getElementById("currentpath").innerHTML = "Current path: <b>" + getPath().replace("/fileserver", "") + "</b>";
  83. document.cookie = "page=" + getPath() + "; path=/";
  84. }