trashbin.html 16 KB

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