|
|
|
|
@ -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"
|
|
|
|
|
}
|
|
|
|
|
|