wip: add upload box

This commit is contained in:
jabuxas 2024-10-15 13:33:18 -03:00
parent f5cb446264
commit e87382bc77
3 changed files with 93 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"log" "log"
"log/slog" "log/slog"
"net/http" "net/http"
@ -65,6 +66,7 @@ func main() {
), ),
) )
mux.HandleFunc("/last", app.lastHandler) mux.HandleFunc("/last", app.lastHandler)
mux.HandleFunc("/upload", app.basicAuth(app.uploadButtonHandler))
srv := &http.Server{ srv := &http.Server{
Addr: app.port, Addr: app.port,

View File

@ -149,10 +149,32 @@ func (app *Application) uploadCurlHandler(w http.ResponseWriter, r *http.Request
app.uploadHandler(w, r) app.uploadHandler(w, r)
} }
func (app *Application) uploadButtonHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
app.uploadHandler(w, r)
return
}
tmpl := template.Must(template.ParseFiles("templates/upload.html"))
if err := tmpl.Execute(w, app.uploadHandler); err != nil {
slog.Warn(error.Error(err))
}
}
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) { func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
// if contentType := r.Header.Get("Content-Type"); contentType == "application/x-www-form-urlencoded" {
// content := r.FormValue("content")
// } else if contentType == "multipart/form-data" {
// }
}
func (app *Application) formFileHandler(w http.ResponseWriter, r *http.Request) {
file, handler, err := r.FormFile("file") file, handler, err := r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, "Error retrieving the file", http.StatusBadRequest) http.Error(w, "Error retrieving the file", http.StatusBadRequest)
slog.Warn(error.Error(err))
return return
} }
defer file.Close() defer file.Close()

69
templates/upload.html Normal file
View File

@ -0,0 +1,69 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>dir listing</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
header {
display: flex;
justify-content: center;
align-items: center;
}
.file-listing {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
.file-listing th,
.file-listing td {
border: 1px solid #ddd;
padding: 12px;
text-align: left;
}
.file-listing th {
background-color: #333;
color: white;
}
.file-listing tr:nth-child(even) {
background-color: #f9f9f9;
}
.file-listing tr:hover {
background-color: #f1f1f1;
}
.file-listing td a {
color: #0288d1;
text-decoration: none;
}
.file-listing td a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<form action="/upload" method="POST">
<textarea name="content" rows="10" cols="50" placeholder="Enter your content here..."></textarea><br /><br />
<button type="submit">Upload</button>
</form>
</body>
</html>