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-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-09-08 15:12:06 +00:00
|
|
|
)
|
|
|
|
|
2022-10-20 22:12:29 +00:00
|
|
|
const (
|
|
|
|
LOGDATE string = "2006-01-02T15:04:05.000-07:00"
|
|
|
|
PREFIX string = "/src"
|
|
|
|
RedirectStatusCode int = http.StatusSeeOther
|
|
|
|
)
|
2022-10-20 19:22:01 +00:00
|
|
|
|
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-09-26 17:31:45 +00:00
|
|
|
type appHandler func(http.ResponseWriter, *http.Request) error
|
2022-09-10 23:03:04 +00:00
|
|
|
|
2022-09-26 17:31:45 +00:00
|
|
|
func (fn appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := fn(w, r); err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 00:09:05 +00:00
|
|
|
func notFound(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
w.WriteHeader(404)
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
|
|
|
|
htmlBody := `<html lang="en">
|
|
|
|
<head>
|
|
|
|
<style>
|
2022-10-29 00:18:34 +00:00
|
|
|
a{display:block;height:100%;width:100%;text-decoration:none;color:inherit;cursor:auto;}
|
|
|
|
</style>
|
|
|
|
<title>
|
2022-10-29 00:09:05 +00:00
|
|
|
Not Found
|
2022-10-29 00:18:34 +00:00
|
|
|
</title>
|
2022-10-29 00:09:05 +00:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<a href="/">404 page not found</a>
|
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
|
|
|
|
_, err := io.WriteString(w, htmlBody)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-20 01:37:12 +00:00
|
|
|
func splitQueryParams(query string) []string {
|
|
|
|
if query == "" {
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
2022-10-20 15:38:32 +00:00
|
|
|
params := strings.Split(query, ",")
|
|
|
|
|
|
|
|
for i := 0; i < len(params); i++ {
|
|
|
|
params[i] = strings.ToLower(params[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return params
|
2022-10-20 01:37:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateQueryParams(filters *Filters, sort string) string {
|
|
|
|
switch {
|
|
|
|
case Filter && !Sort:
|
|
|
|
return fmt.Sprintf("?include=%v&exclude=%v",
|
|
|
|
filters.GetIncludes(),
|
|
|
|
filters.GetExcludes(),
|
|
|
|
)
|
|
|
|
case !Filter && Sort:
|
|
|
|
return fmt.Sprintf("?sort=%v", sort)
|
|
|
|
case Filter && Sort:
|
|
|
|
return fmt.Sprintf("?include=%v&exclude=%v&sort=%v",
|
|
|
|
filters.GetIncludes(),
|
|
|
|
filters.GetExcludes(),
|
|
|
|
sort,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
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 createFilters(includes, excludes []string) Filters {
|
|
|
|
filters := Filters{}
|
|
|
|
|
|
|
|
if Filter {
|
|
|
|
filters.Includes = includes
|
|
|
|
filters.Excludes = excludes
|
|
|
|
}
|
|
|
|
|
|
|
|
return filters
|
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
func generateFilePath(filePath string) string {
|
|
|
|
htmlBody := PREFIX
|
2022-10-23 21:29:58 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
2022-11-05 16:00:42 +00:00
|
|
|
htmlBody += "/"
|
2022-10-23 21:29:58 +00:00
|
|
|
}
|
2022-11-05 16:00:42 +00:00
|
|
|
htmlBody += filePath
|
2022-10-23 21:29:58 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
return htmlBody
|
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-10-28 23:32:03 +00:00
|
|
|
func serveHtml(w http.ResponseWriter, r *http.Request, filePath, dimensions string) 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-05 16:00:42 +00:00
|
|
|
filters := Filters{}
|
|
|
|
if Filter {
|
|
|
|
filters = createFilters(splitQueryParams(r.URL.Query().Get("include")), splitQueryParams(r.URL.Query().Get("exclude")))
|
|
|
|
}
|
|
|
|
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody := `<html lang="en">
|
2022-09-08 15:12:06 +00:00
|
|
|
<head>
|
2022-10-29 00:52:53 +00:00
|
|
|
<style>
|
|
|
|
a{display:block;height:100%;width:100%;text-decoration:none}
|
|
|
|
img{max-width:100%;max-height:97vh;height:auto;}
|
|
|
|
</style>
|
|
|
|
<title>`
|
2022-10-28 23:32:03 +00:00
|
|
|
htmlBody += fmt.Sprintf("%v (%v)", fileName, dimensions)
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody += `</title>
|
2022-09-08 15:12:06 +00:00
|
|
|
</head>
|
2022-10-29 00:18:34 +00:00
|
|
|
<body>
|
2022-11-05 16:00:42 +00:00
|
|
|
<a href="/`
|
|
|
|
htmlBody += generateQueryParams(&filters, r.URL.Query().Get("sort"))
|
|
|
|
htmlBody += `"><img src="`
|
|
|
|
htmlBody += generateFilePath(filePath)
|
2022-09-08 20:57:52 +00:00
|
|
|
htmlBody += `"></img></a>
|
2022-09-09 00:11:07 +00:00
|
|
|
</body>
|
|
|
|
</html>`
|
|
|
|
|
2022-09-16 18:45:33 +00:00
|
|
|
_, err := io.WriteString(w, htmlBody)
|
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-05 16:00:42 +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-10-25 05:06:57 +00:00
|
|
|
filePath, err := filepath.EvalSymlinks(strings.TrimPrefix(prefixedFilePath, PREFIX))
|
|
|
|
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-10-29 00:09:05 +00:00
|
|
|
notFound(w, r)
|
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-10-29 00:09:05 +00:00
|
|
|
notFound(w, r)
|
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-10-20 22:55:25 +00:00
|
|
|
startTime.Format(LOGDATE),
|
|
|
|
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-09-26 17:31:45 +00:00
|
|
|
func serveStaticFileHandler(paths []string) appHandler {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
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-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
func serveHtmlHandler(paths []string, re regexp.Regexp, fileCache *[]string) appHandler {
|
2022-09-26 17:31:45 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-24 23:59:10 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
filters := createFilters(splitQueryParams(r.URL.Query().Get("include")), splitQueryParams(r.URL.Query().Get("exclude")))
|
2022-10-20 00:41:42 +00:00
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
sortOrder := r.URL.Query().Get("sort")
|
2022-09-18 18:07:46 +00:00
|
|
|
|
|
|
|
switch {
|
2022-11-05 16:00:42 +00:00
|
|
|
case r.URL.Path == "/" && refererUri != "":
|
|
|
|
path, err := splitPath(refererUri, re)
|
2022-10-18 21:40:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-05 16:00:42 +00:00
|
|
|
filePath, err := getNextFile(path, sortOrder)
|
2022-09-18 18:07:46 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-17 16:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if filePath == "" {
|
2022-11-05 16:00:42 +00:00
|
|
|
filePath, err = getNewFile(paths, &filters, sortOrder, re, fileCache)
|
2022-09-24 23:59:10 +00:00
|
|
|
switch {
|
|
|
|
case err != nil && err == ErrNoImagesFound:
|
|
|
|
http.NotFound(w, r)
|
2022-10-18 18:42:32 +00:00
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-10-20 01:37:12 +00:00
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 22:12:29 +00:00
|
|
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
2022-11-05 16:00:42 +00:00
|
|
|
case r.URL.Path == "/" && refererUri == "":
|
|
|
|
filePath, err := getNewFile(paths, &filters, sortOrder, re, fileCache)
|
|
|
|
switch {
|
|
|
|
case err != nil && err == ErrNoImagesFound:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
case err != nil:
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
2022-09-16 18:45:33 +00:00
|
|
|
|
2022-10-23 21:47:23 +00:00
|
|
|
newUrl := fmt.Sprintf("http://%v%v%v",
|
2022-10-23 21:16:40 +00:00
|
|
|
r.Host,
|
|
|
|
preparePath(filePath),
|
2022-10-20 01:37:12 +00:00
|
|
|
generateQueryParams(&filters, sortOrder),
|
|
|
|
)
|
2022-10-20 22:12:29 +00:00
|
|
|
http.Redirect(w, r, newUrl, RedirectStatusCode)
|
2022-09-18 18:07:46 +00:00
|
|
|
default:
|
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 {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
2022-10-29 00:09:05 +00:00
|
|
|
notFound(w, r)
|
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-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-18 18:17:29 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
if !image {
|
2022-10-29 00:09:05 +00:00
|
|
|
notFound(w, r)
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-28 23:32:03 +00:00
|
|
|
dimensions, err := getImageDimensions(filePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = serveHtml(w, r, filePath, dimensions)
|
2022-09-18 18:17:29 +00:00
|
|
|
if err != nil {
|
2022-09-26 17:31:45 +00:00
|
|
|
return err
|
2022-09-16 18:45:33 +00:00
|
|
|
}
|
2022-09-08 20:30:51 +00:00
|
|
|
}
|
2022-09-26 17:31:45 +00:00
|
|
|
|
|
|
|
return nil
|
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-10-23 18:45:49 +00:00
|
|
|
re := regexp.MustCompile(`(.+)([0-9]{3})(\..+)`)
|
|
|
|
|
2022-10-29 00:49:49 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2022-10-28 22:19:04 +00:00
|
|
|
fileCache := []string{}
|
|
|
|
|
|
|
|
http.Handle("/", serveHtmlHandler(paths, *re, &fileCache))
|
2022-10-23 21:29:58 +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
|
|
|
}
|