2019-11-18 23:10:50 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-11-20 21:58:42 +01:00
|
|
|
"image/png"
|
2019-11-18 23:10:50 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2019-11-20 21:58:42 +01:00
|
|
|
"os"
|
2019-11-18 23:10:50 +01:00
|
|
|
"path"
|
2019-11-21 10:08:17 +01:00
|
|
|
"strings"
|
2019-11-18 23:10:50 +01:00
|
|
|
|
2019-11-20 21:58:42 +01:00
|
|
|
"github.com/boombuler/barcode"
|
|
|
|
"github.com/boombuler/barcode/qr"
|
2019-11-21 10:08:17 +01:00
|
|
|
"github.com/disintegration/imaging"
|
2019-11-18 23:10:50 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2019-11-21 12:41:31 +01:00
|
|
|
var config = readConfig("")
|
|
|
|
|
|
|
|
type Configuration struct {
|
|
|
|
Host string
|
|
|
|
Port string
|
|
|
|
IMGPath string
|
|
|
|
}
|
2019-11-18 23:10:50 +01:00
|
|
|
|
2019-11-20 21:58:42 +01:00
|
|
|
// our main function
|
2019-11-18 23:10:50 +01:00
|
|
|
func main() {
|
|
|
|
|
2019-11-21 12:41:31 +01:00
|
|
|
fmt.Println("Host: " + config.Host)
|
|
|
|
fmt.Println("Post: " + config.Port)
|
|
|
|
fmt.Println("ImgPath: " + config.IMGPath)
|
|
|
|
|
2019-11-18 23:10:50 +01:00
|
|
|
router := mux.NewRouter()
|
2019-11-21 11:17:33 +01:00
|
|
|
router.HandleFunc("/folder/{foldername:.*}", getFolderContent).Methods("GET")
|
|
|
|
router.HandleFunc("/qr/{qrlink:.*}", getQRCode).Methods("GET")
|
|
|
|
router.HandleFunc("/thumb/{imgname:.*}", getThumbNail).Methods("GET")
|
2019-11-21 11:57:54 +01:00
|
|
|
router.HandleFunc("/imgdl/{imgname:.*}", getImageDownload).Methods("GET")
|
2019-11-21 12:41:31 +01:00
|
|
|
router.PathPrefix("/img/").Handler(http.StripPrefix("/img/", http.FileServer(http.Dir(config.IMGPath)))).Methods("GET")
|
2019-11-18 23:10:50 +01:00
|
|
|
router.PathPrefix("/").Handler(http.FileServer(http.Dir("web"))).Methods("GET")
|
2019-11-21 12:41:31 +01:00
|
|
|
log.Fatal(http.ListenAndServe(":"+config.Port, router))
|
2019-11-21 11:17:33 +01:00
|
|
|
|
2019-11-18 23:10:50 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 11:17:33 +01:00
|
|
|
func getFolderContent(w http.ResponseWriter, r *http.Request) {
|
2019-11-18 23:10:50 +01:00
|
|
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
pFolder := params["foldername"]
|
2019-11-21 12:41:31 +01:00
|
|
|
fmt.Println(path.Join(config.IMGPath, pFolder))
|
|
|
|
files, err := ioutil.ReadDir(path.Join(config.IMGPath, pFolder))
|
2019-11-18 23:10:50 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dateien []string
|
|
|
|
for _, f := range files {
|
2019-11-21 10:08:17 +01:00
|
|
|
if !strings.HasSuffix(f.Name(), ".thumb.jpg") {
|
|
|
|
dateien = append(dateien, f.Name())
|
|
|
|
}
|
2019-11-18 23:10:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(dateien)
|
|
|
|
}
|
2019-11-20 21:58:42 +01:00
|
|
|
|
2019-11-21 11:17:33 +01:00
|
|
|
func getQRCode(w http.ResponseWriter, r *http.Request) {
|
2019-11-20 21:58:42 +01:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
2019-11-21 10:08:17 +01:00
|
|
|
|
2019-11-21 11:17:33 +01:00
|
|
|
func getThumbNail(w http.ResponseWriter, r *http.Request) {
|
2019-11-21 10:08:17 +01:00
|
|
|
|
|
|
|
// Variablen auswerten
|
|
|
|
params := mux.Vars(r)
|
|
|
|
pIMGName := params["imgname"]
|
|
|
|
|
|
|
|
// Das Thumbnail soll nur generiert werden wenn es noch nicht existiert
|
2019-11-21 12:41:31 +01:00
|
|
|
if !fileExists(config.IMGPath + "/" + pIMGName + ".thumb.jpg") {
|
2019-11-21 10:08:17 +01:00
|
|
|
// Originaldatei öffnen, welche angefragt wird
|
2019-11-21 12:41:31 +01:00
|
|
|
src, err := imaging.Open(config.IMGPath + "/" + pIMGName)
|
2019-11-21 10:08:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to open image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Foto auf 200px skalieren
|
|
|
|
src = imaging.Resize(src, 200, 0, imaging.NearestNeighbor)
|
|
|
|
|
2019-11-21 12:41:31 +01:00
|
|
|
err = imaging.Save(src, config.IMGPath+"/"+pIMGName+".thumb.jpg")
|
2019-11-21 10:08:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to save image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Datei ausgeben
|
|
|
|
png.Encode(w, src)
|
|
|
|
} else {
|
|
|
|
|
2019-11-21 12:41:31 +01:00
|
|
|
src, err := imaging.Open(config.IMGPath + "/" + pIMGName + ".thumb.jpg")
|
2019-11-21 10:08:17 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to open image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Datei ausgeben
|
|
|
|
png.Encode(w, src)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-21 11:57:54 +01:00
|
|
|
func getImageDownload(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
// Variablen auswerten
|
|
|
|
params := mux.Vars(r)
|
|
|
|
pIMGName := params["imgname"]
|
|
|
|
|
|
|
|
// Originaldatei öffnen, welche angefragt wird
|
2019-11-21 12:41:31 +01:00
|
|
|
src, err := imaging.Open(config.IMGPath + "/" + pIMGName)
|
2019-11-21 11:57:54 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to open image: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Disposition", "attachment; filename=Fotobox_"+pIMGName)
|
|
|
|
|
|
|
|
png.Encode(w, src)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-21 10:08:17 +01:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// Hilfsfunktionen
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func fileExists(filename string) bool {
|
|
|
|
info, err := os.Stat(filename)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !info.IsDir()
|
|
|
|
}
|
2019-11-21 12:41:31 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|