2022-09-08 15:12:06 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2022 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-09-10 23:03:04 +00:00
|
|
|
"fmt"
|
2022-09-08 15:12:06 +00:00
|
|
|
"io"
|
2022-11-10 06:26:21 +00:00
|
|
|
"log"
|
2022-10-29 00:49:49 +00:00
|
|
|
"math/rand"
|
2022-09-08 15:12:06 +00:00
|
|
|
"net/http"
|
2022-09-08 20:30:51 +00:00
|
|
|
"net/url"
|
2022-09-08 15:57:59 +00:00
|
|
|
"os"
|
2022-09-16 18:45:33 +00:00
|
|
|
"path/filepath"
|
2022-10-23 18:45:49 +00:00
|
|
|
"regexp"
|
2022-10-23 21:29:58 +00:00
|
|
|
"runtime"
|
2022-09-08 15:12:06 +00:00
|
|
|
"strconv"
|
2022-09-08 20:57:52 +00:00
|
|
|
"strings"
|
2022-09-10 23:03:04 +00:00
|
|
|
"time"
|
2022-11-10 05:17:19 +00:00
|
|
|
|
|
|
|
"github.com/yosssi/gohtml"
|
2022-09-08 15:12:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 22:12:29 +00:00
|
|
|
const (
|
2022-11-10 05:17:19 +00:00
|
|
|
LogDate string = `2006-01-02T15:04:05.000-07:00`
|
|
|
|
Prefix string = `/src`
|
2022-10-20 22:12:29 +00:00
|
|
|
RedirectStatusCode int = http.StatusSeeOther
|
|
|
|
)
|
2022-10-20 19:22:01 +00:00
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
type Regexes struct {
|
|
|
|
Alphanumeric *regexp.Regexp
|
|
|
|
Filename *regexp.Regexp
|
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
type Filters struct {
|
|
|
|
Includes []string
|
|
|
|
Excludes []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filters) IsEmpty() bool {
|
2022-11-05 16:00:42 +00:00
|
|
|
return !(f.HasIncludes() && f.HasExcludes())
|
2022-10-20 00:27:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
func (f *Filters) HasIncludes() bool {
|
2022-11-05 16:00:42 +00:00
|
|
|
return len(f.Includes) != 0
|
2022-10-20 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func (f *Filters) GetIncludes() string {
|
|
|
|
return strings.Join(f.Includes, ",")
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
func (f *Filters) HasExcludes() bool {
|
2022-11-05 16:00:42 +00:00
|
|
|
return len(f.Excludes) != 0
|
2022-10-20 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 00:27:11 +00:00
|
|
|
func (f *Filters) GetExcludes() string {
|
|
|
|
return strings.Join(f.Excludes, ",")
|
|
|
|
}
|
|
|
|
|
2022-11-10 06:26:21 +00:00
|
|
|
func notFound(w http.ResponseWriter, r *http.Request, filePath string) error {
|
|
|
|
startTime := time.Now()
|
2022-09-10 23:03:04 +00:00
|
|
|
|
2022-11-10 06:26:21 +00:00
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%v | Unavailable file %v requested by %v\n",
|
|
|
|
startTime.Format(LogDate),
|
|
|
|
filePath,
|
|
|
|
r.RemoteAddr,
|
|
|
|
)
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 00:09:05 +00:00
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
2022-11-10 06:26:21 +00:00
|
|
|
var htmlBody strings.Builder
|
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
|
|
|
htmlBody.WriteString(`<style>a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}</style>`)
|
|
|
|
htmlBody.WriteString(`<title>Not Found</title></head>`)
|
|
|
|
htmlBody.WriteString(`<body><a href="/">404 page not found</a></body></html>`)
|
|
|
|
|
|
|
|
_, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
|
2022-10-29 00:09:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
func getRefreshInterval(r *http.Request) string {
|
|
|
|
refreshInterval := r.URL.Query().Get("refresh")
|
|
|
|
|
|
|
|
num, err := strconv.Atoi(refreshInterval)
|
2022-11-11 15:19:53 +00:00
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
if err != nil || num < 0 {
|
|
|
|
refreshInterval = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
return refreshInterval
|
|
|
|
}
|
|
|
|
|
|
|
|
func getSortOrder(r *http.Request) string {
|
|
|
|
sortOrder := r.URL.Query().Get("sort")
|
2022-11-11 15:19:53 +00:00
|
|
|
if sortOrder == "asc" || sortOrder == "desc" {
|
|
|
|
return sortOrder
|
2022-11-10 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 15:19:53 +00:00
|
|
|
return ""
|
2022-11-10 16:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func splitQueryParams(query string, regexes *Regexes) []string {
|
2022-11-10 19:57:38 +00:00
|
|
|
results := []string{}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
if query == "" {
|
2022-11-10 19:57:38 +00:00
|
|
|
return results
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
params := strings.Split(query, ",")
|
|
|
|
|
|
|
|
for i := 0; i < len(params); i++ {
|
2022-11-10 19:57:38 +00:00
|
|
|
if regexes.Alphanumeric.MatchString(params[i]) {
|
|
|
|
results = append(results, strings.ToLower(params[i]))
|
2022-11-10 16:09:39 +00:00
|
|
|
}
|
2022-10-20 15:38:32 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 19:57:38 +00:00
|
|
|
return results
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
func generateQueryParams(filters *Filters, sortOrder, refreshInterval string) string {
|
2022-11-09 01:24:49 +00:00
|
|
|
var hasParams bool
|
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
var queryParams strings.Builder
|
|
|
|
|
|
|
|
queryParams.WriteString("?")
|
2022-11-09 01:24:49 +00:00
|
|
|
|
|
|
|
if Filter {
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString("include=")
|
2022-11-09 01:24:49 +00:00
|
|
|
if filters.HasIncludes() {
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString(filters.GetIncludes())
|
2022-11-09 01:24:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString("&exclude=")
|
2022-11-09 01:24:49 +00:00
|
|
|
if filters.HasExcludes() {
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString(filters.GetExcludes())
|
2022-11-09 01:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hasParams = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if Sort {
|
|
|
|
if hasParams {
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString("&")
|
2022-11-09 01:24:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString(fmt.Sprintf("sort=%v", sortOrder))
|
2022-11-09 01:24:49 +00:00
|
|
|
|
|
|
|
hasParams = true
|
|
|
|
}
|
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
if hasParams {
|
|
|
|
queryParams.WriteString("&")
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
2022-11-10 06:04:47 +00:00
|
|
|
queryParams.WriteString(fmt.Sprintf("refresh=%v", refreshInterval))
|
2022-10-20 01:37:12 +00:00
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
return queryParams.String()
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
func stripQueryParams(u string) (string, error) {
|
|
|
|
uri, err := url.Parse(u)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
uri.RawQuery = ""
|
|
|
|
|
|
|
|
escapedUri, err := url.QueryUnescape(uri.String())
|
2022-09-24 23:59:10 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return "", err
|
2022-09-24 23:59:10 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
return strings.TrimPrefix(escapedUri, "/"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return escapedUri, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateFilePath(filePath string) string {
|
2022-11-11 15:19:53 +00:00
|
|
|
var htmlBody strings.Builder
|
|
|
|
|
|
|
|
htmlBody.WriteString(Prefix)
|
2022-10-23 21:29:58 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2022-11-11 15:19:53 +00:00
|
|
|
htmlBody.WriteString(`/`)
|
2022-10-23 21:29:58 +00:00
|
|
|
}
|
2022-11-11 15:19:53 +00:00
|
|
|
htmlBody.WriteString(filePath)
|
2022-10-23 21:29:58 +00:00
|
|
|
|
2022-11-11 15:19:53 +00:00
|
|
|
return htmlBody.String()
|
2022-09-24 23:59:10 +00:00
|
|
|
}
|
|
|
|
|
2022-09-17 16:07:37 +00:00
|
|
|
func refererToUri(referer string) string {
|
|
|
|
parts := strings.SplitAfterN(referer, "/", 4)
|
|
|
|
|
|
|
|
if len(parts) < 4 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return "/" + parts[3]
|
|
|
|
}
|
|
|
|
|
2022-11-10 05:17:19 +00:00
|
|
|
func serveHtml(w http.ResponseWriter, r *http.Request, filePath string, dimensions *Dimensions, filters *Filters) error {
|
2022-09-16 18:45:33 +00:00
|
|
|
fileName := filepath.Base(filePath)
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
sortOrder := getSortOrder(r)
|
2022-11-09 01:24:49 +00:00
|
|
|
|
2022-11-10 19:57:38 +00:00
|
|
|
refreshInterval := getRefreshInterval(r)
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
2022-11-09 01:24:49 +00:00
|
|
|
|
2022-11-10 05:33:13 +00:00
|
|
|
var htmlBody strings.Builder
|
2022-11-10 05:53:02 +00:00
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
2022-11-11 05:24:14 +00:00
|
|
|
htmlBody.WriteString(`<style>html,body{margin:0;padding:0;height:100%;}`)
|
|
|
|
htmlBody.WriteString(`a{display:block;height:100%;width:100%;text-decoration:none;}`)
|
|
|
|
htmlBody.WriteString(`img{margin:auto;display:block;max-width:97%;max-height:97%;object-fit:scale-down;`)
|
|
|
|
htmlBody.WriteString(`position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);}</style>`)
|
2022-11-10 05:33:13 +00:00
|
|
|
htmlBody.WriteString(fmt.Sprintf(`<title>%v (%vx%v)</title>`,
|
|
|
|
fileName,
|
|
|
|
dimensions.Width,
|
|
|
|
dimensions.Height))
|
|
|
|
htmlBody.WriteString(`</head><body>`)
|
2022-11-09 01:24:49 +00:00
|
|
|
if refreshInterval != "0" {
|
|
|
|
r, err := strconv.Atoi(refreshInterval)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
refreshTimer := strconv.Itoa(r * 1000)
|
2022-11-10 15:38:40 +00:00
|
|
|
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<script>window.onload = function(){setInterval(function(){window.location.href = '/%v';}, %v);};</script>",
|
2022-11-10 05:17:19 +00:00
|
|
|
queryParams,
|
2022-11-10 05:33:13 +00:00
|
|
|
refreshTimer))
|
2022-11-09 01:24:49 +00:00
|
|
|
}
|
2022-11-10 15:38:40 +00:00
|
|
|
htmlBody.WriteString(fmt.Sprintf(`<a href="/%v"><img src="%v" width="%v" height="%v" alt="Roulette selected: %v"></a>`,
|
|
|
|
queryParams,
|
|
|
|
generateFilePath(filePath),
|
|
|
|
dimensions.Width,
|
|
|
|
dimensions.Height,
|
|
|
|
fileName))
|
2022-11-10 05:33:13 +00:00
|
|
|
htmlBody.WriteString(`</body></html>`)
|
2022-09-09 00:11:07 +00:00
|
|
|
|
2022-11-10 06:04:47 +00:00
|
|
|
_, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
|
2022-09-08 20:57:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-04 02:35:02 +00:00
|
|
|
func serveStaticFile(w http.ResponseWriter, r *http.Request, paths []string) error {
|
2022-11-10 21:34:07 +00:00
|
|
|
prefixedFilePath, err := stripQueryParams(r.URL.Path)
|
2022-09-26 17:31:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
2022-09-10 21:17:55 +00:00
|
|
|
|
2022-11-10 21:34:07 +00:00
|
|
|
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, Prefix))
|
2022-10-25 05:06:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
if !pathIsValid(filePath, paths) {
|
2022-11-10 06:26:21 +00:00
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return nil
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
exists, err := fileExists(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-10 21:17:55 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
if !exists {
|
2022-11-10 06:26:21 +00:00
|
|
|
notFound(w, r, filePath)
|
2022-10-04 02:35:02 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-08 20:57:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 22:55:25 +00:00
|
|
|
startTime := time.Now()
|
2022-09-10 23:03:04 +00:00
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
buf, err := os.ReadFile(filePath)
|
2022-09-08 15:12:06 +00:00
|
|
|
if err != nil {
|
2022-09-08 20:57:52 +00:00
|
|
|
return err
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
2022-09-08 20:57:52 +00:00
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
|
2022-09-10 21:17:55 +00:00
|
|
|
if Verbose {
|
2022-10-25 05:11:55 +00:00
|
|
|
fmt.Printf("%v | Served %v (%v) to %v in %v\n",
|
2022-11-10 05:17:19 +00:00
|
|
|
startTime.Format(LogDate),
|
2022-10-20 22:55:25 +00:00
|
|
|
filePath,
|
2022-10-25 05:11:55 +00:00
|
|
|
humanReadableSize(len(buf)),
|
2022-10-20 22:55:25 +00:00
|
|
|
r.RemoteAddr,
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
2022-09-10 21:17:55 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-11-10 06:26:21 +00:00
|
|
|
func serveStaticFileHandler(paths []string) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-10-04 02:35:02 +00:00
|
|
|
err := serveStaticFile(w, r, paths)
|
2022-09-16 18:45:33 +00:00
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
func serveHtmlHandler(paths []string, regexes *Regexes, fileCache *[]string) http.HandlerFunc {
|
2022-11-10 06:26:21 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2022-10-20 01:37:12 +00:00
|
|
|
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
|
2022-09-26 17:31:45 +00:00
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-09-26 17:31:45 +00:00
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
filters := &Filters{
|
|
|
|
Includes: splitQueryParams(r.URL.Query().Get("include"), regexes),
|
|
|
|
Excludes: splitQueryParams(r.URL.Query().Get("exclude"), regexes),
|
|
|
|
}
|
2022-10-20 00:41:42 +00:00
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
sortOrder := getSortOrder(r)
|
2022-09-18 18:07:46 +00:00
|
|
|
|
2022-11-10 16:09:39 +00:00
|
|
|
refreshInterval := getRefreshInterval(r)
|
2022-11-09 01:24:49 +00:00
|
|
|
|
2022-11-05 16:17:31 +00:00
|
|
|
if r.URL.Path == "/" {
|
|
|
|
var filePath string
|
|
|
|
var err error
|
2022-10-18 21:40:13 +00:00
|
|
|
|
2022-11-05 16:17:31 +00:00
|
|
|
if refererUri != "" {
|
2022-11-10 16:09:39 +00:00
|
|
|
filePath, err = getNextFile(refererUri, sortOrder, regexes)
|
2022-11-05 16:17:31 +00:00
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-11-05 16:17:31 +00:00
|
|
|
}
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filePath == "" {
|
2022-11-11 15:44:47 +00:00
|
|
|
filePath, err = getNewFile(paths, filters, sortOrder, regexes, fileCache)
|
2022-09-24 23:59:10 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && err == ErrNoImagesFound:
|
2022-11-10 06:26:21 +00:00
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
2022-10-18 18:42:32 +00:00
|
|
|
case err != nil:
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-10-18 18:42:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
queryParams := generateQueryParams(filters, sortOrder, refreshInterval)
|
2022-11-09 01:24:49 +00:00
|
|
|
|
2022-10-23 21:47:23 +00:00
|
|
|
newUrl := fmt.Sprintf("http://%v%v%v",
|
2022-10-23 21:29:58 +00:00
|
|
|
r.Host,
|
|
|
|
preparePath(filePath),
|
2022-11-09 01:24:49 +00:00
|
|
|
queryParams,
|
2022-10-20 01:37:12 +00:00
|
|
|
)
|
2022-10-20 22:12:29 +00:00
|
|
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
2022-11-05 16:17:31 +00:00
|
|
|
} else {
|
2022-10-23 21:47:23 +00:00
|
|
|
filePath := r.URL.Path
|
|
|
|
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
filePath = strings.TrimPrefix(filePath, "/")
|
|
|
|
}
|
2022-09-16 18:45:33 +00:00
|
|
|
|
2022-10-20 22:55:25 +00:00
|
|
|
exists, err := fileExists(filePath)
|
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-10-20 22:55:25 +00:00
|
|
|
}
|
|
|
|
if !exists {
|
2022-11-10 06:26:21 +00:00
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
2022-10-20 22:55:25 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
image, err := isImage(filePath)
|
2022-09-16 18:45:33 +00:00
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-09-18 18:17:29 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
if !image {
|
2022-11-10 06:26:21 +00:00
|
|
|
notFound(w, r, filePath)
|
|
|
|
|
|
|
|
return
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 23:32:03 +00:00
|
|
|
dimensions, err := getImageDimensions(filePath)
|
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-10-28 23:32:03 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
err = serveHtml(w, r, filePath, dimensions, filters)
|
2022-09-18 18:17:29 +00:00
|
|
|
if err != nil {
|
2022-11-10 06:26:21 +00:00
|
|
|
log.Fatal(err)
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func doNothing(http.ResponseWriter, *http.Request) {}
|
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func ServePage(args []string) error {
|
2022-10-25 05:06:57 +00:00
|
|
|
fmt.Printf("roulette v%v\n\n", Version)
|
2022-10-23 22:39:49 +00:00
|
|
|
|
2022-09-09 19:28:57 +00:00
|
|
|
paths, err := normalizePaths(args)
|
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-09 19:28:57 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
regexes := &Regexes{
|
|
|
|
Filename: regexp.MustCompile(`(.+)([0-9]{3})(\..+)`),
|
|
|
|
Alphanumeric: regexp.MustCompile(`^[a-zA-Z0-9]*$`),
|
|
|
|
}
|
2022-10-23 18:45:49 +00:00
|
|
|
|
2022-10-29 00:49:49 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
fileCache := &[]string{}
|
2022-10-28 22:19:04 +00:00
|
|
|
|
2022-11-11 15:44:47 +00:00
|
|
|
http.Handle("/", serveHtmlHandler(paths, regexes, fileCache))
|
2022-11-10 05:17:19 +00:00
|
|
|
http.Handle(Prefix+"/", http.StripPrefix(Prefix, serveStaticFileHandler(paths)))
|
2022-09-08 15:12:06 +00:00
|
|
|
http.HandleFunc("/favicon.ico", doNothing)
|
|
|
|
|
2022-10-18 21:54:01 +00:00
|
|
|
err = http.ListenAndServe(":"+strconv.FormatInt(int64(Port), 10), nil)
|
2022-09-16 19:45:54 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 19:45:54 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-08 15:12:06 +00:00
|
|
|
}
|