Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7aef03a7cd | ||
|
|
d1f1ad563b | ||
|
|
66c6d9a3fb | ||
|
|
2c718824d2 | ||
|
|
f8dce0b817 | ||
|
|
0eb781a6d3 | ||
|
|
9150550609 | ||
|
|
84abf6b820 | ||
|
|
4904589086 |
2
.env
2
.env
@@ -3,6 +3,6 @@ ENV=
|
||||
PHOTO_DIR=images
|
||||
HOST=0.0.0.0
|
||||
PORT=8080
|
||||
INTERVAL_DEFAULT=10
|
||||
INTERVAL_DEFAULT=120
|
||||
#path for logger files default {executable name}.log
|
||||
LOG_PATH=
|
||||
@@ -98,6 +98,7 @@ jobs:
|
||||
# 2. Create Directory Structure
|
||||
mkdir -p $BUILD_DIR/usr/bin
|
||||
mkdir -p $BUILD_DIR/usr/share/$PKG_NAME
|
||||
mkdir -p $BUILD_DIR/usr/share/$PKG_NAME/data
|
||||
mkdir -p $BUILD_DIR/etc/systemd/system
|
||||
mkdir -p $BUILD_DIR/DEBIAN
|
||||
|
||||
@@ -115,12 +116,11 @@ jobs:
|
||||
|
||||
# 5. Create a Template Autostart File (Instead of Systemd)
|
||||
mkdir -p $BUILD_DIR/usr/share/$PKG_NAME/setup
|
||||
cat <<EOF > $BUILD_DIR/usr/share/$PKG_NAME/setup/$PKG_NAME.desktop
|
||||
cat <<'EOF' > $BUILD_DIR/usr/share/$PKG_NAME/setup/$PKG_NAME.desktop
|
||||
[Desktop Entry]
|
||||
Type=Application
|
||||
Name=Slideshow App
|
||||
Exec=/usr/bin/$PKG_NAME
|
||||
WorkingDirectory=/usr/share/$PKG_NAME
|
||||
Exec=sh -c 'i=0; while [ $i -lt 30 ]; do if [ "$(ls -A /media/$USER 2>/dev/null)" ] && ping -c 1 -W 1 8.8.8.8 >/dev/null 2>&1; then break; fi; i=$((i+1)); sleep 1; done; cd /usr/share/slideshowapp && /usr/bin/slideshowapp'
|
||||
Terminal=false
|
||||
EOF
|
||||
|
||||
@@ -151,6 +151,10 @@ jobs:
|
||||
|
||||
USER_HOME=$(getent passwd "$REAL_USER" | cut -d: -f6)
|
||||
|
||||
echo "Setting permissions for /usr/share/slideshowapp..."
|
||||
chown -R "$REAL_USER:$REAL_USER" /usr/share/slideshowapp
|
||||
chmod -R 755 /usr/share/slideshowapp
|
||||
|
||||
echo "Post-install: Target user is $REAL_USER"
|
||||
echo "Post-install: Target home is $USER_HOME"
|
||||
|
||||
@@ -160,7 +164,7 @@ jobs:
|
||||
cp /usr/share/slideshowapp/env.template /usr/share/slideshowapp/.env || true
|
||||
fi
|
||||
|
||||
# 3. Setup Autostart (THE FIX)
|
||||
# 3. Setup Autostart
|
||||
# We use -p to ensure parent directories exist and set ownership immediately
|
||||
AUTOSTART_DIR="$USER_HOME/.config/autostart"
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 2. Delete each file
|
||||
for _, name := range filenames {
|
||||
fullPath := filepath.Join(uploadDir, filepath.Base(name)) // Base() for security
|
||||
fullPath := filepath.Join(uploadDir, filepath.Base(name))
|
||||
os.Remove(fullPath)
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ func InfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
data := map[string]string{
|
||||
"ip": GetLocalIP(),
|
||||
"port": port,
|
||||
"speed": speed,
|
||||
"speed": GetInterval(),
|
||||
}
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type Schedule map[string]interface{}
|
||||
|
||||
func SaveSchedule(w http.ResponseWriter, r *http.Request) {
|
||||
var s Schedule
|
||||
json.NewDecoder(r.Body).Decode(&s)
|
||||
data, _ := json.Marshal(s)
|
||||
os.WriteFile("schedule.json", data, 0644)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetSchedule(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := os.ReadFile("schedule.json")
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(Schedule{})
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
81
handlers/settings.go
Normal file
81
handlers/settings.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slideshowApp/env"
|
||||
"slideshowApp/utils"
|
||||
)
|
||||
|
||||
var settingsFile = "settings.json"
|
||||
|
||||
type Settings map[string]any
|
||||
|
||||
func SaveSettings(w http.ResponseWriter, r *http.Request) {
|
||||
var s Settings
|
||||
if err := json.NewDecoder(r.Body).Decode(&s); err != nil {
|
||||
fmt.Println(12, err)
|
||||
utils.SendJSONError(w, "Invalid input", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(s, "", " ")
|
||||
if err != nil {
|
||||
fmt.Println(13, err)
|
||||
|
||||
utils.SendJSONError(w, "Encoding failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = os.WriteFile(getFilePath(), data, 0644)
|
||||
if err != nil {
|
||||
fmt.Println(14, err)
|
||||
|
||||
utils.SendJSONError(w, "write file failed", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func GetSettings(w http.ResponseWriter, r *http.Request) {
|
||||
data, err := os.ReadFile(getFilePath())
|
||||
if err != nil {
|
||||
json.NewEncoder(w).Encode(Settings{"default_interval": GetInterval()})
|
||||
return
|
||||
}
|
||||
w.Write(data)
|
||||
}
|
||||
|
||||
func GetInterval() string {
|
||||
data, err := os.ReadFile(getFilePath())
|
||||
if err != nil {
|
||||
interval := env.IntervalDefault.GetValue()
|
||||
if interval == "" {
|
||||
interval = "10"
|
||||
}
|
||||
return interval
|
||||
}
|
||||
var settings Settings
|
||||
err = json.Unmarshal(data, &settings)
|
||||
if err != nil {
|
||||
return "10"
|
||||
}
|
||||
|
||||
if interval, ok := settings["default_interval"]; ok {
|
||||
return interval.(string)
|
||||
}
|
||||
return "10"
|
||||
}
|
||||
|
||||
func getFilePath() string {
|
||||
switch runtime.GOOS {
|
||||
case "linux":
|
||||
return filepath.Join(".", "data", settingsFile)
|
||||
default:
|
||||
return settingsFile
|
||||
}
|
||||
}
|
||||
4
main.go
4
main.go
@@ -50,8 +50,8 @@ func main() {
|
||||
r.HandleFunc("/ws", handlers.Websocket)
|
||||
r.HandleFunc("/upload", handlers.UploadHandler).Methods("POST")
|
||||
r.HandleFunc("/settings", func(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, staticFolder+"settings.html") })
|
||||
r.HandleFunc("/api/save-schedule", handlers.SaveSchedule).Methods("POST")
|
||||
r.HandleFunc("/api/get-schedule", handlers.GetSchedule).Methods("GET")
|
||||
r.HandleFunc("/api/save-settings", handlers.SaveSettings).Methods("POST")
|
||||
r.HandleFunc("/api/get-settings", handlers.GetSettings).Methods("GET")
|
||||
|
||||
r.HandleFunc("/api/delete", handlers.DeleteHandler).Methods("POST")
|
||||
r.HandleFunc("/manage", func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
12
utils/http.go
Normal file
12
utils/http.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func SendJSONError(w http.ResponseWriter, message string, code int) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(code)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": message})
|
||||
}
|
||||
@@ -33,7 +33,7 @@
|
||||
|
||||
<button id="shuffleBtn" onclick="toggleShuffle()" class="text-xs uppercase font-bold text-gray-400 hover:text-white transition-colors flex items-center gap-2">
|
||||
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /></svg>
|
||||
Shuffle: <span id="shuffleStatus">OFF</span>
|
||||
Shuffle: <span id="shuffleStatus">ON</span>
|
||||
</button>
|
||||
|
||||
<button onclick="toggleFullScreen()" class="text-sm hover:text-blue-400 transition-colors">Full Screen</button>
|
||||
@@ -58,6 +58,18 @@
|
||||
const speedInput = document.getElementById('speed');
|
||||
const shuffleStatus = document.getElementById('shuffleStatus');
|
||||
|
||||
async function initializeSlideshow() {
|
||||
// 1. Get the default interval from API
|
||||
const defaultInterval = await checkDefaultInterval();
|
||||
speedInput.value = defaultInterval;
|
||||
|
||||
// 2. Setup QR Code
|
||||
setupQRCode();
|
||||
|
||||
// 3. Load Images (which eventually calls start())
|
||||
loadImages();
|
||||
}
|
||||
|
||||
// Fisher-Yates Shuffle Algorithm
|
||||
function shuffleArray(array) {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
@@ -75,10 +87,23 @@
|
||||
index = 0;
|
||||
}
|
||||
|
||||
async function checkSchedule() {
|
||||
async function checkDefaultInterval() {
|
||||
try {
|
||||
const res = await fetch('/api/get-schedule');
|
||||
const schedule = await res.json();
|
||||
const res = await fetch('/api/get-settings');
|
||||
const settings = await res.json();
|
||||
|
||||
// Return the saved interval, or fallback to 10 if it's missing
|
||||
return settings.default_interval || "120";
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch default interval:", err);
|
||||
return "120";
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSettings() {
|
||||
try {
|
||||
const res = await fetch('/api/get-settings');
|
||||
const settings = await res.json();
|
||||
|
||||
const now = new Date();
|
||||
const day = now.toLocaleDateString('en-US', { weekday: 'long' });
|
||||
@@ -86,9 +111,9 @@
|
||||
const currentTime = now.getHours().toString().padStart(2, '0') + ":" +
|
||||
now.getMinutes().toString().padStart(2, '0');
|
||||
|
||||
const isActive = schedule[day + "_active"];
|
||||
const start = schedule[day + "_start"];
|
||||
const end = schedule[day + "_end"];
|
||||
const isActive = settings[day + "_active"];
|
||||
const start = settings[day + "_start"];
|
||||
const end = settings[day + "_end"];
|
||||
|
||||
// Check if we should be "OFF"
|
||||
if (isActive && (currentTime < start || currentTime > end)) {
|
||||
@@ -131,7 +156,7 @@
|
||||
async function showNext() {
|
||||
if (playlist.length === 0) return;
|
||||
|
||||
const isRunning = await checkSchedule();
|
||||
const isRunning = await checkSettings();
|
||||
if (!isRunning) return;
|
||||
|
||||
viewer.classList.replace('fade-in', 'fade-out');
|
||||
@@ -205,8 +230,7 @@
|
||||
speedInput.addEventListener('change', start);
|
||||
|
||||
// Initial setup
|
||||
setupQRCode();
|
||||
loadImages();
|
||||
initializeSlideshow();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -8,24 +8,41 @@
|
||||
<body class="bg-gray-50 min-h-screen p-6">
|
||||
<div class="max-w-2xl mx-auto bg-white rounded-xl shadow-lg p-8 border border-gray-100">
|
||||
<div class="flex justify-between items-center mb-8">
|
||||
<h1 class="text-2xl font-bold text-gray-800">Weekly Scheduler</h1>
|
||||
<h1 class="text-2xl font-bold text-gray-800">Slideshow Settings</h1>
|
||||
<a href="/" class="text-blue-600 hover:underline text-sm">Back</a>
|
||||
</div>
|
||||
|
||||
<form id="scheduleForm" class="space-y-4">
|
||||
<table class="w-full text-left">
|
||||
<thead>
|
||||
<tr class="text-gray-400 text-xs uppercase">
|
||||
<th class="pb-4">Day</th>
|
||||
<th class="pb-4">Enabled</th>
|
||||
<th class="pb-4">Start Time</th>
|
||||
<th class="pb-4">End Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="text-gray-600 italic text-sm">
|
||||
</tbody>
|
||||
</table>
|
||||
<button type="submit" class="w-full bg-blue-600 text-white py-3 rounded-lg font-bold shadow-md hover:bg-blue-700 transition">Save Schedule</button>
|
||||
<form id="scheduleForm" class="space-y-8">
|
||||
<div class="space-y-4">
|
||||
<h2 class="text-sm font-semibold text-gray-400 uppercase tracking-wider">General Settings</h2>
|
||||
<div class="flex items-center justify-between p-4 bg-gray-50 rounded-lg border border-gray-100">
|
||||
<label class="text-gray-700 font-medium">Default Interval (seconds)</label>
|
||||
<input type="number" name="default_interval" min="1" value="10"
|
||||
class="w-24 bg-white border rounded-md p-2 text-center focus:ring-2 focus:ring-blue-500 outline-none">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<hr class="border-gray-100">
|
||||
|
||||
<div class="space-y-4">
|
||||
<h2 class="text-sm font-semibold text-gray-400 uppercase tracking-wider">Weekly Scheduler</h2>
|
||||
<table class="w-full text-left">
|
||||
<thead>
|
||||
<tr class="text-gray-400 text-xs uppercase">
|
||||
<th class="pb-4">Day</th>
|
||||
<th class="pb-4">Enabled</th>
|
||||
<th class="pb-4">Start Time</th>
|
||||
<th class="pb-4">End Time</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="text-gray-600 italic text-sm">
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<button type="submit" class="w-full bg-blue-600 text-white py-3 rounded-lg font-bold shadow-md hover:bg-blue-700 transition">
|
||||
Save All Settings
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -47,32 +64,57 @@
|
||||
|
||||
// Load existing settings
|
||||
async function loadSettings() {
|
||||
const res = await fetch('/api/get-schedule');
|
||||
const data = await res.json();
|
||||
Object.keys(data).forEach(key => {
|
||||
const el = document.querySelector(`[name="${key}"]`);
|
||||
if(el.type === 'checkbox') el.checked = data[key];
|
||||
else el.value = data[key];
|
||||
});
|
||||
try {
|
||||
const res = await fetch('/api/get-settings');
|
||||
const data = await res.json();
|
||||
|
||||
Object.keys(data).forEach(key => {
|
||||
const el = document.querySelector(`[name="${key}"]`);
|
||||
if (el) {
|
||||
if (el.type === 'checkbox') el.checked = data[key];
|
||||
else el.value = data[key];
|
||||
}
|
||||
});
|
||||
} catch (err) {
|
||||
console.error("Failed to load settings:", err);
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('scheduleForm').onsubmit = async (e) => {
|
||||
e.preventDefault();
|
||||
const formData = new FormData(e.target);
|
||||
const obj = {};
|
||||
formData.forEach((value, key) => {
|
||||
// Handle checkbox value manually
|
||||
if (key.includes('_active')) obj[key] = true;
|
||||
else obj[key] = value;
|
||||
});
|
||||
// Ensure unchecked boxes are false
|
||||
days.forEach(day => { if(!obj[day+'_active']) obj[day+'_active'] = false; });
|
||||
|
||||
await fetch('/api/save-schedule', {
|
||||
formData.forEach((value, key) => {
|
||||
// Convert number fields to integers
|
||||
if (key === 'default_interval') {
|
||||
obj[key] = parseInt(value);
|
||||
}
|
||||
// Handle checkbox presence
|
||||
else if (key.includes('_active')) {
|
||||
obj[key] = true;
|
||||
}
|
||||
else {
|
||||
obj[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
// Ensure unchecked boxes are sent as false
|
||||
days.forEach(day => {
|
||||
if (!obj[day + '_active']) obj[day + '_active'] = false;
|
||||
});
|
||||
|
||||
const res = await fetch('/api/save-settings', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(obj)
|
||||
});
|
||||
alert("Schedule Saved!");
|
||||
|
||||
if (res.ok) {
|
||||
alert("Settings Saved Successfully!");
|
||||
} else {
|
||||
alert("Error saving settings.");
|
||||
}
|
||||
};
|
||||
|
||||
loadSettings();
|
||||
|
||||
Reference in New Issue
Block a user