package main import ( "flag" "fmt" "io" "log" "net/http" "net/url" "os" "path/filepath" "os/exec" "strings" _ "embed" ) //go:embed tncmd.sh var thumbnailCmd string //go:embed styles.css var styleSheet string //easily put attrs in a tag type htmlAttrs map[string]string func (attrs htmlAttrs) String() string { var ret string for k, v := range attrs { ret += fmt.Sprintf("%s=\"%s\" ", k, url.QueryEscape(v)) } 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) } func wrapWithTag(str string, tag string) string { return fmt.Sprintf("<%s>%s", tag, str, tag) } //cleaner than making an attr for one href func wrapWithTagHref(str string, tag string) string { return fmt.Sprintf("<%s href=%s>%s", tag, url.QueryEscape(str), str, tag) } func serveStylesheet(w http.ResponseWriter, r *http.Request) { sheet, err := os.Open("styles.css") if err != nil { http.Error(w, "File not found", http.StatusNotFound) return } defer sheet.Close() w.Header().Set("Content-Disposition", "attachment; filename=style.css") w.Header().Set("Content-Type", "text/css") _, err = io.Copy(w, sheet) if err != nil { http.Error(w, "Error sending file", http.StatusInternalServerError) return } } 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") converter := flag.String("t", ".", "location of conversion script") flag.Parse() createThumbnailsDirectory() var contents string entries, err := os.ReadDir(*directory) if err != nil { log.Fatal(err) } for _, e := range entries { go func(name string) { new := name newImg := strings.TrimSuffix(new, filepath.Ext(new)) + ".png" if filepath.Ext(new) != ".pdf" { return } dir, _ := filepath.Abs(*directory) cmd := exec.Command(*converter, filepath.Join(dir, name)) fmt.Println(cmd) if err := cmd.Run(); err != nil { log.Println(err) } else { fmt.Println("Created thumbnail for: " + new) http.HandleFunc("/" + url.QueryEscape(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 } }) } }(e.Name()) } for _, e := range entries { new := e.Name() newImg := strings.TrimSuffix(new, filepath.Ext(new)) + ".png" if filepath.Ext(new) != ".pdf" { continue } //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) if err != nil { http.Error(w, "File not found.", http.StatusNotFound) return } defer file.Close() w.Header().Set("Content-Disposition", "attachment; filename=" + new) w.Header().Set("Content-Type", "application/pdf") _, err = io.Copy(w, file) if err != nil { http.Error(w, "Error sending file.", http.StatusInternalServerError) return } }) gridAttrs := htmlAttrs { "class": "grid-item", } hrefAttrs := htmlAttrs { "href": e.Name(), } imgAttrs := htmlAttrs { "src": newImg, } var img string img = imgWithTagAttrs(imgAttrs) var href string href = wrapWithTagAttrs(img, "a", hrefAttrs) contents += wrapWithTagAttrs(href, "div", gridAttrs) contents += "\n" } gridContainerAttrs := htmlAttrs { "class": "grid-container", } contents = wrapWithTagAttrs(contents, "div", gridContainerAttrs) contents = wrapWithTag(contents, "pre") contents = wrapWithTag(string(styleSheet), "style") + contents contents = wrapWithTag(contents, "html") contents = "" + contents handler := func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(contents)) } http.Handle("/", http.HandlerFunc(handler)) //http.Handle("/styles.css", http.HandlerFunc(serveStylesheet)) log.Printf("Serving %s on HTTP port: %s\n", *directory, *port) log.Fatal(http.ListenAndServe(":"+*port, nil)) }