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("/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)
}

View File

@ -24,7 +24,10 @@ func InitLog() {
var logFilePath string
if runtime.GOOS == "windows" {
logFilePath = filepath.Join("C:\\ProgramData\\CCTV", logFileName)
} else if runtime.GOOS == "darwin" {
logFilePath = filepath.Join("/Library/Logs", logFileName)
} else {
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) {
// get the newest picture name
pictures, err := os.ReadDir(UploadDir)
pictures, err := os.ReadDir("uploads")
if err != nil {
http.Error(w, "Error reading directory", http.StatusInternalServerError)
return
@ -278,15 +278,23 @@ func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
}
func HandleFileServer(dir, prefix string) http.HandlerFunc {
fs := http.FileServer(http.Dir(dir))
realHandler := http.StripPrefix(prefix, fs).ServeHTTP
func HandlerToHandlerFunc(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
LogFileServerRequest(getClientIP(r), r.URL.Path)
realHandler(w, r)
h.ServeHTTP(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) {
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>
</head>
<body>
<h1>Latest Picture</h1>
<img src="{{.picture}}" alt="Latest Picture">
<h1>Latest Picture</h1>
<img src="/uploads/Cat03.jpg" alt="Latest Picture">
<img src="{{.picture}}" alt="Latest Picture">
</body>
</html>