Compare commits
No commits in common. "e87382bc777a4fa87b71599ffcb32b7ac2cb6320" and "58c8321a18d47629718edbdf8e72f1b5a5673da6" have entirely different histories.
e87382bc77
...
58c8321a18
2
abyss.go
2
abyss.go
@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
@ -66,7 +65,6 @@ func main() {
|
||||
),
|
||||
)
|
||||
mux.HandleFunc("/last", app.lastHandler)
|
||||
mux.HandleFunc("/upload", app.basicAuth(app.uploadButtonHandler))
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: app.port,
|
||||
|
49
handlers.go
49
handlers.go
@ -60,7 +60,7 @@ func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Reques
|
||||
Name: file.Name(),
|
||||
Path: filepath.Join(r.URL.Path, file.Name()),
|
||||
Size: info.Size(),
|
||||
FormattedSize: FormatFileSize(info.Size()),
|
||||
FormattedSize: formatFileSize(info.Size()),
|
||||
})
|
||||
}
|
||||
|
||||
@ -74,9 +74,20 @@ func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Reques
|
||||
}
|
||||
}
|
||||
|
||||
func formatFileSize(size int64) string {
|
||||
if size < 1024 {
|
||||
return fmt.Sprintf("%d B", size)
|
||||
} else if size < 1024*1024 {
|
||||
return fmt.Sprintf("%.2f KB", float64(size)/1024)
|
||||
} else if size < 1024*1024*1024 {
|
||||
return fmt.Sprintf("%.2f MB", float64(size)/(1024*1024))
|
||||
}
|
||||
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
|
||||
}
|
||||
|
||||
func (app *Application) indexHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
app.uploadCurlHandler(w, r)
|
||||
app.uploadHandler(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
@ -135,46 +146,20 @@ func (app *Application) lastHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, app.lastUploadedFile)
|
||||
}
|
||||
|
||||
func (app *Application) uploadCurlHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.Error(w, "Method not allowed", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
if !CheckAuth(r, app.key) {
|
||||
if !app.checkAuth(r) {
|
||||
http.Error(w, "You're not authorized.", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
// 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")
|
||||
if err != nil {
|
||||
http.Error(w, "Error retrieving the file", http.StatusBadRequest)
|
||||
slog.Warn(error.Error(err))
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
@ -220,6 +205,10 @@ func (app *Application) formFileHandler(w http.ResponseWriter, r *http.Request)
|
||||
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
|
||||
}
|
||||
|
||||
func (app *Application) checkAuth(r *http.Request) bool {
|
||||
return r.Header.Get("X-Auth") == string(app.key)
|
||||
}
|
||||
|
||||
func (app *Application) basicAuth(next http.HandlerFunc) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
username, password, ok := r.BasicAuth()
|
||||
|
21
helpers.go
21
helpers.go
@ -1,21 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CheckAuth(r *http.Request, key string) bool {
|
||||
return r.Header.Get("X-Auth") == key
|
||||
}
|
||||
|
||||
func FormatFileSize(size int64) string {
|
||||
if size < 1024 {
|
||||
return fmt.Sprintf("%d B", size)
|
||||
} else if size < 1024*1024 {
|
||||
return fmt.Sprintf("%.2f KB", float64(size)/1024)
|
||||
} else if size < 1024*1024*1024 {
|
||||
return fmt.Sprintf("%.2f MB", float64(size)/(1024*1024))
|
||||
}
|
||||
return fmt.Sprintf("%.2f GB", float64(size)/(1024*1024*1024))
|
||||
}
|
@ -1,69 +0,0 @@
|
||||
<!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>
|
Loading…
Reference in New Issue
Block a user