Add file icons

plus some fixes
This commit is contained in:
nxshock 2021-09-22 19:09:26 +05:00
parent 5fd8fc6573
commit f0ad3afff9
5 changed files with 67 additions and 14 deletions

View File

@ -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)
}

8
icons.go Normal file
View File

@ -0,0 +1,8 @@
package main
import (
"embed"
)
//go:embed icons/*.svg
var iconsFS embed.FS

View File

@ -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;
}
</style>
</head>
<body>
<header>
<span><svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15" fill="#61afef" stroke="#353b45"><circle cx="7.5" cy="7.5" r="5" fill="none" stroke="#61afef"/><circle cx="3" cy="10" r="2"/><circle cx="12" cy="10" r="2"/><circle cx="7.5" cy="2.5" r="2"/></svg> File Storage</span>
<span>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15" fill="#61afef" stroke="#353b45"><circle cx="7.5" cy="7.5" r="5" fill="none" stroke="#61afef"/><circle cx="3" cy="10" r="2"/><circle cx="12" cy="10" r="2"/><circle cx="7.5" cy="2.5" r="2"/></svg>
File Storage
</span>
<label>
<input id="file-uploader" type="file" id="upload-button">
Загрузить файл
@ -158,13 +158,16 @@
<th>Размер</th>
<th>Дата</th>
</tr>
{{range .}} <tr>
<td><a href="/download?filename={{.Name}}">{{.Name}}</a></td>
{{range .Files}} <tr>
<td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a></td>
<td><pre>{{.Size}}</pre></td>
<td>{{.Date}}</td>
</tr>
{{end}} </table>
</main>
<footer>
Файлы хранятся как минимум {{.StorageDuration}} ч.
</footer>
</body>
</html>

View File

@ -32,6 +32,7 @@ func init() {
}
http.HandleFunc("/", HandleRoot)
http.HandleFunc("/icon", HandleIcon)
http.HandleFunc("/upload", HandleUpload)
http.HandleFunc("/download", HandleDownload)
}

View File

@ -26,3 +26,11 @@ func sizeToApproxHuman(s int64) string {
return fmt.Sprintf("%.1f KiB", v)
}
func nvl(a1, a2 string) string {
if a1 != "" {
return a1
}
return a2
}