trashbin.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <html>
  2. <head>
  3. <title>Trash Bin</title>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0 user-scalable=no">
  6. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  7. <script type="text/javascript" src="../../script/jquery.min.js"></script>
  8. <script type="text/javascript" src="../../script/semantic/semantic.min.js"></script>
  9. <script type="text/javascript" src="../../script/ao_module.js"></script>
  10. <style>
  11. body{
  12. background-color:white;
  13. }
  14. .banner{
  15. background-color:#4287f5;
  16. height:50px;
  17. padding:12px;
  18. padding-left:20px;
  19. padding-top:16px;
  20. }
  21. #opricon{
  22. position:absolute;
  23. top:0px;
  24. right:0px;
  25. width:80px;
  26. height:80px;
  27. }
  28. .title{
  29. color:white;
  30. font-size:130%;
  31. }
  32. .content{
  33. padding:12px;
  34. }
  35. .info{
  36. margin-top:3px;
  37. width:80%;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <span class="normalview">
  43. <div class="ui menu">
  44. <div class="header item">
  45. <img class="ui middle aligned mini image" src="trashbin_img/small_icon.png" style="margin-right:8px; height:28px; width:28px;"></img> Trash Bin
  46. </div>
  47. <div class="right item">
  48. <button class="ui negative small button" onclick="clearAll();">Delete All</button>
  49. </div>
  50. </div>
  51. <div class="ui container">
  52. <div class="ui segment">
  53. <table class="ui celled table">
  54. <thead>
  55. <tr><th>Filename</th>
  56. <th>Delete Time</th>
  57. <th>Details</th>
  58. <th>Restore</th>
  59. </tr></thead>
  60. <tbody id="trashList">
  61. </tbody>
  62. </table>
  63. <div class="ui message" id="scanning">
  64. <i class="loading spinner icon"></i> Scanning for trash in all disks...
  65. </div>
  66. </div>
  67. </div>
  68. </span>
  69. <div class="confirmRemove" style="display:none;">
  70. <div class="banner">
  71. <div class="title">Move <span id="fcount">0 File</span> to Trash Bin?</div>
  72. <img id="opricon" src="trashbin_img/trashopr.png" class="ui image"></img>
  73. </div>
  74. <div class="content">
  75. <div class="info">Deleting: <span class="filename"></span></div>
  76. <div style="position:absolute; bottom:8px;right:4px;">
  77. <button class="ui small button" onclick="ao_module_close();">Cancel</button>
  78. <button class="ui primary small button" onclick="confirmDelete();">Confirm</button>
  79. </div>
  80. </div>
  81. </div>
  82. <div class="ui modal" style="overflow-y:auto; overflow-x:hidden;">
  83. <i class="close icon"></i>
  84. <div class="header filename">
  85. No File Selected
  86. </div>
  87. <div class="ui basic segment">
  88. <table class="ui very basic celled table">
  89. <thead>
  90. <tr>
  91. <th>Property</th>
  92. <th>Value</th>
  93. </tr>
  94. </thead>
  95. <tbody id="detailList">
  96. </tbody>
  97. </table>
  98. </div>
  99. <div class="actions">
  100. <div class="ui icon blue button" onclick=" $('.ui.modal').modal('hide');">
  101. OK
  102. </div>
  103. </div>
  104. </div>
  105. <script>
  106. var deletePendingFiles = ao_module_loadInputFiles();
  107. var previousTrashbinFilelist = [];
  108. var legacyMode = !('WebSocket' in window || 'MozWebSocket' in window);
  109. if (deletePendingFiles != null && deletePendingFiles.length > 0){
  110. //Handle trash treatment
  111. console.log(deletePendingFiles);
  112. ao_module_setFixedWindowSize();
  113. ao_module_setWindowSize(400,200);
  114. $(".normalview").hide();
  115. $(".confirmRemove").show();
  116. //Update graphical information
  117. var displayname = deletePendingFiles[0].filename;
  118. $("#fcount").text("1 File");
  119. if (deletePendingFiles.length > 1){
  120. displayname = displayname + " and " + (deletePendingFiles.length - 1) + " more";
  121. $("#fcount").text(deletePendingFiles.length + " Files");
  122. }
  123. $(".filename").text(displayname);
  124. }else{
  125. initList();
  126. }
  127. //Initialize the trash list
  128. function initList(){
  129. $("#trashList").html("");
  130. $("#scanning").show();
  131. if (!legacyMode){
  132. //Use WebSocket for rendering
  133. var ws = new WebSocket(getWSEndpoint() + `/system/file_system/ws/listTrash`);
  134. ws.onopen = function() {
  135. previousTrashbinFilelist = [];
  136. console.log("TrashBin WebSocket Opened");
  137. };
  138. ws.onmessage = function (evt) {
  139. var data = evt.data;
  140. var thisFile = JSON.parse(data);
  141. var filedata = encodeURIComponent(JSON.stringify(thisFile));
  142. //console.log(thisFile);
  143. $("#trashList").append(`<tr>
  144. <td>${thisFile.OriginalFilename}</td>
  145. <td>${thisFile.RemoveDate}</td>
  146. <td><button filedata="${filedata}" class="ui tiny button" onclick="showDetail(this);">Details</button></td>
  147. <td><button filedata="${filedata}" class="ui green tiny button" onclick="restore(this);">Restore</button></td>
  148. </tr>`);
  149. previousTrashbinFilelist.push(thisFile);
  150. };
  151. ws.onclose = function() {
  152. console.log("TrashBin WebSocket transfer completed");
  153. if (previousTrashbinFilelist.length == 0){
  154. $("#trashList").append(`<tr><td colspan="4"><i class="checkmark icon"></i> There are no files or folders in Trashbin.</td></tr>`);
  155. }
  156. $("#scanning").hide();
  157. };
  158. ws.onerror = function(){
  159. console.log("Open failed. Fallback to legacy mode");
  160. legacyMode = true;
  161. initList();
  162. }
  163. }else{
  164. //Use AJAX (Slower)
  165. $.get("../../system/file_system/listTrash",function(data){
  166. if (data.error !== undefined){
  167. $("#scanning").hide();
  168. alert(data.error);
  169. }else{
  170. previousTrashbinFilelist = [];
  171. for (var i =0; i < data.length; i++){
  172. var thisFile = data[i];
  173. var filedata = encodeURIComponent(JSON.stringify(thisFile));
  174. $("#trashList").append(`<tr>
  175. <td>${thisFile.OriginalFilename}</td>
  176. <td>${thisFile.RemoveDate}</td>
  177. <td><button filedata="${filedata}" class="ui tiny button" onclick="showDetail(this);">Details</button></td>
  178. <td><button filedata="${filedata}" class="ui green tiny button" onclick="restore(this);">Restore</button></td>
  179. </tr>`);
  180. previousTrashbinFilelist.push(thisFile);
  181. }
  182. if (data.length == 0){
  183. $("#trashList").append(`<tr><td colspan="4"><i class="checkmark icon"></i> There are no files or folders in Trashbin.</td></tr>`);
  184. }
  185. $("#scanning").hide();
  186. }
  187. });
  188. }
  189. }
  190. function getWSEndpoint(){
  191. //Open opeartion in websocket
  192. let protocol = "wss://";
  193. if (location.protocol !== 'https:') {
  194. protocol = "ws://";
  195. }
  196. wsControlEndpoint = (protocol + window.location.hostname + ":" + window.location.port);
  197. return wsControlEndpoint;
  198. }
  199. //Listen to the change of the trashbin
  200. setInterval(function(){
  201. $.get("../../system/file_system/listTrash",function(data){
  202. if (data.length != previousTrashbinFilelist.length){
  203. //Change of the trashbin file. Update the list
  204. initList();
  205. }
  206. });
  207. }, 30000);
  208. function showDetail(object){
  209. var filedata = JSON.parse(decodeURIComponent($(object).attr("filedata")));
  210. $(".filename").text(filedata.OriginalFilename);
  211. $("#detailList").html("");
  212. var keyMapping = {
  213. "Filename": "Storage Filename",
  214. "Filepath": "Storage Filepath",
  215. "FileExt": "File Extension",
  216. "IsDir": "Is Directory",
  217. "Filesize": "File Size",
  218. "RemoveTimestamp": "Remove Timestamp",
  219. "RemoveDate": "Remove Datetime",
  220. "OriginalPath": "Original Path",
  221. "OriginalFilename": "Original Filename"
  222. }
  223. for (var [key, value] of Object.entries(filedata)) {
  224. console.log(key, value);
  225. var displayKey = keyMapping[key];
  226. if (key == "Filesize"){
  227. value = bytesToSize(value);
  228. }
  229. $("#detailList").append(`<tr>
  230. <td>
  231. ${displayKey}
  232. </td>
  233. <td>
  234. ${value}
  235. </td>
  236. </tr>`);
  237. }
  238. $('.ui.modal').modal('show').css({"overflow-y":"auto","overflow-x": "hidden", "max-height":window.innerHeight - 30 + "px"});
  239. }
  240. function bytesToSize(bytes) {
  241. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  242. if (bytes == 0) return '0 Byte';
  243. var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
  244. return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
  245. }
  246. function clearAll(){
  247. if (confirm("Confirm deleting ALL FILE PERMANENTLY?")){
  248. $.get("../../system/file_system/clearTrash",function(data){
  249. initList();
  250. });
  251. }
  252. }
  253. function restore(obj){
  254. var filedata = JSON.parse(decodeURIComponent($(obj).attr("filedata")));
  255. var filepath = filedata.Filepath;
  256. $.ajax({
  257. url: "../../system/file_system/restoreTrash",
  258. method: "POST",
  259. data: {"src" : filepath},
  260. success: function(data){
  261. if (data.error !== undefined){
  262. alert(data.error);
  263. }else{
  264. initList();
  265. parent.refresh(undefined, true);
  266. }
  267. }
  268. })
  269. }
  270. function confirmDelete(){
  271. //Confirm delete of the files in the list
  272. var deleteFileList = [];
  273. for (var i =0; i <deletePendingFiles.length; i++){
  274. deleteFileList.push(deletePendingFiles[i].filepath);
  275. }
  276. requestCSRFToken(function(token){
  277. $.ajax({
  278. url: "../../system/file_system/fileOpr",
  279. method:"POST",
  280. data: {opr: "recycle", src: JSON.stringify(deleteFileList), csrft: token},
  281. success: function(data){
  282. if (data.error !== undefined){
  283. console.log("Delete failed! " + data.error)
  284. }else{
  285. //Delete completed. Close this window
  286. parent.refresh(undefined, true);
  287. ao_module_close();
  288. }
  289. }
  290. });
  291. });
  292. }
  293. function requestCSRFToken(callback){
  294. $.ajax({
  295. url: "../../system/csrf/new",
  296. success: function(token){
  297. callback(token);
  298. }
  299. })
  300. }
  301. </script>
  302. </body>
  303. </html>