disk_restore.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>File Restore</title>
  5. <meta name="mobile-web-app-capable" content="yes">
  6. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  7. <meta charset="UTF-8">
  8. <link rel="stylesheet" href="../../script/semantic/semantic.min.css">
  9. <script src="../../script/jquery.min.js"></script>
  10. <script src="../../script/semantic/semantic.min.js"></script>
  11. <script type="text/javascript" src="../../script/ao_module.js"></script>
  12. <style>
  13. .hidden{
  14. display:none;
  15. }
  16. .disabled{
  17. opacity: 0.5;
  18. pointer-events: none;
  19. }
  20. .colorblock{
  21. width: 20px;
  22. height: 20px;
  23. display: inline-block;
  24. vertical-align: bottom;
  25. margin-right: 12px;
  26. }
  27. .blue.colorblock{
  28. background-color: #52bdf2;
  29. }
  30. .grey.colorblock{
  31. background-color: #b9b9b9;
  32. }
  33. .yellow.colorblock{
  34. background-color: #e5e75c;
  35. }
  36. .ui.table tr td{
  37. border-top: 0px solid transparent !important;
  38. }
  39. .overlap.bar{
  40. position: absolute !important;
  41. top:0px;
  42. left: 0px;
  43. }
  44. .nointeract{
  45. pointer-events: none;
  46. user-select: none;
  47. }
  48. #restorableList{
  49. max-height: 300px;
  50. overflow-y: scroll;
  51. padding-right: 4px;
  52. }
  53. #snapshotList{
  54. max-height: 300px;
  55. overflow-y: scroll;
  56. padding-right: 4px;
  57. }
  58. .timeleft{
  59. width: 30px;
  60. height: 30px;
  61. display: inline-block;
  62. user-select: none;
  63. vertical-align: baseline;
  64. margin-bottom: -10px;
  65. margin-right: 4px;
  66. }
  67. .closebtn{
  68. position: absolute;
  69. right: 4px;
  70. top: 4px;
  71. }
  72. </style>
  73. </head>
  74. <body>
  75. <br>
  76. <div class="ui container">
  77. <h3 class="ui header">
  78. Backup & Restore
  79. <div class="sub header">Show restore points for the selected virtual disk</div>
  80. </h3>
  81. <div class="ui divider"></div>
  82. <div id="error" class="ui red inverted segment hidden">
  83. <h4 class="ui header">
  84. <i class="remove icon"></i>
  85. <div class="content">
  86. List Restore Failed
  87. <div class="sub reason header" style="color: white;">Unknown Vroot paramter given</div>
  88. </div>
  89. </h4>
  90. </div>
  91. <div id="restoreCancel" class="ui grey inverted segment hidden">
  92. <h4 class="ui header">
  93. <i class="remove icon"></i>
  94. <div class="content">
  95. Snapshot Restore Cancelled
  96. <div class="sub reason header" style="color: white;">Operation cancelled by user</div>
  97. </div>
  98. </h4>
  99. </div>
  100. <div id="succ" class="ui green segment" style="display:none;">
  101. <h4 class="ui header">
  102. <i class="green checkmark icon"></i>
  103. <div class="content">
  104. File Restored
  105. <div class="sub reason header openfolder"><a style="cursor: pointer" onclick="handleOpenRestoreLocation()"><i class="ui folder open icon"></i> Open Location</a></div>
  106. </div>
  107. </h4>
  108. </div>
  109. <div id="fileinfo" class="ui segment" style="display:none;">
  110. <h4 class="ui header">
  111. <div class="content">
  112. <span class="filename">No File Selected</span>
  113. <div class="sub header relpath">N/A</div>
  114. </div>
  115. </h4>
  116. <div class="ui divider"></div>
  117. <h5>Restorable File Properties</h5>
  118. <span class="backupDisk">N/A</span><br>
  119. <span class="restorePoint">N/A</span><br>
  120. <span class="deleteTime">N/A</span><br>
  121. <span class="deleteRemainingTime">N/A</span><br>
  122. <button id="snapshotSummaryBtn" class="ui mini button" style="display:none;" onclick="openSnapshotInfo();">View Snapshot Summary</button>
  123. <button class="circular ui icon basic small button closebtn" onclick='$("#fileinfo").slideUp("fast");'>
  124. <i class="icon remove"></i>
  125. </button>
  126. </div>
  127. <div id="snapshotList" class="ui middle aligned divided list">
  128. <div class="item">
  129. <img class="ui mini image nointeract" src="../../img/system/drive-backup.svg">
  130. <div class="content">
  131. No Restorable Snapshot(s) for this virtual disk.
  132. </div>
  133. </div>
  134. </div>
  135. <div id="restorableList" class="ui middle aligned divided list">
  136. <div class="item">
  137. <img class="ui mini image nointeract" src="../../img/system/file-notfound.svg">
  138. <div class="content">
  139. No Restorable File(s) for this virtual disk.
  140. </div>
  141. </div>
  142. </div>
  143. <div class="ui checkbox">
  144. <input type="checkbox" name="showhidden" autocomplete="false" onchange="toggleHiddenFiles(this.checked);">
  145. <label>Show hidden / cache files</label>
  146. </div>
  147. <div class="ui divider" style="margin-top: 8px; margin-bottom: 8px;"></div>
  148. <br>
  149. <div class="ui tiny green button" onclick="restoreAll();">Restore All</div>
  150. <button class="ui right floated button" onclick="ao_module_close();">Close</button>
  151. <br>
  152. </div>
  153. <script>
  154. var vrootID = "";
  155. var viewingFileInfo = {};
  156. $(".ui.checkbox").checkbox();
  157. //Extract vroot id from the window hash
  158. if (window.location.hash.length > 1){
  159. vrootID = JSON.parse(decodeURIComponent(window.location.hash.substr(1)));
  160. if (vrootID.length >= 1){
  161. listRestorableFiles(vrootID[0]);
  162. }else{
  163. $("#error").removeClass("hidden");
  164. }
  165. }else{
  166. $("#error").removeClass("hidden");
  167. }
  168. function openSnapshotInfo(){
  169. var snapshotInfo = {
  170. SnapshotDisk: viewingFileInfo.BackupDiskUID,
  171. SnapshotName: viewingFileInfo.Filename
  172. };
  173. var hashPassthrough = encodeURIComponent(JSON.stringify(snapshotInfo));
  174. ao_module_newfw({
  175. url: "SystemAO/disk/disk_snapshot.html#" + hashPassthrough,
  176. width: 900,
  177. height: 520,
  178. appicon: "img/system/backup.svg",
  179. title: "Snapshot Summary",
  180. });
  181. }
  182. function showFileInfo(object){
  183. var filedata = $(object).attr("filedata");
  184. filedata = JSON.parse(decodeURIComponent(filedata));
  185. console.log(filedata);
  186. //Show information on page
  187. if (filedata.IsSnapshot){
  188. $("#fileinfo").find(".filename").text("Snapshot " + filedata.Filename);
  189. }else{
  190. $("#fileinfo").find(".filename").text(filedata.Filename + ` (${ao_module_utils.formatBytes(filedata.Filesize, 2)})`);
  191. }
  192. viewingFileInfo=filedata;
  193. //Hide the filename in relpath
  194. var shortenRelpath = filedata.RelpathOnDisk.split("/");
  195. shortenRelpath.pop();
  196. shortenRelpath = shortenRelpath.join("/") + "/";
  197. $("#fileinfo").find(".relpath").text(shortenRelpath);
  198. $("#fileinfo").find(".backupDisk").html('<i class="ui blue disk icon"></i> Restorable file from ' + filedata.BackupDiskUID + ":/");
  199. $("#fileinfo").find(".deleteTime").html('<i class="ui trash icon"></i> File deleted since ' + timeConverter(filedata.DeleteTime));
  200. $("#fileinfo").find(".restorePoint").html('<i class="ui refresh icon"></i> Restore to ' + (filedata.RestorePoint));
  201. $("#fileinfo").find(".deleteRemainingTime").html('<i class="ui red remove icon"></i> Auto delete in '+ secondsToHms(filedata.RemainingTime));
  202. if (filedata.IsSnapshot){
  203. $("#fileinfo").find(".deleteRemainingTime").hide();
  204. $("#fileinfo").find(".deleteTime").hide();
  205. $("#snapshotSummaryBtn").show();
  206. }else{
  207. $("#fileinfo").find(".deleteRemainingTime").show();
  208. $("#fileinfo").find(".deleteTime").show();
  209. $("#snapshotSummaryBtn").hide();
  210. }
  211. $("#fileinfo").slideDown('fast');
  212. }
  213. function secondsToHms(d) {
  214. d = Number(d);
  215. var h = Math.floor(d / 3600);
  216. var m = Math.floor(d % 3600 / 60);
  217. var s = Math.floor(d % 3600 % 60);
  218. var hDisplay = h > 0 ? h.toString().padStart(2, "0") + (h == 1 ? " hour, " : " hours, ") : "";
  219. var mDisplay = m > 0 ? m.toString().padStart(2, "0") + (m == 1 ? " minute, " : " minutes") : "";
  220. if (h > 24){
  221. var days = Math.floor(h/24);
  222. days = days + (days == 1 ? " day, " : " days, ")
  223. var hours = h % 24 + (h == 1 ? " hour, " : " hours, ");
  224. return days + hours + mDisplay;
  225. } else{
  226. return hDisplay + mDisplay;
  227. }
  228. }
  229. function timeConverter(UNIX_timestamp){
  230. var a = new Date(UNIX_timestamp * 1000);
  231. var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  232. var year = a.getFullYear();
  233. var month = months[a.getMonth()];
  234. var date = a.getDate();
  235. var hour = a.getHours().toString().padStart(2, "0");
  236. var min = a.getMinutes().toString().padStart(2, "0");
  237. var sec = a.getSeconds().toString().padStart(2, "0");
  238. var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
  239. return time;
  240. }
  241. function toggleHiddenFiles(status=undefined){
  242. if (status == undefined){
  243. $(".hiddenfile").slideToggle("fast");
  244. }else if (status == true){
  245. $(".hiddenfile").slideDown("fast");
  246. }else if (status == false){
  247. $(".hiddenfile").slideUp("fast");
  248. }
  249. }
  250. //Restore this file
  251. var restoreFileInfo = undefined;
  252. function restoreThisFile(fileObject){
  253. var filedata = $(fileObject).attr("filedata");
  254. filedata = JSON.parse(decodeURIComponent(filedata));
  255. console.log(filedata);
  256. if (filedata.IsSnapshot == true && !confirm("Confirm snapshot restore? All newly created files will be kept.")){
  257. $("#restoreCancel").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
  258. return;
  259. }
  260. $.ajax({
  261. url: "../../system/backup/restoreFile",
  262. method: "POST",
  263. data: {"bdid": filedata.BackupDiskUID, "relpath": filedata.RelpathOnDisk},
  264. success: function(data){
  265. if (data.error !== undefined){
  266. alert("Restore failed: " + data.error);
  267. }else{
  268. console.log(data);
  269. if (filedata.IsSnapshot){
  270. restoreFileInfo = filedata;
  271. }else{
  272. restoreFileInfo = data;
  273. }
  274. if (restoreFileInfo.RestoredVirtualPath == ""){
  275. $("#openfolder").hide();
  276. }else{
  277. $("#openfolder").show();
  278. }
  279. $("#succ").finish().stop().slideDown('fast').delay(10000).slideUp('fast');
  280. listRestorableFiles(vrootID[0]);
  281. }
  282. }
  283. })
  284. }
  285. function handleOpenRestoreLocation(){
  286. if (restoreFileInfo.RestorePoint !== undefined){
  287. //Snapshot
  288. ao_module_openPath(restoreFileInfo.RestorePoint + ":/");
  289. return
  290. }else if (restoreFileInfo.RestoredVirtualPath != undefined && restoreFileInfo.RestoredVirtualPath != ""){
  291. var filepath = restoreFileInfo.RestoredVirtualPath;
  292. var tmp = filepath.split("/");
  293. var filename = tmp.pop();
  294. var dirname = tmp.join("/");
  295. ao_module_openPath(dirname, filename);
  296. }
  297. }
  298. function listRestorableFiles(rootID){
  299. $.ajax({
  300. url: "/system/backup/listRestorable",
  301. data: {vroot: rootID},
  302. success: function(data){
  303. if (data.error !== undefined){
  304. $("#error").find(".reason").text(data.error);
  305. $("#error").removeClass("hidden");
  306. }else{
  307. //Filter out the restorable snapshot and files
  308. let restorableFiles = [];
  309. let restorableSnapshots = [];
  310. data.RestorableFiles.forEach(file => {
  311. if (file.IsSnapshot){
  312. restorableSnapshots.push(file);
  313. }else{
  314. restorableFiles.push(file);
  315. }
  316. });
  317. //Sort the result by latest first (file only)
  318. restorableFiles.sort(function(a, b) {
  319. return b.DeleteTime - a.DeleteTime;
  320. });
  321. restorableSnapshots.sort(function(a, b) {
  322. return b.Filename > a.Filename;
  323. });
  324. //Display the reuslt for restorableSnapshot
  325. $("#snapshotList").html("");
  326. restorableSnapshots.forEach(snapshot => {
  327. let snapshotData = encodeURIComponent(JSON.stringify(snapshot));
  328. $("#snapshotList").append(`<div class="item restorableFile snapshot" filedata="${snapshotData}">
  329. <div class="right floated content">
  330. <div class="ui tiny button" onclick="showFileInfo(this.parentNode.parentNode)">Info</div>
  331. <div class="ui tiny green button" onclick="restoreThisFile($(this).parent().parent());">Restore</div>
  332. </div>
  333. <div class="content">
  334. <p style="word-break: break-all; font-size: 97%"><img class="ui mini spaced image nointeract" src="../../img/system/drive-backup.svg">
  335. Snapshot ${snapshot.Filename}
  336. </p>
  337. </div>
  338. </div>`);
  339. })
  340. //Display the result for restorableFile
  341. $("#restorableList").html("");
  342. restorableFiles.forEach(fileObject => {
  343. let thisFileData = encodeURIComponent(JSON.stringify(fileObject));
  344. var timerIcon = "100.svg";
  345. if (fileObject.RemainingTime < 75600){
  346. timerIcon = "87_5.svg"
  347. }else if (fileObject.RemainingTime < 64800){
  348. timerIcon = "75.svg"
  349. }else if (fileObject.RemainingTime < 54000){
  350. timerIcon = "62_5.svg"
  351. }else if (fileObject.RemainingTime < 43200){
  352. timerIcon = "50.svg"
  353. }else if (fileObject.RemainingTime < 32400){
  354. timerIcon = "37_5.svg"
  355. }else if (fileObject.RemainingTime < 21600){
  356. timerIcon = "25.svg"
  357. }else if (fileObject.RemainingTime < 10800){
  358. timerIcon = "12_5.svg"
  359. }else if (fileObject.RemainingTime < 7200){
  360. timerIcon = "0.svg"
  361. }
  362. var deleteTimeLeftHumanReadable = secondsToHms(fileObject.RemainingTime);
  363. var fileIcon = "../../img/system/file-restore.svg";
  364. var isHidden = "";
  365. if (fileObject.IsHidden == true){
  366. fileIcon = "../../img/system/file-restore-hidden.svg";
  367. isHidden = "hiddenfile";
  368. }
  369. $("#restorableList").append(`<div class="item restorableFile file ${isHidden}" filedata="${thisFileData}">
  370. <div class="right floated content">
  371. <div class="timeleft" title="Will be deleted in ${deleteTimeLeftHumanReadable}">
  372. <img class="ui fluid image" src="../../img/icons/backup/${timerIcon}"/>
  373. </div>
  374. <div class="ui tiny button" onclick="showFileInfo(this.parentNode.parentNode)">Info</div>
  375. <div class="ui tiny green button" onclick="restoreThisFile($(this).parent().parent());">Restore</div>
  376. </div>
  377. <div class="content">
  378. <p style="word-break: break-all;"><img class="ui mini spaced image nointeract" src="${fileIcon}">
  379. ${fileObject.Filename}
  380. </p>
  381. </div>
  382. </div>`);
  383. });
  384. if (restorableFiles.length == 0){
  385. $("#restorableList").html(`<div class="item">
  386. <img class="ui mini image nointeract" src="../../img/system/file-notfound.svg">
  387. <div class="content">
  388. No Restorable File(s) for this virtual disk.
  389. </div>
  390. </div>`);
  391. }
  392. }
  393. $(".hiddenfile").hide();
  394. }
  395. })
  396. }
  397. function restoreAll(){
  398. if (confirm("Confirm restore all files from backup?")){
  399. $(".restorableFile").each(function(){
  400. //Restore this file if it is not a snapshot
  401. if ($(this).hasClass("snapshot") == false){
  402. restoreThisFile($(this));
  403. }
  404. });
  405. }
  406. }
  407. </script>
  408. </body>
  409. </html>