disk_restore.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. <br><br>
  154. <script>
  155. var vrootID = "";
  156. var viewingFileInfo = {};
  157. $(".ui.checkbox").checkbox();
  158. //Extract vroot id from the window hash
  159. if (window.location.hash.length > 1){
  160. vrootID = JSON.parse(decodeURIComponent(window.location.hash.substr(1)));
  161. if (vrootID.length >= 1){
  162. listRestorableFiles(vrootID[0]);
  163. }else{
  164. $("#error").removeClass("hidden");
  165. }
  166. }else{
  167. $("#error").removeClass("hidden");
  168. }
  169. function openSnapshotInfo(){
  170. var snapshotInfo = {
  171. SnapshotDisk: viewingFileInfo.BackupDiskUID,
  172. SnapshotName: viewingFileInfo.Filename
  173. };
  174. var hashPassthrough = encodeURIComponent(JSON.stringify(snapshotInfo));
  175. ao_module_newfw({
  176. url: "SystemAO/disk/disk_snapshot.html#" + hashPassthrough,
  177. width: 900,
  178. height: 520,
  179. appicon: "img/system/backup.svg",
  180. title: "Snapshot Summary",
  181. });
  182. }
  183. function showFileInfo(object){
  184. var filedata = $(object).attr("filedata");
  185. filedata = JSON.parse(decodeURIComponent(filedata));
  186. console.log(filedata);
  187. //Show information on page
  188. if (filedata.IsSnapshot){
  189. $("#fileinfo").find(".filename").text("Snapshot " + filedata.Filename);
  190. }else{
  191. $("#fileinfo").find(".filename").text(filedata.Filename + ` (${ao_module_utils.formatBytes(filedata.Filesize, 2)})`);
  192. }
  193. viewingFileInfo=filedata;
  194. //Hide the filename in relpath
  195. var shortenRelpath = filedata.RelpathOnDisk.split("/");
  196. shortenRelpath.pop();
  197. shortenRelpath = shortenRelpath.join("/") + "/";
  198. $("#fileinfo").find(".relpath").text(shortenRelpath);
  199. $("#fileinfo").find(".backupDisk").html('<i class="ui blue disk icon"></i> Restorable file from ' + filedata.BackupDiskUID + ":/");
  200. $("#fileinfo").find(".deleteTime").html('<i class="ui trash icon"></i> File deleted since ' + timeConverter(filedata.DeleteTime));
  201. $("#fileinfo").find(".restorePoint").html('<i class="ui refresh icon"></i> Restore to ' + (filedata.RestorePoint));
  202. $("#fileinfo").find(".deleteRemainingTime").html('<i class="ui red remove icon"></i> Auto delete in '+ secondsToHms(filedata.RemainingTime));
  203. if (filedata.IsSnapshot){
  204. $("#fileinfo").find(".deleteRemainingTime").hide();
  205. $("#fileinfo").find(".deleteTime").hide();
  206. $("#snapshotSummaryBtn").show();
  207. }else{
  208. $("#fileinfo").find(".deleteRemainingTime").show();
  209. $("#fileinfo").find(".deleteTime").show();
  210. $("#snapshotSummaryBtn").hide();
  211. }
  212. $("#fileinfo").slideDown('fast');
  213. }
  214. function secondsToHms(d) {
  215. d = Number(d);
  216. var h = Math.floor(d / 3600);
  217. var m = Math.floor(d % 3600 / 60);
  218. var s = Math.floor(d % 3600 % 60);
  219. var hDisplay = h > 0 ? h.toString().padStart(2, "0") + (h == 1 ? " hour, " : " hours, ") : "";
  220. var mDisplay = m > 0 ? m.toString().padStart(2, "0") + (m == 1 ? " minute, " : " minutes") : "";
  221. return hDisplay + mDisplay;
  222. }
  223. function timeConverter(UNIX_timestamp){
  224. var a = new Date(UNIX_timestamp * 1000);
  225. var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  226. var year = a.getFullYear();
  227. var month = months[a.getMonth()];
  228. var date = a.getDate();
  229. var hour = a.getHours().toString().padStart(2, "0");
  230. var min = a.getMinutes().toString().padStart(2, "0");
  231. var sec = a.getSeconds().toString().padStart(2, "0");
  232. var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
  233. return time;
  234. }
  235. function toggleHiddenFiles(status=undefined){
  236. if (status == undefined){
  237. $(".hiddenfile").slideToggle("fast");
  238. }else if (status == true){
  239. $(".hiddenfile").slideDown("fast");
  240. }else if (status == false){
  241. $(".hiddenfile").slideUp("fast");
  242. }
  243. }
  244. //Restore this file
  245. var restoreFileInfo = undefined;
  246. function restoreThisFile(fileObject){
  247. var filedata = $(fileObject).attr("filedata");
  248. filedata = JSON.parse(decodeURIComponent(filedata));
  249. console.log(filedata);
  250. if (filedata.IsSnapshot == true && !confirm("Confirm snapshot restore? All newly created files will be kept.")){
  251. $("#restoreCancel").stop().finish().slideDown('fast').delay(3000).slideUp('fast');
  252. return;
  253. }
  254. $.ajax({
  255. url: "../../system/backup/restoreFile",
  256. method: "POST",
  257. data: {"bdid": filedata.BackupDiskUID, "relpath": filedata.RelpathOnDisk},
  258. success: function(data){
  259. if (data.error !== undefined){
  260. alert("Restore failed: " + data.error);
  261. }else{
  262. console.log(data);
  263. if (filedata.IsSnapshot){
  264. restoreFileInfo = filedata;
  265. }else{
  266. restoreFileInfo = data;
  267. }
  268. if (restoreFileInfo.RestoredVirtualPath == ""){
  269. $("#openfolder").hide();
  270. }else{
  271. $("#openfolder").show();
  272. }
  273. $("#succ").finish().stop().slideDown('fast').delay(10000).slideUp('fast');
  274. listRestorableFiles(vrootID[0]);
  275. }
  276. }
  277. })
  278. }
  279. function handleOpenRestoreLocation(){
  280. if (restoreFileInfo.RestorePoint !== undefined){
  281. //Snapshot
  282. ao_module_openPath(restoreFileInfo.RestorePoint + ":/");
  283. return
  284. }else if (restoreFileInfo.RestoredVirtualPath != undefined && restoreFileInfo.RestoredVirtualPath != ""){
  285. var filepath = restoreFileInfo.RestoredVirtualPath;
  286. var tmp = filepath.split("/");
  287. var filename = tmp.pop();
  288. var dirname = tmp.join("/");
  289. ao_module_openPath(dirname, filename);
  290. }
  291. }
  292. function listRestorableFiles(rootID){
  293. $.ajax({
  294. url: "/system/backup/listRestorable",
  295. data: {vroot: rootID},
  296. success: function(data){
  297. if (data.error !== undefined){
  298. $("#error").find(".reason").text(data.error);
  299. $("#error").removeClass("hidden");
  300. }else{
  301. //Filter out the restorable snapshot and files
  302. let restorableFiles = [];
  303. let restorableSnapshots = [];
  304. data.RestorableFiles.forEach(file => {
  305. if (file.IsSnapshot){
  306. restorableSnapshots.push(file);
  307. }else{
  308. restorableFiles.push(file);
  309. }
  310. });
  311. //Sort the result by latest first (file only)
  312. restorableFiles.sort(function(a, b) {
  313. return b.DeleteTime - a.DeleteTime;
  314. });
  315. restorableSnapshots.sort(function(a, b) {
  316. return b.Filename > a.Filename;
  317. });
  318. //Display the reuslt for restorableSnapshot
  319. $("#snapshotList").html("");
  320. restorableSnapshots.forEach(snapshot => {
  321. let snapshotData = encodeURIComponent(JSON.stringify(snapshot));
  322. $("#snapshotList").append(`<div class="item restorableFile snapshot" filedata="${snapshotData}">
  323. <div class="right floated content">
  324. <div class="ui tiny button" onclick="showFileInfo(this.parentNode.parentNode)">Info</div>
  325. <div class="ui tiny green button" onclick="restoreThisFile($(this).parent().parent());">Restore</div>
  326. </div>
  327. <div class="content">
  328. <p style="word-break: break-all; font-size: 97%"><img class="ui mini spaced image nointeract" src="../../img/system/drive-backup.svg">
  329. Snapshot ${snapshot.Filename}
  330. </p>
  331. </div>
  332. </div>`);
  333. })
  334. //Display the result for restorableFile
  335. $("#restorableList").html("");
  336. restorableFiles.forEach(fileObject => {
  337. let thisFileData = encodeURIComponent(JSON.stringify(fileObject));
  338. var timerIcon = "100.svg";
  339. if (fileObject.RemainingTime < 75600){
  340. timerIcon = "87_5.svg"
  341. }else if (fileObject.RemainingTime < 64800){
  342. timerIcon = "75.svg"
  343. }else if (fileObject.RemainingTime < 54000){
  344. timerIcon = "62_5.svg"
  345. }else if (fileObject.RemainingTime < 43200){
  346. timerIcon = "50.svg"
  347. }else if (fileObject.RemainingTime < 32400){
  348. timerIcon = "37_5.svg"
  349. }else if (fileObject.RemainingTime < 21600){
  350. timerIcon = "25.svg"
  351. }else if (fileObject.RemainingTime < 10800){
  352. timerIcon = "12_5.svg"
  353. }else if (fileObject.RemainingTime < 7200){
  354. timerIcon = "0.svg"
  355. }
  356. var deleteTimeLeftHumanReadable = secondsToHms(fileObject.RemainingTime);
  357. var fileIcon = "../../img/system/file-restore.svg";
  358. var isHidden = "";
  359. if (fileObject.IsHidden == true){
  360. fileIcon = "../../img/system/file-restore-hidden.svg";
  361. isHidden = "hiddenfile";
  362. }
  363. $("#restorableList").append(`<div class="item restorableFile file ${isHidden}" filedata="${thisFileData}">
  364. <div class="right floated content">
  365. <div class="timeleft" title="Will be deleted in ${deleteTimeLeftHumanReadable}">
  366. <img class="ui fluid image" src="../../img/icons/backup/${timerIcon}"/>
  367. </div>
  368. <div class="ui tiny button" onclick="showFileInfo(this.parentNode.parentNode)">Info</div>
  369. <div class="ui tiny green button" onclick="restoreThisFile($(this).parent().parent());">Restore</div>
  370. </div>
  371. <div class="content">
  372. <p style="word-break: break-all;"><img class="ui mini spaced image nointeract" src="${fileIcon}">
  373. ${fileObject.Filename}
  374. </p>
  375. </div>
  376. </div>`);
  377. });
  378. if (restorableFiles.length == 0){
  379. $("#restorableList").html(`<div class="item">
  380. <img class="ui mini image nointeract" src="../../img/system/file-notfound.svg">
  381. <div class="content">
  382. No Restorable File(s) for this virtual disk.
  383. </div>
  384. </div>`);
  385. }
  386. }
  387. $(".hiddenfile").hide();
  388. }
  389. })
  390. }
  391. function restoreAll(){
  392. if (confirm("Confirm restore all files from backup?")){
  393. $(".restorableFile").each(function(){
  394. //Restore this file if it is not a snapshot
  395. if ($(this).hasClass("snapshot") == false){
  396. restoreThisFile($(this));
  397. }
  398. });
  399. }
  400. }
  401. </script>
  402. </body>
  403. </html>