every file sharing service needs qts
honestly this code might be kind of shit, and it needs to used on the other page(s) as well but im too lazy, fixed a styling issue with the upload result page though
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"log"
|
||||
"math/rand/v2"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -39,16 +43,42 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
qtFiles, err := os.ReadDir("qts")
|
||||
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var qts []string
|
||||
for _, qt := range qtFiles {
|
||||
if qt.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
switch strings.ToLower(filepath.Ext(qt.Name())) {
|
||||
case ".jpeg", "jpg", ".jxl", ".png", ".webp", "avif", ".gif":
|
||||
qts = append(qts, "/qts/"+url.PathEscape(qt.Name()))
|
||||
}
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
fs := http.FileServer(http.Dir("static"))
|
||||
mux.Handle("GET /css/", fs)
|
||||
mux.Handle("GET /js/", fs)
|
||||
mux.Handle("GET /static/", http.StripPrefix("/static/", fs))
|
||||
mux.Handle("GET /qts/", http.StripPrefix(
|
||||
"/qts/",
|
||||
http.FileServer((http.Dir("qts"))),
|
||||
))
|
||||
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
||||
data := HomePageData{
|
||||
Expiries: config.Expiries,
|
||||
}
|
||||
|
||||
if len(qts) > 0 {
|
||||
log.Println(len(qts))
|
||||
data.Qt = qts[rand.IntN(len(qts))]
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
|
||||
if err := pages.ExecuteTemplate(w, "home.html", data); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user