thumbnails

main
sandyx 1 year ago
parent 9417bdd9fe
commit 8c58331e58

5
.gitignore vendored

@ -0,0 +1,5 @@
*.pdf
*.png
main
pdf
thumbnails

@ -1 +0,0 @@
it has some text in it

@ -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("<img %s/>", a.String())
}
func wrapWithTagAttrs(str, tag string, a htmlAttrs) string {
return fmt.Sprintf("<%s %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"
}

@ -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;

@ -0,0 +1,6 @@
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <path>"
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

@ -0,0 +1,2 @@
#!/bin/bash
magick convert -thumbnail "140x200" -background white -alpha remove -crop 178x178+0+0 "$1"[0] "$2/${1%.pdf}.png"
Loading…
Cancel
Save