Browse Source

Add Musicify app settings store to server

Toby Chui 2 tuần trước cách đây
mục cha
commit
d81fd4cb01
3 tập tin đã thay đổi với 30 bổ sung24 xóa
  1. 2 2
      src/file_system.go
  2. 11 8
      src/web/Musicify/embedded.html
  3. 17 14
      src/web/Musicify/musicify.js

+ 2 - 2
src/file_system.go

@@ -2306,9 +2306,9 @@ func system_fs_handleUserPreference(w http.ResponseWriter, r *http.Request) {
 		utils.SendOK(w)
 	} else if key != "" && value != "" {
 		//Set mode. Set the preference with given key
-		if len(value) > 1024 {
+		if len(value) > 1024*1024 { //1KB
 			//Size too big. Reject storage
-			utils.SendErrorResponse(w, "Preference value too long. Preference value can only store maximum 1024 characters.")
+			utils.SendErrorResponse(w, "Preference value too long. Preference value can only store maximum 1KB.")
 			return
 		}
 		sysdb.Write("fs", "pref/"+key+"/"+username, value)

+ 11 - 8
src/web/Musicify/embedded.html

@@ -267,12 +267,15 @@
 		player.volume = Math.max(0, Math.min(1, isNaN(initVol) ? 0.6 : initVol));
 		setVolumeUI(player.volume);
 
-		// Restore prefs
-		var rr = localStorage.getItem('musicifyEmb_repeat');
-		if (rr === 'all' || rr === 'one') repeatMode = rr;
-		shuffleMode = (localStorage.getItem('musicifyEmb_shuffle') === 'true');
-		updateRepeatUI();
-		updateShuffleUI();
+		// Restore prefs from server (cross-device sync)
+		ao_module_storage.loadStorage("Musicify", "emb_repeat", function(val) {
+			if (val === 'all' || val === 'one') repeatMode = val;
+			updateRepeatUI();
+		});
+		ao_module_storage.loadStorage("Musicify", "emb_shuffle", function(val) {
+			if (val !== null && val !== undefined) shuffleMode = (val === 'true');
+			updateShuffleUI();
+		});
 
 		// Read input file passed by File Manager / ao_module_openWith
 		var inputFiles  = ao_module_loadInputFiles();
@@ -533,7 +536,7 @@
 		// ── Shuffle ───────────────────────────────────────────────────────
 		function toggleShuffle() {
 			shuffleMode = !shuffleMode;
-			localStorage.setItem('musicifyEmb_shuffle', shuffleMode);
+			ao_module_storage.setStorage("Musicify", "emb_shuffle", String(shuffleMode));
 			if (shuffleMode) buildShuffledList();
 			updateShuffleUI();
 		}
@@ -556,7 +559,7 @@
 			var modes = ['none', 'all', 'one'];
 			var idx   = modes.indexOf(repeatMode);
 			repeatMode = modes[(idx + 1) % modes.length];
-			localStorage.setItem('musicifyEmb_repeat', repeatMode);
+			ao_module_storage.setStorage("Musicify", "emb_repeat", repeatMode);
 			updateRepeatUI();
 		}
 

+ 17 - 14
src/web/Musicify/musicify.js

@@ -128,16 +128,19 @@ function musicifyApp() {
                 this._audio.volume = this.volume / 100;
             }
 
-            // Restore recently played
-            try {
-                var rp = localStorage.getItem('musicify_recent');
-                if (rp) this.recentlyPlayed = JSON.parse(rp).slice(0, 12);
-            } catch(e) {}
-
-            // Restore shuffle/repeat prefs
-            this.shuffle = localStorage.getItem('musicify_shuffle') === 'true';
-            var rep = localStorage.getItem('musicify_repeat');
-            if (rep === 'all' || rep === 'one') this.repeat = rep;
+            // Restore shuffle / repeat / recently-played from server-side prefs
+            // (cross-device: stored per user, not per browser)
+            ao_module_storage.loadStorage("Musicify", "shuffle", function(val) {
+                if (val !== null && val !== undefined) self.shuffle = (val === 'true');
+            });
+            ao_module_storage.loadStorage("Musicify", "repeat", function(val) {
+                if (val === 'all' || val === 'one' || val === 'none') self.repeat = val;
+            });
+            ao_module_storage.loadStorage("Musicify", "recent", function(val) {
+                if (val) {
+                    try { self.recentlyPlayed = JSON.parse(val).slice(0, 12); } catch(e) {}
+                }
+            });
 
             // MediaSession
             this._setupMediaSession();
@@ -913,7 +916,7 @@ function musicifyApp() {
 
         toggleShuffle() {
             this.shuffle = !this.shuffle;
-            localStorage.setItem('musicify_shuffle', this.shuffle);
+            ao_module_storage.setStorage("Musicify", "shuffle", String(this.shuffle));
             if (this.shuffle) this._buildShuffledQueue(this.queueIndex);
         },
 
@@ -921,7 +924,7 @@ function musicifyApp() {
             var modes = ['none', 'all', 'one'];
             var idx = modes.indexOf(this.repeat);
             this.repeat = modes[(idx + 1) % modes.length];
-            localStorage.setItem('musicify_repeat', this.repeat);
+            ao_module_storage.setStorage("Musicify", "repeat", this.repeat);
         },
 
         _onEnded() {
@@ -1034,14 +1037,14 @@ function musicifyApp() {
         },
 
         // ════════════════════════════════════════════════════════════════════
-        //  RECENTLY PLAYED (localStorage)
+        //  RECENTLY PLAYED (server-side, cross-device)
         // ════════════════════════════════════════════════════════════════════
         _saveRecentlyPlayed(song) {
             var list = this.recentlyPlayed.filter(s => s.filepath !== song.filepath);
             list.unshift(song);
             list = list.slice(0, 12);
             this.recentlyPlayed = list;
-            try { localStorage.setItem('musicify_recent', JSON.stringify(list)); } catch(e) {}
+            ao_module_storage.setStorage("Musicify", "recent", JSON.stringify(list));
         },
 
         // ════════════════════════════════════════════════════════════════════