package main import ( "encoding/json" "fmt" "image/png" "io/ioutil" "log" "net/http" "os" "path" "strings" "github.com/boombuler/barcode" "github.com/boombuler/barcode/qr" "github.com/disintegration/imaging" "github.com/gorilla/mux" ) var config = readConfig("") type Configuration struct { Host string Port string IMGPath string } // our main function func main() { fmt.Println("Host: " + config.Host) fmt.Println("Post: " + config.Port) fmt.Println("ImgPath: " + config.IMGPath) 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.HandleFunc("/imgdl/{imgname:.*}", getImageDownload).Methods("GET") router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(config.IMGPath)))).Methods("GET") router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET") log.Fatal(http.ListenAndServe(":"+config.Port, router)) } func getFolderContent(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) pFolder := params["foldername"] fmt.Println(path.Join(config.IMGPath, pFolder)) files, err := ioutil.ReadDir(path.Join(config.IMGPath, pFolder)) if err != nil { log.Fatal(err) } var dateien []string for _, f := range files { if !strings.HasSuffix(f.Name(), ".thumb.jpg") { dateien = append(dateien, f.Name()) } } json.NewEncoder(w).Encode(dateien) } func getQRCode(w http.ResponseWriter, r *http.Request) { // Variablen auswerten params := mux.Vars(r) pQRLink := params["qrlink"] // QR-Code erstellen qrCode, _ := qr.Encode(pQRLink, qr.M, qr.Auto) // Größe festlegen qrCode, _ = barcode.Scale(qrCode, 200, 200) // Datei erstellen file, _ := os.Create("qrcode.png") defer file.Close() // 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(config.IMGPath + "/" + pIMGName + ".thumb.jpg") { // Originaldatei öffnen, welche angefragt wird src, err := imaging.Open(config.IMGPath + "/" + 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, config.IMGPath+"/"+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(config.IMGPath + "/" + pIMGName + ".thumb.jpg") if err != nil { log.Fatalf("failed to open image: %v", err) } // Datei ausgeben png.Encode(w, src) } } func getImageDownload(w http.ResponseWriter, r *http.Request) { // Variablen auswerten params := mux.Vars(r) pIMGName := params["imgname"] // Originaldatei öffnen, welche angefragt wird src, err := imaging.Open(config.IMGPath + "/" + pIMGName) if err != nil { log.Fatalf("failed to open image: %v", err) } w.Header().Set("Content-Disposition", "attachment; filename=Fotobox_"+pIMGName) png.Encode(w, src) } //-------------------------------------------------------------------------- // Hilfsfunktionen //-------------------------------------------------------------------------- func fileExists(filename string) bool { info, err := os.Stat(filename) if os.IsNotExist(err) { return false } return !info.IsDir() } func readConfig(filename string) *Configuration { // initialize conf with default values. conf := &Configuration{Host: "http://127.0.0.1", Port: "8000", IMGPath: "C:\\Fotos"} b, err := ioutil.ReadFile("./config.json") if err != nil { return conf } if err = json.Unmarshal(b, conf); err != nil { return conf } return conf }