Compare commits

...

2 Commits

Author SHA1 Message Date
d37cacf2b7 Merge branch 'main' of https://git.neunzweinull.com/jan/CCTV 2025-03-02 12:03:17 +01:00
5ab286bad9 /uploads 2025-03-02 12:02:54 +01:00
6 changed files with 23 additions and 9 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -40,7 +40,9 @@ func main() {
http.HandleFunc("/upload", internal.AuthMiddleware(internal.UploadHandler)) http.HandleFunc("/upload", internal.AuthMiddleware(internal.UploadHandler))
http.HandleFunc("/health", internal.HealthHandler) http.HandleFunc("/health", internal.HealthHandler)
http.HandleFunc("/uploads/", internal.AuthClientMiddleware(internal.HandleFileServer("/uploads", "/uploads/"))) http.HandleFunc("/uploads/", internal.AuthClientMiddleware(
internal.HandlerToHandlerFunc(http.StripPrefix("/uploads/", http.FileServer(http.Dir("./uploads")))),
))
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }

View File

@ -24,7 +24,10 @@ func InitLog() {
var logFilePath string var logFilePath string
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName) logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName)
} else if runtime.GOOS == "darwin" {
logFilePath = filepath.Join("/Library/Logs", logFileName)
} else { } else {
logFilePath = filepath.Join("/var/log", logFileName) logFilePath = filepath.Join("/var/log", logFileName)
} }

View File

@ -155,7 +155,7 @@ func AuthClientMiddleware(next http.HandlerFunc) http.HandlerFunc {
func DashboardHandler(w http.ResponseWriter, r *http.Request) { func DashboardHandler(w http.ResponseWriter, r *http.Request) {
// get the newest picture name // get the newest picture name
pictures, err := os.ReadDir(UploadDir) pictures, err := os.ReadDir("uploads")
if err != nil { if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError) http.Error(w, "Error reading directory", http.StatusInternalServerError)
return return
@ -278,15 +278,23 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
} }
func HandleFileServer(dir, prefix string) http.HandlerFunc { func HandlerToHandlerFunc(h http.Handler) http.HandlerFunc {
fs := http.FileServer(http.Dir(dir))
realHandler := http.StripPrefix(prefix, fs).ServeHTTP
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
LogFileServerRequest(getClientIP(r), r.URL.Path) h.ServeHTTP(w, r)
realHandler(w, r)
} }
} }
func FileServerHandler(w http.ResponseWriter, r *http.Request) {
filepath := filepath.Join("uploads", r.URL.Path)
if _, err := os.Stat(filepath); err != nil {
http.Error(w, "File not found", http.StatusNotFound)
return
}
http.ServeFile(w, r, filepath)
}
func HealthHandler(w http.ResponseWriter, r *http.Request) { func HealthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }

BIN
uploads/Cat03.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 273 KiB

View File

@ -4,7 +4,8 @@
<title>CCTV</title> <title>CCTV</title>
</head> </head>
<body> <body>
<h1>Latest Picture</h1> <h1>Latest Picture</h1>
<img src="{{.picture}}" alt="Latest Picture"> <img src="/uploads/Cat03.jpg" alt="Latest Picture">
<img src="{{.picture}}" alt="Latest Picture">
</body> </body>
</html> </html>