From b69578d0ac2dfcaa49c4dd06aa1c0aca45829ecf Mon Sep 17 00:00:00 2001 From: decentral1se Date: Sat, 5 Feb 2022 17:10:29 +0100 Subject: [PATCH] fix: strip really verbose logs See https://git.vvvvvvaria.org/decentral1se/distribusi-go/issues/7 --- distribusi.go | 125 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 35 deletions(-) diff --git a/distribusi.go b/distribusi.go index c1ed955..6f42ea6 100644 --- a/distribusi.go +++ b/distribusi.go @@ -42,6 +42,8 @@ var port = ":3000" // exiftooInstalled tells us if the exiftool binary is installed or not. var exiftoolInstalled = true +var logStripMsg = "...stripped from logs for brevity..." + // generatedInDistribusi is an internal marker to help recognise when // distribusi-go has generated files. var generatedInDistribusi = "" @@ -114,24 +116,41 @@ type htmlTag = string // htmlTags is a fileType/subType to htmlTag mapping for HTML generation purposes. var htmlTags = map[fileType]map[subType]htmlTag{ "text": { - "html": "
%s
", - "generic": "
%s
", + "html": `
%s
`, + "generic": `
%s
`, }, "image": { - "thumbnail": "
%s
", - "generic": "
%s
", + "thumbnail": trimAllNewlines(`
+ + + +
%s
+
`), + "generic": trimAllNewlines(`
+ + + +
%s
+
`), }, "application": { - "pdf": "", + "pdf": trimAllNewlines(` + + `), }, "audio": { - "generic": "", + "generic": trimAllNewlines(``), }, "video": { - "generic": "", + "generic": trimAllNewlines(``), }, "unknown": { - "generic": "%s", + "generic": `%s`, }, } @@ -283,7 +302,7 @@ Example: if c.Bool("serve") { go func() { - logrus.Debug("starting up web server") + logrus.Debug("attempting to start up the web server") if err := serveHTTP(root); err != nil { ch <- err @@ -423,7 +442,7 @@ func distribusify(c *cli.Context, root string, ignore []string) error { continue } - unknown, href, err := getHref(c, file, mtype) + unknown, href, err := mkHref(c, file, mtype) if err != nil { logrus.Debugf("failed to generate href for %s", file) continue @@ -570,11 +589,18 @@ func trimFinalNewline(contents []byte) string { return strings.TrimSuffix(string(contents), "\n") } -// getHref figures out which href tag corresponds to which file by navigating +// trimAllNewlines removes all new lines. +func trimAllNewlines(contents string) string { + return strings.ReplaceAll(string(contents), "\n", "") +} + +// mkHref figures out which href tag corresponds to which file by navigating // the mimetype. If a type of file is unknown, this is signalled via the bool // return value. -func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { +func mkHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { var href string + var hrefTemplate string + var strippedDebugOutput string var unknown bool fname := filepath.Base(fpath) @@ -587,9 +613,13 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { } if stype == "html" { - href = fmt.Sprintf(htmlTags[ftype][stype], fname, trimFinalNewline(fcontents)) + hrefTemplate = htmlTags[ftype][stype] + href = fmt.Sprintf(hrefTemplate, fname, trimFinalNewline(fcontents)) + strippedDebugOutput = fmt.Sprintf(hrefTemplate, fname, logStripMsg) } else { - href = fmt.Sprintf(htmlTags[ftype]["generic"], trimFinalNewline(fcontents)) + hrefTemplate = htmlTags[ftype]["generic"] + href = fmt.Sprintf(hrefTemplate, trimFinalNewline(fcontents)) + strippedDebugOutput = fmt.Sprintf(hrefTemplate, logStripMsg) } } else if ftype == "image" { caption, err := getCaption(c, fpath) @@ -598,33 +628,46 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { } if stype == "gif" { - href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype, fname, caption) + hrefTemplate = htmlTags[ftype]["generic"] + href = fmt.Sprintf(hrefTemplate, fname, stype, fname, caption) } else { thumb, err := genThumb(c, fpath, caption) if err != nil { + hrefTemplate = htmlTags[ftype]["generic"] + href = fmt.Sprintf(hrefTemplate, fname, stype, fname, caption) logrus.Debugf("failed to generate thumbnail for %s, showing original image", fpath) - href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype, fname, caption) } else { - href = fmt.Sprintf(htmlTags[ftype]["thumbnail"], fname, thumb, caption) + hrefTemplate = htmlTags[ftype]["thumbnail"] + href = fmt.Sprintf(hrefTemplate, fname, thumb, caption) + strippedDebugOutput = fmt.Sprintf(hrefTemplate, fname, logStripMsg, caption) } } } else if ftype == "application" { if stype == "pdf" { - href = fmt.Sprintf(htmlTags[ftype][stype], fname, fname) + hrefTemplate = htmlTags[ftype][stype] + href = fmt.Sprintf(hrefTemplate, fname, fname) } else { unknown = true - href = fmt.Sprintf(htmlTags["unknown"]["generic"], stype, fname, fname) + hrefTemplate = htmlTags["unknown"]["generic"] + href = fmt.Sprintf(hrefTemplate, stype, fname, fname) } } else if ftype == "audio" { - href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype) + hrefTemplate = htmlTags[ftype]["generic"] + href = fmt.Sprintf(hrefTemplate, fname, stype) } else if ftype == "video" { - href = fmt.Sprintf(htmlTags[ftype]["generic"], fname, stype) + hrefTemplate = htmlTags[ftype]["generic"] + href = fmt.Sprintf(hrefTemplate, fname, stype) } else { unknown = true - href = fmt.Sprintf(htmlTags["unknown"]["generic"], stype, fname, fname) + hrefTemplate = htmlTags["unknown"]["generic"] + href = fmt.Sprintf(hrefTemplate, stype, fname, fname) } - logrus.Debugf("generated href for %s: %s", fname, href) + if strippedDebugOutput != "" { + logrus.Debugf("%s was wrapped in: %s", fname, strippedDebugOutput) + } else { + logrus.Debugf("%s was wrapped in: %s", fname, href) + } return unknown, href, nil } @@ -632,31 +675,43 @@ func getHref(c *cli.Context, fpath string, mtype string) (bool, string, error) { // mkDiv cosntructs a HTML div for inclusion in the generated index.html. func mkDiv(c *cli.Context, mtype string, href, fname string, unknown bool) (string, error) { var div string + var divTemplate string + var strippedDebugOutput string filename := fmt.Sprintf("%s", fname) ftype, stype := parseMtype(mtype) if ftype == "text" { - divTag := "
%s%s
" - div = fmt.Sprintf(divTag, fname, ftype, href, filename) + divTemplate = "
%s%s
" + div = fmt.Sprintf(divTemplate, fname, ftype, href, filename) + strippedDebugOutput = fmt.Sprintf(divTemplate, fname, ftype, logStripMsg, filename) } else if ftype == "os" { - divTag := "
%s
" - div = fmt.Sprintf(divTag, fname, stype, href) + if stype == "directory" { + divTemplate = "
%s
" + div = fmt.Sprintf(divTemplate, fname, stype, href) + } else { + // don't include filename since link already has it + divTemplate = "
%s
" + div = fmt.Sprintf(divTemplate, fname, ftype, href) + } } else { if unknown { - // we really don't know what this is, so the filename is the href and we - // avoid adding it again - divTag := "
%s
" - div = fmt.Sprintf(divTag, fname, ftype, href) + // don't include filename since link already has it + divTemplate = "
%s
" + div = fmt.Sprintf(divTemplate, fname, ftype, href) } else { - // images, videos, etc. still get a filename - divTag := "
%s%s
" - div = fmt.Sprintf(divTag, fname, ftype, href, filename) + divTemplate = "
%s%s
" + div = fmt.Sprintf(divTemplate, fname, ftype, href, filename) + strippedDebugOutput = fmt.Sprintf(divTemplate, fname, ftype, logStripMsg, filename) } } - logrus.Debugf("generated div wrapper for %s: %s", fname, div) + if strippedDebugOutput != "" { + logrus.Debugf("%s was wrapped in: %s", fname, strippedDebugOutput) + } else { + logrus.Debugf("%s was wrapped in: %s", fname, div) + } return div, nil }