first commit
This commit is contained in:
96
handlers/files.go
Normal file
96
handlers/files.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slideshowApp/env"
|
||||
"sync"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
// Keep track of all open slideshow connections
|
||||
clients = make(map[*websocket.Conn]bool)
|
||||
clientsMu sync.Mutex
|
||||
)
|
||||
|
||||
func UploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseMultipartForm(50 << 20)
|
||||
files := r.MultipartForm.File["myPictures"]
|
||||
uploadDir := env.PhotoDir.GetValue()
|
||||
os.MkdirAll(uploadDir, os.ModePerm)
|
||||
|
||||
for _, fileHeader := range files {
|
||||
file, _ := fileHeader.Open()
|
||||
dstPath := filepath.Join(uploadDir, fileHeader.Filename)
|
||||
dst, _ := os.Create(dstPath)
|
||||
io.Copy(dst, file)
|
||||
file.Close()
|
||||
dst.Close()
|
||||
}
|
||||
|
||||
// NEW: Tell all slideshows to update their lists
|
||||
notifyClients()
|
||||
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func ListFilesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
files, err := os.ReadDir(env.PhotoDir.GetValue())
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to read directory", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var fileNames []string
|
||||
for _, file := range files {
|
||||
if !file.IsDir() {
|
||||
fileNames = append(fileNames, file.Name())
|
||||
}
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(fileNames)
|
||||
}
|
||||
|
||||
func DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// 1. Get filenames from request body
|
||||
var filenames []string
|
||||
err := json.NewDecoder(r.Body).Decode(&filenames)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
uploadDir := env.PhotoDir.GetValue()
|
||||
|
||||
// 2. Delete each file
|
||||
for _, name := range filenames {
|
||||
fullPath := filepath.Join(uploadDir, filepath.Base(name)) // Base() for security
|
||||
os.Remove(fullPath)
|
||||
}
|
||||
|
||||
// 3. Notify slideshows to refresh their lists
|
||||
notifyClients()
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
func notifyClients() {
|
||||
clientsMu.Lock()
|
||||
defer clientsMu.Unlock()
|
||||
for client := range clients {
|
||||
err := client.WriteMessage(websocket.TextMessage, []byte("refresh"))
|
||||
if err != nil {
|
||||
client.Close()
|
||||
delete(clients, client)
|
||||
}
|
||||
}
|
||||
}
|
||||
51
handlers/infos.go
Normal file
51
handlers/infos.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net"
|
||||
"net/http"
|
||||
"slideshowApp/env"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Helper to get the local network IP address
|
||||
func getLocalIP() string {
|
||||
if env.Host.GetValue() != "0.0.0.0" && env.Host.GetValue() != "localhost" {
|
||||
return env.Host.GetValue()
|
||||
}
|
||||
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "localhost"
|
||||
}
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
if env.Env.GetValue() == "development" && !strings.Contains(ipnet.IP.String(), "192.168") {
|
||||
continue
|
||||
}
|
||||
return ipnet.IP.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return "localhost"
|
||||
}
|
||||
|
||||
func InfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
port := env.Port.GetValue()
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
speed := env.IntervalDefault.GetValue()
|
||||
if speed == "" {
|
||||
speed = "10"
|
||||
}
|
||||
|
||||
data := map[string]string{
|
||||
"ip": getLocalIP(),
|
||||
"port": port,
|
||||
"speed": speed,
|
||||
}
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}
|
||||
26
handlers/scheduler.go
Normal file
26
handlers/scheduler.go
Normal file
@@ -0,0 +1,26 @@
|
||||
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)
|
||||
}
|
||||
23
handlers/websocket.go
Normal file
23
handlers/websocket.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
}
|
||||
)
|
||||
|
||||
func Websocket(w http.ResponseWriter, r *http.Request) {
|
||||
conn, err := upgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
clientsMu.Lock()
|
||||
clients[conn] = true
|
||||
clientsMu.Unlock()
|
||||
}
|
||||
Reference in New Issue
Block a user