Browse Source

optimized user UI

Toby Chui 1 year ago
parent
commit
fd7b0e47b3

+ 1 - 0
firmware/v3/web-server/server.ino

@@ -142,6 +142,7 @@ void initWebServer() {
   server.on("/api/share/new", HTTP_POST, HandleCreateShare);
   server.on("/api/share/del", HTTP_POST, HandleRemoveShare);
   server.on("/api/share/list", HTTP_GET, HandleShareList);
+  server.on("/api/share/clean", HTTP_GET, HandleShareListCleaning);
   server.on("/share", HTTP_GET, HandleShareAccess);
   
   

+ 44 - 0
firmware/v3/web-server/share.ino

@@ -177,6 +177,50 @@ void HandleShareAccess(AsyncWebServerRequest *r) {
   }
 }
 
+//Clear the shares that no longer exists
+void HandleShareListCleaning(AsyncWebServerRequest *r) {
+  if (!HandleAuth(r)) {
+    return;
+  }
+
+  File root = SD.open(DB_root + "shln/");
+  bool firstObject = true;
+  if (root) {
+    while (true) {
+      File entry = root.openNextFile();
+      if (!entry) {
+        // No more files
+        break;
+      } else {
+        //Filter out all the directory if any
+        if (entry.isDirectory()) {
+          continue;
+        }
+
+        //Read the filename from file
+        String filename = "";
+        while (entry.available()) {
+          filename = filename + entry.readString();
+        }
+
+        //Check if the target file still exists
+        File targetFile = SD.open("/www" + filename);
+        if (!targetFile) {
+          //File no longer exists. Remove this share entry
+          DBRemove("shln", entry.name());
+          DBRemove("shfn", filename);
+        } else {
+          //File still exists.
+          targetFile.close();
+        }
+      }
+    }
+  }
+
+  SendOK(r);
+}
+
+
 //Get the file share ID from filename, return empty string if not shared
 String GetFileShareIDByFilename(String filepath) {
   return DBRead("shfn", filepath);

+ 23 - 0
sd_card/www/admin/shares.html

@@ -18,6 +18,13 @@
     <br>
     <div class="ui container">
         <div class="ui segment">
+            <h3 class="ui header">
+                <img src="img/opr/share.svg">
+                <div class="content">
+                    Shares Manager
+                    <div class="sub header">All the shared files on the SD card</div>
+                </div>
+            </h3>
             <table class="ui celled striped table">
                 <thead>
                   <tr><th colspan="4">
@@ -34,6 +41,11 @@
                 </tbody>
             </table>
             <button onclick="initShareList();" class="ui basic button"><i class="ui green refresh icon"></i>Refresh List</button>
+            <div class="ui divider"></div>
+            <b>Share Entry Cleaning</b>
+            <p>Some share links might point to files that no longer exists on the SD card. <br>
+                Click the "Clean Shares" button below to remove broken share links and regenerate the shared link table.</p>
+            <button onclick="runShareCleaning();" class="ui basic button"><i class="ui green recycle icon"></i>Clean Shares</button>
         </div>
     </div>
     <script>
@@ -88,6 +100,17 @@
             }
             
         }
+
+        function runShareCleaning(){
+            $.get("/api/share/clean", function(data){
+                if (data.error != undefined){
+                    alert(data.error);
+                }else{
+                    initShareList();
+                    alert("Share cleaning completed");
+                }
+            })
+        }
     </script>
 </body>
 </html>

+ 3 - 0
sd_card/www/admin/users.html

@@ -170,6 +170,9 @@
             //user can only change their own password
             function changePassword(username){
                 let newpassword = prompt("New password", "");
+                if (newpassword == "" || newpassword== null){
+                    return;
+                }
                 let confirmNewPassword  = prompt("Confirm new password", "");
                 if (newpassword != confirmNewPassword){
                     alert("Confirm password not match!");