This commit is contained in:
2026-11-18 17:13:44 -05:00
parent a58d9af781
commit dc3b66cc58
5 changed files with 205 additions and 160 deletions
+2 -65
View File
@@ -8,7 +8,6 @@ import (
"encoding/json"
"errors"
"fmt"
"html/template"
"io"
"log"
"mime"
@@ -20,18 +19,9 @@ import (
"time"
"unicode/utf8"
"github.com/gabriel-vasile/mimetype"
"gorm.io/gorm"
)
type HomePageData struct {
Expiries []Expiry
Qt string
PastePlaceholder string
FileMaxSize string
PasteMaxSize string
}
func health(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
fmt.Fprintln(w, "OK")
@@ -56,20 +46,7 @@ type ContentInfoResponse struct {
Filename string `json:"-"`
}
func render(pages *template.Template, w http.ResponseWriter, name string, data any) {
w.Header().Set("Conten-Type", "text/html; charset=utf-8")
if err := pages.ExecuteTemplate(w, name, data); err != nil {
log.Printf("render %s: %v", name, err)
}
}
func staticPage(pages *template.Template, name string, qts []string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
render(pages, w, name, getRandomQt(qts))
}
}
func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template, qts []string) http.HandlerFunc {
func uploadHandler(db *gorm.DB, cfg Config, pages *Pages) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost && r.Method != http.MethodPut {
w.Header().Set("Allow", "POST,PUT")
@@ -281,7 +258,7 @@ func uploadHandler(db *gorm.DB, cfg Config, pages *template.Template, qts []stri
SHA256: hex.EncodeToString(upload.SHA256),
DeleteToken: upload.DeleteToken,
FileExtension: filepath.Ext(upload.Filename),
Qt: getRandomQt(qts),
Qt: getRandomQt(pages.Qts),
}
switch returnType {
@@ -466,43 +443,3 @@ func deleteHandler(db *gorm.DB, cfg Config) http.HandlerFunc {
w.WriteHeader(http.StatusNoContent)
}
}
func detectMimeType(file io.ReadSeeker) (string, error) {
if _, err := file.Seek(0, io.SeekStart); err != nil {
return "", fmt.Errorf("rewind file: %w", err)
}
mtype, dtcErr := mimetype.DetectReader(file)
if _, err := file.Seek(0, io.SeekStart); err != nil {
return "", fmt.Errorf("rewind file: %w", err)
}
if dtcErr != nil {
return "", fmt.Errorf("detect MIME type: %w", dtcErr)
}
return mtype.String(), nil
}
func normalizeMIMEType(contentType string) string {
const plaintext = "text/plain; charset=utf-8"
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return plaintext
}
if strings.HasSuffix(mediaType, "+xml") {
return plaintext
}
switch mediaType {
case "text/html", "text/javascript", "application/javascript",
"application/ecmascript", "text/ecmascript", "application/xml",
"text/xml", "text/xsl",
"text/css", "application/wasm":
return plaintext
case "application/pdf":
return "application/octet-stream"
default:
return contentType
}
}