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" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
) )
func HandleRoot(w http.ResponseWriter, r *http.Request) { func HandleRoot(w http.ResponseWriter, r *http.Request) {
@ -17,12 +18,18 @@ func HandleRoot(w http.ResponseWriter, r *http.Request) {
} }
type FileInfo struct { type FileInfo struct {
Ext string
Name string Name string
Size string Size string
Date 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 { err := filepath.Walk(config.StoragePath, func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
@ -31,7 +38,14 @@ func HandleRoot(w http.ResponseWriter, r *http.Request) {
if info.IsDir() { if info.IsDir() {
return nil 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 return nil
}) })
@ -91,7 +105,7 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
filename := filepath.Base(r.FormValue("filename")) filename := filepath.Base(r.FormValue("filename"))
if 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 return
} }
@ -115,3 +129,22 @@ func HandleDownload(w http.ResponseWriter, r *http.Request) {
io.CopyBuffer(w, f, make([]byte, 4096)) 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; vertical-align: middle;
width: 1em; width: 1em;
height: 1em; height: 1em;
margin: 0;
padding: 0;
} }
header, footer { header, footer {
@ -87,12 +89,12 @@
} }
td:nth-child(2) { td:nth-child(2) {
width: 6em; width: 5em;
text-align: right; text-align: right;
} }
td:nth-child(3) { td:nth-child(3) {
width: 10em; width: 6em;
text-align: right; text-align: right;
} }
@ -129,20 +131,18 @@
margin: 0; margin: 0;
padding: .5em; padding: .5em;
cursor: pointer; cursor: pointer;
color: #ffffff; color: #fff;
text-shadow: 1px 1px 1px #000; text-shadow: 1px 1px 1px #000;
} }
svg {
margin: 0;
vertical-align: middle;
}
</style> </style>
</head> </head>
<body> <body>
<header> <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> <label>
<input id="file-uploader" type="file" id="upload-button"> <input id="file-uploader" type="file" id="upload-button">
Загрузить файл Загрузить файл
@ -158,13 +158,16 @@
<th>Размер</th> <th>Размер</th>
<th>Дата</th> <th>Дата</th>
</tr> </tr>
{{range .}} <tr> {{range .Files}} <tr>
<td><a href="/download?filename={{.Name}}">{{.Name}}</a></td> <td><img src="/icon?ext={{.Ext}}"> <a href="/download?filename={{.Name}}">{{.Name}}</a></td>
<td><pre>{{.Size}}</pre></td> <td><pre>{{.Size}}</pre></td>
<td>{{.Date}}</td> <td>{{.Date}}</td>
</tr> </tr>
{{end}} </table> {{end}} </table>
</main> </main>
<footer>
Файлы хранятся как минимум {{.StorageDuration}} ч.
</footer>
</body> </body>
</html> </html>

View File

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

View File

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