feat(gallery): add gallery feature

This commit is contained in:
hgc 2023-04-15 10:10:51 +00:00
parent a04c4afbdd
commit 4ee3d438cb

23
main.go
View File

@ -3,10 +3,12 @@ package main
import (
"crypto/rand"
_ "embed"
"html"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/gabriel-vasile/mimetype"
@ -34,6 +36,19 @@ func shortID(length int64) string {
var db *bolt.DB
func GalleryHandler(w http.ResponseWriter, r *http.Request) {
v := mux.Vars(r)
w.Write([]byte(`<!DOCTYPE html><html><head><style>body { background-color: black; color: white; }</style></head><body>`))
for _, i := range strings.Split(v["files"], ",") {
link := viper.GetString("vhost") + `/u/` + i
w.Write([]byte(`<p>` + html.EscapeString(i) + `</p><a href="` + html.EscapeString(link) + `">` + `<img width=500em src="` + html.EscapeString(link) + `"></img></a>`))
}
w.Write([]byte(`</body></html>`))
}
func UploadHandler(w http.ResponseWriter, r *http.Request) {
// url_len sanitize
inpUrlLen := r.FormValue("url_len")
@ -135,7 +150,7 @@ func main() {
viper.SetDefault("bind", "127.0.0.1:8000")
viper.SetDefault("database", "filehole.db")
viper.SetDefault("filedir", "./data")
viper.SetDefault("vhost", "http://127.0.0.1:8000")
viper.SetDefault("vhost", "http://127.0.0.1:8000")
viper.SetConfigName("config")
viper.SetConfigType("toml")
@ -188,7 +203,11 @@ func main() {
r := mux.NewRouter()
r.PathPrefix("/u/").Handler(http.StripPrefix("/u/", NoDirectoryList(http.FileServer(http.Dir(viper.GetString("filedir"))))))
// Serve multiple images in a gallery
r.HandleFunc("/g/{files}", GalleryHandler)
// Serve files from data dir statically
r.PathPrefix("/u/").Handler(http.StripPrefix("/u/", NoDirectoryList(http.FileServer(http.Dir(viper.GetString("filedir"))))))
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(indexPage)