1
0
mirror of https://github.com/nxshock/gallery.git synced 2024-11-27 00:11:00 +05:00
gallery/handlers.go

73 lines
1.7 KiB
Go
Raw Normal View History

2023-01-08 14:52:16 +05:00
package main
import (
"log"
"net/http"
"path/filepath"
"sort"
"strings"
)
func getResponse(path string) ([]Item, error) {
fileNames, err := filepath.Glob(filepath.Join(path, "*"))
if err != nil {
return nil, err
}
for i := range fileNames {
2023-04-09 21:48:40 +05:00
fileNames[i] = strings.TrimPrefix(filepath.ToSlash(fileNames[i]), filepath.ToSlash(config.WorkingDirectory))[1:] // TODO: check is first slash properly removed
2023-01-08 14:52:16 +05:00
}
items := make([]Item, 0)
for _, fileName := range fileNames {
itemType, err := getType(filepath.Join(config.WorkingDirectory, fileName))
if err != nil {
log.Println(err)
continue
}
if itemType == Unknown {
continue
}
items = append(items, Item{itemType, Path(fileName)})
}
sort.Slice(items, func(i, j int) bool {
return items[i].ItemType < items[j].ItemType
})
return items, nil
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
items, err := getResponse(filepath.Join(config.WorkingDirectory, r.URL.Path[1:]))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_ = t.Execute(w, items)
}
func previewHandler(w http.ResponseWriter, r *http.Request) {
fileName := strings.TrimPrefix(r.URL.Path, "/preview/")
b, err := previewCache.Read(filepath.Join(config.WorkingDirectory, fileName))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "image/avif")
w.Header().Set("Cache-Control", "max-age=31536000")
_, _ = w.Write(b)
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
fileName := strings.TrimPrefix(r.URL.Path, "/view/")
http.ServeFile(w, r, filepath.Join(config.WorkingDirectory, fileName))
}