2023-09-11 17:09:08 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2023-09-12 18:06:45 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-09-11 17:09:08 +00:00
|
|
|
"net/http"
|
2023-09-12 18:06:45 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2023-09-11 17:09:08 +00:00
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-09-12 18:06:45 +00:00
|
|
|
"github.com/yosssi/gohtml"
|
2023-09-12 21:32:19 +00:00
|
|
|
"seedno.de/seednode/roulette/types"
|
2023-09-11 17:09:08 +00:00
|
|
|
)
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.Handle {
|
2023-09-12 18:06:45 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
indexDump := cache.List()
|
2023-09-12 18:06:45 +00:00
|
|
|
|
|
|
|
fileCount := len(indexDump)
|
|
|
|
|
|
|
|
var startIndex, stopIndex int
|
|
|
|
|
|
|
|
page, err := strconv.Atoi(p.ByName("page"))
|
|
|
|
if err != nil || page <= 0 {
|
|
|
|
startIndex = 0
|
|
|
|
stopIndex = fileCount
|
|
|
|
} else {
|
|
|
|
startIndex = ((page - 1) * int(PageLength))
|
|
|
|
stopIndex = (startIndex + int(PageLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
if startIndex > (fileCount - 1) {
|
|
|
|
indexDump = []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if stopIndex > fileCount {
|
|
|
|
stopIndex = fileCount
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(indexDump, func(p, q int) bool {
|
|
|
|
return strings.ToLower(indexDump[p]) < strings.ToLower(indexDump[q])
|
|
|
|
})
|
|
|
|
|
|
|
|
var htmlBody strings.Builder
|
|
|
|
htmlBody.WriteString(`<!DOCTYPE html><html lang="en"><head>`)
|
2023-09-13 14:26:15 +00:00
|
|
|
htmlBody.WriteString(faviconHtml)
|
2023-09-12 18:06:45 +00:00
|
|
|
htmlBody.WriteString(`<style>a{text-decoration:none;height:100%;width:100%;color:inherit;cursor:pointer}`)
|
|
|
|
htmlBody.WriteString(`table,td,tr{border:1px solid black;border-collapse:collapse}td{white-space:nowrap;padding:.5em}</style>`)
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<title>Index contains %d files</title></head><body><table>", fileCount))
|
|
|
|
if len(indexDump) > 0 {
|
|
|
|
for _, v := range indexDump[startIndex:stopIndex] {
|
|
|
|
var shouldSort = ""
|
|
|
|
|
|
|
|
if Sorting {
|
|
|
|
shouldSort = "?sort=asc"
|
|
|
|
}
|
2023-09-14 04:24:29 +00:00
|
|
|
htmlBody.WriteString(fmt.Sprintf("<tr><td><a href=\"%s%s%s%s\">%s</a></td></tr>\n", Prefix, mediaPrefix, v, shouldSort, v))
|
2023-09-12 18:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if PageLength != 0 {
|
|
|
|
var firstPage int = 1
|
|
|
|
var lastPage int
|
|
|
|
|
|
|
|
if fileCount%int(PageLength) == 0 {
|
|
|
|
lastPage = fileCount / int(PageLength)
|
|
|
|
} else {
|
|
|
|
lastPage = (fileCount / int(PageLength)) + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if paginate {
|
|
|
|
var prevStatus, nextStatus string = "", ""
|
|
|
|
|
|
|
|
if page <= 1 {
|
|
|
|
prevStatus = " disabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
if page >= lastPage {
|
|
|
|
nextStatus = " disabled"
|
|
|
|
}
|
|
|
|
|
|
|
|
prevPage := page - 1
|
|
|
|
if prevPage < 1 {
|
|
|
|
prevPage = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
nextPage := page + 1
|
|
|
|
if nextPage > lastPage {
|
|
|
|
nextPage = fileCount / int(PageLength)
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\">First</button>",
|
|
|
|
firstPage))
|
|
|
|
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\"%s>Prev</button>",
|
|
|
|
prevPage,
|
|
|
|
prevStatus))
|
|
|
|
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\"%s>Next</button>",
|
|
|
|
nextPage,
|
|
|
|
nextStatus))
|
|
|
|
|
|
|
|
htmlBody.WriteString(fmt.Sprintf("<button onclick=\"window.location.href = '/html/%d';\">Last</button>",
|
|
|
|
lastPage))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
htmlBody.WriteString(`</table></body></html>`)
|
|
|
|
|
|
|
|
b, err := io.WriteString(w, gohtml.Format(htmlBody.String()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served HTML index page (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-12 18:06:45 +00:00
|
|
|
humanReadableSize(b),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 19:28:21 +00:00
|
|
|
func serveIndexJson(args []string, index *fileCache, errorChannel chan<- error) httprouter.Handle {
|
2023-09-12 18:06:45 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
cachedFiles := index.List()
|
2023-09-12 18:06:45 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
fileCount := len(cachedFiles)
|
2023-09-12 18:06:45 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
sort.SliceStable(cachedFiles, func(p, q int) bool {
|
|
|
|
return strings.ToLower(cachedFiles[p]) < strings.ToLower(cachedFiles[q])
|
2023-09-12 18:06:45 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
var startIndex, stopIndex int
|
|
|
|
|
|
|
|
page, err := strconv.Atoi(p.ByName("page"))
|
|
|
|
if err != nil || page <= 0 {
|
|
|
|
startIndex = 0
|
|
|
|
stopIndex = fileCount
|
|
|
|
} else {
|
|
|
|
startIndex = ((page - 1) * int(PageLength))
|
|
|
|
stopIndex = (startIndex + int(PageLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
if startIndex > (fileCount - 1) {
|
2023-09-13 14:26:15 +00:00
|
|
|
cachedFiles = []string{}
|
2023-09-12 18:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if stopIndex > fileCount {
|
|
|
|
stopIndex = fileCount
|
|
|
|
}
|
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
response, err := json.MarshalIndent(cachedFiles[startIndex:stopIndex], "", " ")
|
2023-09-12 18:06:45 +00:00
|
|
|
if err != nil {
|
2023-09-15 19:28:21 +00:00
|
|
|
errorChannel <- err
|
2023-09-12 18:06:45 +00:00
|
|
|
|
|
|
|
serverError(w, r, nil)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served JSON index page (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-12 18:06:45 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func serveAvailableExtensions() httprouter.Handle {
|
2023-09-13 03:35:15 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
response := []byte(types.SupportedFormats.GetExtensions())
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
w.Write(response)
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served available extensions list (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-13 04:35:17 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
2023-09-13 03:35:15 +00:00
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func serveEnabledExtensions(formats *types.Types) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
startTime := time.Now()
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
response := []byte(formats.GetExtensions())
|
2023-09-13 03:35:15 +00:00
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-13 03:35:15 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func serveAvailableMimeTypes() httprouter.Handle {
|
2023-09-13 03:35:15 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
response := []byte(types.SupportedFormats.GetMimeTypes())
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
w.Write(response)
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served available MIME types list (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-13 04:35:17 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
2023-09-13 03:35:15 +00:00
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
|
|
|
|
w.Header().Set("Content-Type", "text/plain")
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
startTime := time.Now()
|
2023-09-13 03:35:15 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
response := []byte(formats.GetMimeTypes())
|
2023-09-13 03:35:15 +00:00
|
|
|
|
|
|
|
w.Write(response)
|
|
|
|
|
|
|
|
if Verbose {
|
|
|
|
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
|
2023-09-13 14:26:15 +00:00
|
|
|
startTime.Format(logDate),
|
2023-09-13 03:35:15 +00:00
|
|
|
humanReadableSize(len(response)),
|
|
|
|
realIP(r),
|
|
|
|
time.Since(startTime).Round(time.Microsecond),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-13 17:00:24 +00:00
|
|
|
|
2023-09-15 19:28:21 +00:00
|
|
|
func registerInfoHandlers(mux *httprouter.Router, args []string, cache *fileCache, formats *types.Types, errorChannel chan<- error) {
|
2023-09-13 17:00:24 +00:00
|
|
|
if Cache {
|
2023-09-14 04:24:29 +00:00
|
|
|
register(mux, Prefix+"/html/", serveIndexHtml(args, cache, false))
|
2023-09-13 17:00:24 +00:00
|
|
|
if PageLength != 0 {
|
2023-09-14 04:24:29 +00:00
|
|
|
register(mux, Prefix+"/html/:page", serveIndexHtml(args, cache, true))
|
2023-09-13 17:00:24 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 19:28:21 +00:00
|
|
|
register(mux, Prefix+"/json", serveIndexJson(args, cache, errorChannel))
|
2023-09-13 17:00:24 +00:00
|
|
|
if PageLength != 0 {
|
2023-09-15 19:28:21 +00:00
|
|
|
register(mux, Prefix+"/json/:page", serveIndexJson(args, cache, errorChannel))
|
2023-09-13 17:00:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 04:24:29 +00:00
|
|
|
register(mux, Prefix+"/available_extensions", serveAvailableExtensions())
|
|
|
|
register(mux, Prefix+"/enabled_extensions", serveEnabledExtensions(formats))
|
|
|
|
register(mux, Prefix+"/available_mime_types", serveAvailableMimeTypes())
|
|
|
|
register(mux, Prefix+"/enabled_mime_types", serveEnabledMimeTypes(formats))
|
2023-09-13 17:00:24 +00:00
|
|
|
}
|