From 4ed73c30863f9d909e822084927710d2f1c20a5a Mon Sep 17 00:00:00 2001 From: jabuxas Date: Wed, 18 Sep 2024 10:03:53 -0300 Subject: [PATCH] feat: add /last for checking last uploaded file --- abyss.go | 1 + handlers.go | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/abyss.go b/abyss.go index 3119db6..3abbf0a 100644 --- a/abyss.go +++ b/abyss.go @@ -56,6 +56,7 @@ func main() { "/tree/", app.basicAuth(app.treeHandler), ) + mux.HandleFunc("/last", app.lastHandler) srv := &http.Server{ Addr: app.port, diff --git a/handlers.go b/handlers.go index 563f06e..27c0117 100644 --- a/handlers.go +++ b/handlers.go @@ -16,10 +16,11 @@ type Application struct { username string password string } - url string - key string - filesDir string - port string + url string + key string + filesDir string + port string + lastUploadedFile string } func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) { @@ -47,6 +48,14 @@ func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) { } } +func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) { + if app.lastUploadedFile == "" { + http.Error(w, "No new files uploaded yet", http.StatusNotFound) + return + } + http.ServeFile(w, r, app.lastUploadedFile) +} + func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/" { http.Error(w, "Method not allowed", http.StatusUnauthorized) @@ -87,6 +96,8 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { http.Error(w, "Error copying the file", http.StatusInternalServerError) } + app.lastUploadedFile = filepath + if app.url == "" { fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename) } else {