From c746f0bae5168a8245abeaf94387d426536491d4 Mon Sep 17 00:00:00 2001 From: "Sven.Schmalle" Date: Thu, 21 Nov 2019 10:08:17 +0100 Subject: [PATCH] =?UTF-8?q?Thumbnail-generierung=20f=C3=BCr=20dir=20Fotos?= =?UTF-8?q?=20mit=20eingebaut?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 ++- main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++- web/index.html | 2 +- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 48037e0..065a6e0 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Zum Kompilieren folgende Go-Abhängigkeiten installieren: - `go get github.com/gorilla/mux` - `go get github.com/boombuler/barcode` - `go get github.com/boombuler/barcode/qr` - + - `go get github.com/disintegration/imaging` + Kompilieren mit: `go build main.go` diff --git a/main.go b/main.go index 780310b..a11ad83 100644 --- a/main.go +++ b/main.go @@ -9,9 +9,11 @@ import ( "net/http" "os" "path" + "strings" "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" + "github.com/disintegration/imaging" "github.com/gorilla/mux" ) @@ -24,6 +26,7 @@ func main() { router := mux.NewRouter() router.HandleFunc("/folder/{foldername:.*}", GetFolderContent).Methods("GET") router.HandleFunc("/qr/{qrlink:.*}", GetQRCode).Methods("GET") + router.HandleFunc("/thumb/{imgname:.*}", GetThumbNail).Methods("GET") router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir("C:/Fotos")))).Methods("GET") router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET") log.Fatal(http.ListenAndServe(":8000", router)) @@ -41,7 +44,9 @@ func GetFolderContent(w http.ResponseWriter, r *http.Request) { var dateien []string for _, f := range files { - dateien = append(dateien, f.Name()) + if !strings.HasSuffix(f.Name(), ".thumb.jpg") { + dateien = append(dateien, f.Name()) + } } json.NewEncoder(w).Encode(dateien) @@ -66,3 +71,51 @@ func GetQRCode(w http.ResponseWriter, r *http.Request) { // Datei ausgeben png.Encode(w, qrCode) } + +func GetThumbNail(w http.ResponseWriter, r *http.Request) { + + // Variablen auswerten + params := mux.Vars(r) + pIMGName := params["imgname"] + + // Das Thumbnail soll nur generiert werden wenn es noch nicht existiert + if !fileExists(StartUpPath + "/" + pIMGName + ".thumb.jpg") { + // Originaldatei öffnen, welche angefragt wird + src, err := imaging.Open(StartUpPath + "/" + pIMGName) + if err != nil { + log.Fatalf("failed to open image: %v", err) + } + + // Foto auf 200px skalieren + src = imaging.Resize(src, 200, 0, imaging.NearestNeighbor) + + err = imaging.Save(src, StartUpPath+"/"+pIMGName+".thumb.jpg") + if err != nil { + log.Fatalf("failed to save image: %v", err) + } + + // Datei ausgeben + png.Encode(w, src) + } else { + + src, err := imaging.Open(StartUpPath + "/" + pIMGName + ".thumb.jpg") + if err != nil { + log.Fatalf("failed to open image: %v", err) + } + + // Datei ausgeben + png.Encode(w, src) + } +} + +//-------------------------------------------------------------------------- +// Hilfsfunktionen +//-------------------------------------------------------------------------- + +func fileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/web/index.html b/web/index.html index 5109a68..99b62c1 100644 --- a/web/index.html +++ b/web/index.html @@ -64,7 +64,7 @@ for ( var i = 0, l = content.length; i < l; i++ ) { var ObjInhalt = content[i]; $("#Ordner").append(""+ - ""+ + ""+ ""+ ObjInhalt+"
"); }