|
|
@ -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 = "<meta name='generator' content='distribusi-go' />" |
|
|
@ -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": "<section id=\"%s\">%s</section>", |
|
|
|
"generic": "<pre>%s</pre>", |
|
|
|
"html": `<section id="%s">%s</section>`, |
|
|
|
"generic": `<pre>%s</pre>`, |
|
|
|
}, |
|
|
|
"image": { |
|
|
|
"thumbnail": "<figure><a href=\"%s\"><img class='thumbnail' src='data:image/jpg;base64,%s'></a><figcaption>%s</figcaption></figure>", |
|
|
|
"generic": "<figure><a href=\"%s\"><img class='%s' loading='lazy' src=\"%s\"/></a><figcaption>%s</figcaption></figure>", |
|
|
|
"thumbnail": trimAllNewlines(`<figure> |
|
|
|
<a href="%s"> |
|
|
|
<img class="thumbnail" src="data:image/jpg;base64,%s"> |
|
|
|
</a> |
|
|
|
<figcaption>%s</figcaption> |
|
|
|
</figure>`), |
|
|
|
"generic": trimAllNewlines(`<figure> |
|
|
|
<a href="%s"> |
|
|
|
<img class=%s loading="lazy" src="%s"/> |
|
|
|
</a> |
|
|
|
<figcaption>%s</figcaption> |
|
|
|
</figure>`), |
|
|
|
}, |
|
|
|
"application": { |
|
|
|
"pdf": "<object data=\"%s\" class='pdf' type='application/pdf'><embed src=\"%s\" type='application/pdf'/></object>", |
|
|
|
"pdf": trimAllNewlines(`<object data="%s" class="pdf" type="application/pdf"> |
|
|
|
<embed src="%s" type="application/pdf"/> |
|
|
|
</object>`), |
|
|
|
}, |
|
|
|
"audio": { |
|
|
|
"generic": "<audio controls> <source src=\"%s\" type='audio/%s'> Your browser does not support the audio element. </audio>", |
|
|
|
"generic": trimAllNewlines(`<audio controls> |
|
|
|
<source src="%s" type="audio/%s"> |
|
|
|
Your browser does not support the audio element. |
|
|
|
</audio>`), |
|
|
|
}, |
|
|
|
"video": { |
|
|
|
"generic": "<video controls> <source src=\"%s\" type='video/%s'> </video>", |
|
|
|
"generic": trimAllNewlines(`<video controls> |
|
|
|
<source src="%s" type="video/%s"> |
|
|
|
</video>`), |
|
|
|
}, |
|
|
|
"unknown": { |
|
|
|
"generic": "<a class='%s' href=\"%s\">%s</a>", |
|
|
|
"generic": `<a class="%s" href="%s">%s</a>`, |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
@ -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("<span class='filename'>%s</span>", fname) |
|
|
|
|
|
|
|
ftype, stype := parseMtype(mtype) |
|
|
|
|
|
|
|
if ftype == "text" { |
|
|
|
divTag := "<div id=\"%s\" class='%s'>%s%s</div>" |
|
|
|
div = fmt.Sprintf(divTag, fname, ftype, href, filename) |
|
|
|
divTemplate = "<div id=\"%s\" class='%s'>%s%s</div>" |
|
|
|
div = fmt.Sprintf(divTemplate, fname, ftype, href, filename) |
|
|
|
strippedDebugOutput = fmt.Sprintf(divTemplate, fname, ftype, logStripMsg, filename) |
|
|
|
} else if ftype == "os" { |
|
|
|
divTag := "<div id=\"%s\" class='x-%s'>%s</div>" |
|
|
|
div = fmt.Sprintf(divTag, fname, stype, href) |
|
|
|
if stype == "directory" { |
|
|
|
divTemplate = "<div id=\"%s\" class='x-%s'>%s</div>" |
|
|
|
div = fmt.Sprintf(divTemplate, fname, stype, href) |
|
|
|
} else { |
|
|
|
// don't include filename since link already has it
|
|
|
|
divTemplate = "<div id=\"%s\" class='%s'>%s</div>" |
|
|
|
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 := "<div id=\"%s\" class='%s'>%s</div>" |
|
|
|
div = fmt.Sprintf(divTag, fname, ftype, href) |
|
|
|
// don't include filename since link already has it
|
|
|
|
divTemplate = "<div id=\"%s\" class='%s'>%s</div>" |
|
|
|
div = fmt.Sprintf(divTemplate, fname, ftype, href) |
|
|
|
} else { |
|
|
|
// images, videos, etc. still get a filename
|
|
|
|
divTag := "<div id=\"%s\" class='%s'>%s%s</div>" |
|
|
|
div = fmt.Sprintf(divTag, fname, ftype, href, filename) |
|
|
|
divTemplate = "<div id=\"%s\" class='%s'>%s%s</div>" |
|
|
|
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 |
|
|
|
} |
|
|
|