diff --git a/handlers.go b/handlers.go index 9383ee4..23a0916 100644 --- a/handlers.go +++ b/handlers.go @@ -8,6 +8,7 @@ import ( "os" "path/filepath" "strconv" + "strings" ) func HandleRoot(w http.ResponseWriter, r *http.Request) { @@ -17,12 +18,18 @@ func HandleRoot(w http.ResponseWriter, r *http.Request) { } type FileInfo struct { + Ext string Name string Size string Date string } - var data []FileInfo + var data struct { + Files []FileInfo + StorageDuration uint + } + + data.StorageDuration = config.RemoveFilePeriod err := filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -31,7 +38,14 @@ func HandleRoot(w http.ResponseWriter, r *http.Request) { if info.IsDir() { return nil } - data = append(data, FileInfo{filepath.Base(path), sizeToApproxHuman(info.Size()), info.ModTime().Format("02.01.2006 15:04")}) + + fileInfo := FileInfo{ + Ext: nvl(strings.ToLower(strings.TrimPrefix(filepath.Ext(path), ".")), "dat"), + Name: filepath.Base(path), + Size: sizeToApproxHuman(info.Size()), + Date: info.ModTime().Format("02.01.2006")} + + data.Files = append(data.Files, fileInfo) return nil }) @@ -91,7 +105,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { filename := filepath.Base(r.FormValue("filename")) if filename == "" { - http.Error(w, `"filename" field can not be empty`, http.StatusBadRequest) + http.Error(w, `"filename" field can't be empty`, http.StatusBadRequest) return } @@ -115,3 +129,22 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) { io.CopyBuffer(w, f, make([]byte, 4096)) } + +func HandleIcon(w http.ResponseWriter, r *http.Request) { + ext := r.FormValue("ext") + + if ext == "" { + http.Error(w, `"ext" field can't be empty`, http.StatusBadRequest) + return + } + + f, err := iconsFS.Open(filepath.ToSlash(filepath.Join("icons", ext+".svg"))) + if err != nil { + f, _ = iconsFS.Open(filepath.ToSlash(filepath.Join("icons", "bin.svg"))) + } + + w.Header().Set("Content-Type", "image/svg+xml") + w.Header().Set("Cache-Control", "public, max-age=31557600") + + io.Copy(w, f) +} diff --git a/icons.go b/icons.go new file mode 100644 index 0000000..021c235 --- /dev/null +++ b/icons.go @@ -0,0 +1,8 @@ +package main + +import ( + "embed" +) + +//go:embed icons/*.svg +var iconsFS embed.FS diff --git a/index.htm b/index.htm index a5798d0..f6c4e19 100644 --- a/index.htm +++ b/index.htm @@ -41,6 +41,8 @@ vertical-align: middle; width: 1em; height: 1em; + margin: 0; + padding: 0; } header, footer { @@ -87,12 +89,12 @@ } td:nth-child(2) { - width: 6em; + width: 5em; text-align: right; } td:nth-child(3) { - width: 10em; + width: 6em; text-align: right; } @@ -129,20 +131,18 @@ margin: 0; padding: .5em; cursor: pointer; - color: #ffffff; + color: #fff; text-shadow: 1px 1px 1px #000; } - - svg { - margin: 0; - vertical-align: middle; - }