feat: add customizable file tree
This commit is contained in:
parent
914e303667
commit
39c6a1f570
16
abyss.go
16
abyss.go
@ -33,10 +33,6 @@ func main() {
|
||||
log.Fatal("basic auth password must be provided")
|
||||
}
|
||||
|
||||
if app.url == "" {
|
||||
slog.Warn("no root url detected, defaulting to localhost.")
|
||||
}
|
||||
|
||||
if app.key == "" {
|
||||
slog.Warn("no upload key detected")
|
||||
}
|
||||
@ -54,11 +50,19 @@ func main() {
|
||||
app.port = ":" + app.port
|
||||
}
|
||||
|
||||
if app.url == "" {
|
||||
slog.Warn("no root url detected, defaulting to localhost.")
|
||||
app.url = "localhost" + app.port
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/", app.indexHandler)
|
||||
mux.HandleFunc(
|
||||
mux.Handle(
|
||||
"/tree/",
|
||||
app.basicAuth(app.treeHandler),
|
||||
http.StripPrefix(
|
||||
"/tree",
|
||||
app.basicAuth(app.fileListingHandler),
|
||||
),
|
||||
)
|
||||
mux.HandleFunc("/last", app.lastHandler)
|
||||
|
||||
|
67
handlers.go
67
handlers.go
@ -7,9 +7,11 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
@ -24,8 +26,63 @@ type Application struct {
|
||||
lastUploadedFile string
|
||||
}
|
||||
|
||||
func (app *Application) treeHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.StripPrefix("/tree/", http.FileServer(http.Dir(app.filesDir))).ServeHTTP(w, r)
|
||||
type FileInfo struct {
|
||||
Name string
|
||||
Path string
|
||||
Size int64
|
||||
FormattedSize string
|
||||
}
|
||||
|
||||
type TemplateData struct {
|
||||
Files []FileInfo
|
||||
URL string
|
||||
}
|
||||
|
||||
func (app *Application) fileListingHandler(w http.ResponseWriter, r *http.Request) {
|
||||
dir := app.filesDir + r.URL.Path
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
var fileInfos []FileInfo
|
||||
for _, file := range files {
|
||||
filePath := filepath.Join(dir, file.Name())
|
||||
info, err := os.Stat(filePath)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fileInfos = append(fileInfos, FileInfo{
|
||||
Name: file.Name(),
|
||||
Path: filepath.Join(r.URL.Path, file.Name()),
|
||||
Size: info.Size(),
|
||||
FormattedSize: formatFileSize(info.Size()),
|
||||
})
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFiles("templates/dirlist.html"))
|
||||
templateData := TemplateData{
|
||||
Files: fileInfos,
|
||||
URL: app.url,
|
||||
}
|
||||
if err := tmpl.Execute(w, templateData); err != nil {
|
||||
slog.Warn(error.Error(err))
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
@ -137,11 +194,7 @@ func (app *Application) uploadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
app.lastUploadedFile = filepath
|
||||
|
||||
if app.url == "" {
|
||||
fmt.Fprintf(w, "http://localhost%s/%s\n", app.port, filename)
|
||||
} else {
|
||||
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
|
||||
}
|
||||
fmt.Fprintf(w, "http://%s/%s\n", app.url, filename)
|
||||
}
|
||||
|
||||
func (app *Application) checkAuth(r *http.Request) bool {
|
||||
|
87
templates/dirlist.html
Normal file
87
templates/dirlist.html
Normal file
@ -0,0 +1,87 @@
|
||||
<!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>
|
||||
<header>
|
||||
<h1>{{ .URL }}</h1>
|
||||
</header>
|
||||
|
||||
<table class="file-listing">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Size</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{range .Files}}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{{.Path}}">{{.Name}}</a>
|
||||
</td>
|
||||
<td>{{.FormattedSize}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user