2023-09-11 17:09:08 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-09-11 17:59:40 +00:00
|
|
|
"errors"
|
2023-09-11 17:09:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/yosssi/gohtml"
|
|
|
|
)
|
|
|
|
|
2023-09-11 17:59:40 +00:00
|
|
|
var (
|
2023-12-31 22:04:54 +00:00
|
|
|
ErrInvalidAdminPrefix = errors.New("admin path must match the pattern " + AllowedCharacters)
|
2024-01-01 17:51:28 +00:00
|
|
|
ErrInvalidConcurrency = errors.New("concurrency limit must be a positive integer")
|
2023-09-26 10:29:55 +00:00
|
|
|
ErrInvalidFileCountRange = errors.New("maximum file count limit must be greater than or equal to minimum file count limit")
|
2023-10-03 20:00:32 +00:00
|
|
|
ErrInvalidFileCountValue = errors.New("file count limits must be non-negative integers no greater than 2147483647")
|
2023-12-31 22:04:54 +00:00
|
|
|
ErrInvalidIgnoreFile = errors.New("ignore filename must match the pattern " + AllowedCharacters)
|
2023-09-26 09:03:52 +00:00
|
|
|
ErrInvalidPort = errors.New("listen port must be an integer between 1 and 65535 inclusive")
|
|
|
|
ErrNoMediaFound = errors.New("no supported media formats found which match all criteria")
|
2023-09-11 17:59:40 +00:00
|
|
|
)
|
|
|
|
|
2023-09-11 17:09:08 +00:00
|
|
|
func newErrorPage(title, body string) string {
|
|
|
|
var htmlBody strings.Builder
|
|
|
|
|
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
2023-09-13 14:26:15 +00:00
|
|
|
htmlBody.WriteString(faviconHtml)
|
2023-10-01 15:11:13 +00:00
|
|
|
htmlBody.WriteString(`<style>html,body,a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
|
2023-09-11 17:09:08 +00:00
|
|
|
htmlBody.WriteString(fmt.Sprintf("<title>%s</title></head>", title))
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<body><a href=\"/\">%s</a></body></html>", body))
|
|
|
|
|
|
|
|
return htmlBody.String()
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func notFound(w http.ResponseWriter, r *http.Request, path string) error {
|
2023-09-11 17:09:08 +00:00
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | ERROR: Unavailable file %s requested by %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
|
|
|
path,
|
2023-09-11 17:09:08 +00:00
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
_, err := io.WriteString(w, gohtml.Format(newErrorPage("Not Found", "404 Page not found")))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-12 18:06:45 +00:00
|
|
|
if Verbose {
|
2023-09-26 10:29:55 +00:00
|
|
|
fmt.Printf("%s | ERROR: Invalid request for %s from %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-11 17:09:08 +00:00
|
|
|
r.URL.Path,
|
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
2023-09-26 10:29:55 +00:00
|
|
|
io.WriteString(w, gohtml.Format(newErrorPage("Server Error", "An error has occurred. Please try again.")))
|
2023-09-11 17:09:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func serverErrorHandler() func(http.ResponseWriter, *http.Request, interface{}) {
|
|
|
|
return serverError
|
|
|
|
}
|