diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63cb519 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.pdf +*.png +main +pdf +thumbnails diff --git a/file name with spaces b/file name with spaces deleted file mode 100644 index 434de0e..0000000 --- a/file name with spaces +++ /dev/null @@ -1 +0,0 @@ -it has some text in it \ No newline at end of file diff --git a/main.go b/main.go index c0e8ff1..b1b34ff 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,16 @@ import ( "net/http" "net/url" "os" + "path/filepath" + "errors" + "os/exec" + "strings" _ "embed" ) +//go:embed tncmd.sh +var thumbnailCmd string + //go:embed styles.css var styleSheet string @@ -25,6 +32,10 @@ func (attrs htmlAttrs) String() string { return ret } +func imgWithTagAttrs(a htmlAttrs) string { + return fmt.Sprintf("", a.String()) +} + func wrapWithTagAttrs(str, tag string, a htmlAttrs) string { return fmt.Sprintf("<%s %s>%s", tag, a.String(), str, tag) } @@ -57,11 +68,22 @@ func serveStylesheet(w http.ResponseWriter, r *http.Request) { } +func createThumbnailsDirectory() { + if _, err := os.Stat("./thumbnails"); err != nil { + if os.IsNotExist(err) { + os.Mkdir("thumbnails", 0755) + } + return + } +} + func main() { port := flag.String("p", "6969", "port to serve on") directory := flag.String("d", ".", "the directory of static file to host") flag.Parse() + createThumbnailsDirectory() + var contents string entries, err := os.ReadDir(*directory) if err != nil { @@ -70,13 +92,45 @@ func main() { for _, e := range entries { new := e.Name() + newImg := strings.TrimSuffix(new, filepath.Ext(new)) + ".png" - if new == "style.css" { continue } + if filepath.Ext(new) != ".pdf" { + continue + } + + cmd := exec.Command("./tncmd.sh", e.Name(), *directory) + if errors.Is(cmd.Err, exec.ErrDot) { + cmd.Err = nil + } + + if err := cmd.Run(); err != nil { + log.Println(err) + } else { + fmt.Println("Created thumbnail for: " + new) + http.HandleFunc("/" + newImg, func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "img/png") + + file, err := os.Open(*directory +"/"+ newImg) + if err != nil { + http.Error(w, "File not found.", http.StatusNotFound) + return + } + defer file.Close() + + _, err = io.Copy(w, file) + if err != nil { + http.Error(w, "Error sending file.", http.StatusInternalServerError) + return + } + }) + } + + //if new == "style.css" { continue } http.HandleFunc("/" + url.QueryEscape(new), func(w http.ResponseWriter, r *http.Request) { log.Println("Request for resource: ", r.RequestURI) - file, err := os.Open(*directory + "/" + new) + file, err := os.Open(*directory + "/" + new) if err != nil { http.Error(w, "File not found.", http.StatusNotFound) return @@ -84,7 +138,7 @@ func main() { defer file.Close() w.Header().Set("Content-Disposition", "attachment; filename=" + new) - w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Type", "application/pdf") _, err = io.Copy(w, file) if err != nil { @@ -101,8 +155,15 @@ func main() { "href": e.Name(), } + imgAttrs := htmlAttrs { + "src": newImg, + } + + var img string + img = imgWithTagAttrs(imgAttrs) + var href string - href = wrapWithTagAttrs(e.Name(), "a", hrefAttrs) + href = wrapWithTagAttrs(img, "a", hrefAttrs) contents += wrapWithTagAttrs(href, "div", gridAttrs) contents += "\n" } diff --git a/styles.css b/styles.css index 5d2b9fe..cf4d761 100644 --- a/styles.css +++ b/styles.css @@ -1,12 +1,15 @@ .grid-container { font-family: Arial, sans-serif; font-size: 4vh; - width: 100%; + //width: 100%; display: grid; + grid-template-columns: repeat(100, 1fr); + grid-auto-rows: minmax(100px, auto); + margin-left: 30%; } .grid-item { - padding: 20px; + padding: 10px; background: green; border-radius: 20px; border: 1px solid white; diff --git a/thumbnail.sh b/thumbnail.sh new file mode 100644 index 0000000..b38003e --- /dev/null +++ b/thumbnail.sh @@ -0,0 +1,6 @@ +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +for f in $1/*.pdf; do magick convert -thumbnail "140x200" -background white -alpha remove -crop 178x178+0+0 "$f"[0] "${f%.pdf}.png"; done diff --git a/tncmd.sh b/tncmd.sh new file mode 100755 index 0000000..3aaead1 --- /dev/null +++ b/tncmd.sh @@ -0,0 +1,2 @@ +#!/bin/bash +magick convert -thumbnail "140x200" -background white -alpha remove -crop 178x178+0+0 "$1"[0] "$2/${1%.pdf}.png"