Add Type() method to types.Type, to allow displaying served file size for both embedded and inline files

This commit is contained in:
Seednode 2023-09-15 15:13:45 -05:00
parent e67384caeb
commit d4cf9cfa4f
12 changed files with 85 additions and 22 deletions

View File

@ -36,7 +36,7 @@ func notFound(w http.ResponseWriter, r *http.Request, path string) error {
startTime := time.Now()
if Verbose {
fmt.Printf("%s | Unavailable file %s requested by %s\n",
fmt.Printf("%s | Error: Unavailable file %s requested by %s\n",
startTime.Format(logDate),
path,
r.RemoteAddr,
@ -58,7 +58,7 @@ func serverError(w http.ResponseWriter, r *http.Request, i interface{}) {
startTime := time.Now()
if Verbose {
fmt.Printf("%s | Invalid request for %s from %s\n",
fmt.Printf("%s | Error: Invalid request for %s from %s\n",
startTime.Format(logDate),
r.URL.Path,
r.RemoteAddr,

View File

@ -79,6 +79,19 @@ func humanReadableSize(bytes int) string {
float64(bytes)/float64(div), "KMGTPE"[exp])
}
func kill(path string, cache *fileCache) error {
err := os.Remove(path)
if err != nil {
return err
}
if Cache {
cache.remove(path)
}
return nil
}
func preparePath(path string) string {
if runtime.GOOS == "windows" {
return fmt.Sprintf("%s/%s", mediaPrefix, filepath.ToSlash(path))
@ -218,7 +231,7 @@ func pathIsValid(path string, paths []string) bool {
switch {
case Verbose && !matchesPrefix:
fmt.Printf("%s | Error: Failed to serve file outside specified path(s): %s\n",
fmt.Printf("%s | Error: File outside specified path(s): %s\n",
time.Now().Format(logDate),
path,
)
@ -422,7 +435,7 @@ Poll:
}
if Verbose {
fmt.Printf("%s | Indexed %d/%d files across %d/%d directories in %s\n",
fmt.Printf("%s | Index: %d/%d files across %d/%d directories in %s\n",
time.Now().Format(logDate),
stats.filesMatched,
stats.filesMatched+stats.filesSkipped,

View File

@ -123,7 +123,7 @@ func serveIndexHtml(args []string, cache *fileCache, paginate bool) httprouter.H
}
if Verbose {
fmt.Printf("%s | Served HTML index page (%s) to %s in %s\n",
fmt.Printf("%s | Serve: HTML index page (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(b),
realIP(r),
@ -178,7 +178,7 @@ func serveIndexJson(args []string, index *fileCache, errorChannel chan<- error)
w.Write(response)
if Verbose {
fmt.Printf("%s | Served JSON index page (%s) to %s in %s\n",
fmt.Printf("%s | Serve: JSON index page (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(len(response)),
realIP(r),
@ -199,7 +199,7 @@ func serveAvailableExtensions() httprouter.Handle {
w.Write(response)
if Verbose {
fmt.Printf("%s | Served available extensions list (%s) to %s in %s\n",
fmt.Printf("%s | Serve: Available extension list (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(len(response)),
realIP(r),
@ -220,7 +220,7 @@ func serveEnabledExtensions(formats *types.Types) httprouter.Handle {
w.Write(response)
if Verbose {
fmt.Printf("%s | Served registered extensions list (%s) to %s in %s\n",
fmt.Printf("%s | Serve: Registered extension list (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(len(response)),
realIP(r),
@ -241,7 +241,7 @@ func serveAvailableMimeTypes() httprouter.Handle {
w.Write(response)
if Verbose {
fmt.Printf("%s | Served available MIME types list (%s) to %s in %s\n",
fmt.Printf("%s | Served available MIME type list (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(len(response)),
realIP(r),
@ -262,7 +262,7 @@ func serveEnabledMimeTypes(formats *types.Types) httprouter.Handle {
w.Write(response)
if Verbose {
fmt.Printf("%s | Served registered MIME types list (%s) to %s in %s\n",
fmt.Printf("%s | Served registered MIME type list (%s) to %s in %s\n",
startTime.Format(logDate),
humanReadableSize(len(response)),
realIP(r),

View File

@ -11,7 +11,7 @@ import (
)
const (
ReleaseVersion string = "0.89.1"
ReleaseVersion string = "0.90.0"
)
var (

View File

@ -100,8 +100,17 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
fileSize := humanReadableSize(len(buf))
if Russian {
err = os.Remove(filePath)
refererUri, err := stripQueryParams(refererToUri(r.Referer()))
if err != nil {
errorChannel <- err
serverError(w, r, nil)
return
}
if Russian && refererUri != "" {
err = kill(filePath, cache)
if err != nil {
errorChannel <- err
@ -109,14 +118,10 @@ func serveStaticFile(paths []string, cache *fileCache, errorChannel chan<- error
return
}
if Cache {
cache.remove(filePath)
}
}
if Verbose {
fmt.Printf("%s | Served %s (%s) to %s in %s\n",
fmt.Printf("%s | Serve: %s (%s) to %s in %s\n",
startTime.Format(logDate),
filePath,
fileSize,
@ -201,7 +206,7 @@ func serveRoot(paths []string, regexes *regexes, cache *fileCache, formats *type
}
}
func serveMedia(paths []string, regexes *regexes, formats *types.Types, errorChannel chan<- error) httprouter.Handle {
func serveMedia(paths []string, regexes *regexes, cache *fileCache, formats *types.Types, errorChannel chan<- error) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
filters := &filters{
included: splitQueryParams(r.URL.Query().Get("include"), regexes),
@ -287,7 +292,11 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types, errorCha
htmlBody.WriteString(body)
htmlBody.WriteString(`</body></html>`)
_, err = io.WriteString(w, gohtml.Format(htmlBody.String()))
startTime := time.Now()
formattedPage := gohtml.Format(htmlBody.String())
_, err = io.WriteString(w, formattedPage)
if err != nil {
errorChannel <- err
@ -295,6 +304,22 @@ func serveMedia(paths []string, regexes *regexes, formats *types.Types, errorCha
return
}
if format.Type() != "embed" {
if Verbose {
fmt.Printf("%s | Serve: %s (%d) to %s in %s\n",
startTime.Format(logDate),
path,
len(formattedPage),
realIP(r),
time.Since(startTime).Round(time.Microsecond),
)
}
if Russian {
kill(path, cache)
}
}
}
}
@ -426,7 +451,7 @@ func ServePage(args []string) error {
register(mux, Prefix+"/favicon.ico", serveFavicons(errorChannel))
register(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, formats, errorChannel))
register(mux, Prefix+mediaPrefix+"/*media", serveMedia(paths, regexes, cache, formats, errorChannel))
register(mux, Prefix+sourcePrefix+"/*static", serveStaticFile(paths, cache, errorChannel))
@ -456,7 +481,7 @@ func ServePage(args []string) error {
fmt.Printf("%s | Error: %v\n", time.Now().Format(logDate), err)
if ExitOnError {
fmt.Printf("%s | Shutting down...\n", time.Now().Format(logDate))
fmt.Printf("%s | Error: Shutting down...\n", time.Now().Format(logDate))
srv.Shutdown(context.Background())
}

View File

@ -57,6 +57,10 @@ func (t Format) Validate(filePath string) bool {
return true
}
func (t Format) Type() string {
return "embed"
}
func New() Format {
return Format{}
}

View File

@ -221,6 +221,10 @@ func (t Format) Validate(filePath string) bool {
return true
}
func (t Format) Type() string {
return "html"
}
func New(theme string) Format {
return Format{
Theme: theme,

View File

@ -56,6 +56,10 @@ func (t Format) Validate(filePath string) bool {
return true
}
func (t Format) Type() string {
return "embed"
}
func New() Format {
return Format{}
}

View File

@ -120,6 +120,10 @@ func ImageDimensions(path string) (*dimensions, error) {
return &dimensions{width: decodedConfig.Width, height: decodedConfig.Height}, nil
}
func (t Format) Type() string {
return "embed"
}
func New() Format {
return Format{}
}

View File

@ -76,6 +76,10 @@ func (t Format) Validate(filePath string) bool {
return utf8.Valid(head)
}
func (t Format) Type() string {
return "html"
}
func New() Format {
return Format{}
}

View File

@ -15,6 +15,7 @@ var SupportedFormats = &Types{
}
type Type interface {
Type() string
Css() string
Title(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
Body(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)

View File

@ -63,6 +63,10 @@ func (t Format) Validate(filePath string) bool {
return true
}
func (t Format) Type() string {
return "embed"
}
func New() Format {
return Format{}
}