From d4cf9cfa4fd062d0d035727d4dd1b6c4d097fa2c Mon Sep 17 00:00:00 2001 From: Seednode Date: Fri, 15 Sep 2023 15:13:45 -0500 Subject: [PATCH] Add Type() method to types.Type, to allow displaying served file size for both embedded and inline files --- cmd/errors.go | 4 ++-- cmd/files.go | 17 +++++++++++++-- cmd/info.go | 12 +++++------ cmd/root.go | 2 +- cmd/web.go | 47 ++++++++++++++++++++++++++++++++---------- types/audio/audio.go | 4 ++++ types/code/code.go | 4 ++++ types/flash/flash.go | 4 ++++ types/images/images.go | 4 ++++ types/text/text.go | 4 ++++ types/types.go | 1 + types/video/video.go | 4 ++++ 12 files changed, 85 insertions(+), 22 deletions(-) diff --git a/cmd/errors.go b/cmd/errors.go index f526a45..3a73672 100644 --- a/cmd/errors.go +++ b/cmd/errors.go @@ -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, diff --git a/cmd/files.go b/cmd/files.go index cc941c8..b7a7edb 100644 --- a/cmd/files.go +++ b/cmd/files.go @@ -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, diff --git a/cmd/info.go b/cmd/info.go index 93959ea..f2d1484 100644 --- a/cmd/info.go +++ b/cmd/info.go @@ -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), diff --git a/cmd/root.go b/cmd/root.go index edffa8d..39af472 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -11,7 +11,7 @@ import ( ) const ( - ReleaseVersion string = "0.89.1" + ReleaseVersion string = "0.90.0" ) var ( diff --git a/cmd/web.go b/cmd/web.go index 38b6f7d..daab36a 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -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(``) - _, 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()) } diff --git a/types/audio/audio.go b/types/audio/audio.go index a6a1a9f..8a46da7 100644 --- a/types/audio/audio.go +++ b/types/audio/audio.go @@ -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{} } diff --git a/types/code/code.go b/types/code/code.go index 6ebbf84..dba0412 100644 --- a/types/code/code.go +++ b/types/code/code.go @@ -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, diff --git a/types/flash/flash.go b/types/flash/flash.go index daa17ae..5a360fb 100644 --- a/types/flash/flash.go +++ b/types/flash/flash.go @@ -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{} } diff --git a/types/images/images.go b/types/images/images.go index 15e41cc..dc5e24d 100644 --- a/types/images/images.go +++ b/types/images/images.go @@ -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{} } diff --git a/types/text/text.go b/types/text/text.go index 7aad421..0e76c45 100644 --- a/types/text/text.go +++ b/types/text/text.go @@ -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{} } diff --git a/types/types.go b/types/types.go index f62e8c9..ebc0b8f 100644 --- a/types/types.go +++ b/types/types.go @@ -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) diff --git a/types/video/video.go b/types/video/video.go index 5c02025..7fca6bf 100644 --- a/types/video/video.go +++ b/types/video/video.go @@ -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{} }