feat: add /last for checking last uploaded file

This commit is contained in:
jabuxas 2024-09-18 10:03:53 -03:00
parent 468fb823e9
commit 4ed73c3086
2 changed files with 16 additions and 4 deletions

View File

@ -56,6 +56,7 @@ func main() {
"/tree/", "/tree/",
app.basicAuth(app.treeHandler), app.basicAuth(app.treeHandler),
) )
mux.HandleFunc("/last", app.lastHandler)
srv := &http.Server{ srv := &http.Server{
Addr: app.port, Addr: app.port,

View File

@ -20,6 +20,7 @@ type Application struct {
key string key string
filesDir string filesDir string
port string port string
lastUploadedFile string
} }
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) { 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) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" { if r.URL.Path != "/" {
http.Error(w, "Method not allowed", http.StatusUnauthorized) 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) http.Error(w, "Error copying the file", http.StatusInternalServerError)
} }
app.lastUploadedFile = filepath
if app.url == "" { if app.url == "" {
fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename) fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename)
} else { } else {