From 3663ae0e1d87e6132db514c96c9df05fd676f8f5 Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 9 Oct 2016 18:22:21 +0200 Subject: bilder on demand runterladen, listen fehlen aber noch diff --git a/tools/image-on-demand/.main.go.swp b/tools/image-on-demand/.main.go.swp new file mode 100644 index 0000000..5b60469 Binary files /dev/null and b/tools/image-on-demand/.main.go.swp differ diff --git a/tools/image-on-demand/gril.list b/tools/image-on-demand/gril.list new file mode 100644 index 0000000..0e24253 --- /dev/null +++ b/tools/image-on-demand/gril.list @@ -0,0 +1,2 @@ +64 0 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou_thumb.jpg +64 1 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou.jpg diff --git a/tools/image-on-demand/gril/full/63.jpg b/tools/image-on-demand/gril/full/63.jpg new file mode 100644 index 0000000..5508ea5 Binary files /dev/null and b/tools/image-on-demand/gril/full/63.jpg differ diff --git a/tools/image-on-demand/gril/full/64.jpg b/tools/image-on-demand/gril/full/64.jpg new file mode 100755 index 0000000..d6e4195 Binary files /dev/null and b/tools/image-on-demand/gril/full/64.jpg differ diff --git a/tools/image-on-demand/gril/thumb/63.jpg b/tools/image-on-demand/gril/thumb/63.jpg new file mode 100644 index 0000000..7844ce6 Binary files /dev/null and b/tools/image-on-demand/gril/thumb/63.jpg differ diff --git a/tools/image-on-demand/gril/thumb/64.jpg b/tools/image-on-demand/gril/thumb/64.jpg new file mode 100755 index 0000000..c40f7f3 Binary files /dev/null and b/tools/image-on-demand/gril/thumb/64.jpg differ diff --git a/tools/image-on-demand/main.go b/tools/image-on-demand/main.go new file mode 100644 index 0000000..a3a401d --- /dev/null +++ b/tools/image-on-demand/main.go @@ -0,0 +1,143 @@ +package main + +import ( + "fmt" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "path" + "strconv" + "strings" + + "github.com/julienschmidt/httprouter" +) + +var files map[string]map[int][]string +var IMAGE_TYPES map[string]int + +func serveImage(w http.ResponseWriter, r *http.Request, p httprouter.Params) { + log.Printf("GET %s", r.URL.Path) + abort := func(code int, reason string) { + http.Error(w, reason, code) + log.Printf("GET failed: %d %s", code, reason) + } + + file, ok := files[p.ByName("type")] + if !ok { + abort(400, "Invalid Type") + return + } + + simageType := p.ByName("image_type") + id := p.ByName("id") + + localPath := path.Join("./", p.ByName("type"), simageType, id) + if _, err := os.Stat(localPath); err == nil { + log.Println("-> Serving from local file") + f, err := os.Open(localPath) + defer f.Close() + if err != nil { + abort(500, "Could not open File") + return + } + w.WriteHeader(200) + io.Copy(w, f) + return + } + + if idx := strings.Index(id, ".jpg"); idx != -1 { + id = id[:idx] + } + + numId, err := strconv.ParseInt(id, 10, 0) + if err != nil { + abort(400, "Invalid ID") + return + } + + imageType, ok := IMAGE_TYPES[simageType] + if !ok { + abort(400, "Invalid image type") + return + } + + images, ok := file[int(numId)] + if !ok { + abort(404, "Invalid Target") + return + } + + if imageType < 0 || int(imageType) >= len(images) { + abort(400, "Invalid image type") + return + } + log.Println("-> downloading from acd") + + res, err := http.Get(images[int(imageType)]) + if err != nil { + abort(500, err.Error()) + return + } + + data, err := ioutil.ReadAll(res.Body) + if err != nil { + abort(500, err.Error()) + return + } + + if err := ioutil.WriteFile(localPath, data, 0755); err != nil { + abort(500, err.Error()) + return + } + + w.Write(data) +} + +func loadList(name string) error { + data, err := ioutil.ReadFile(fmt.Sprintf("%s.list", name)) + if err != nil { + return err + } + + files[name] = make(map[int][]string) + + entries := strings.Split(string(data), "\n") + + for _, entry := range entries { + if entry == "" { + continue + } + data := strings.Split(entry, " ") + + id, err := strconv.ParseInt(data[0], 10, 0) + if err != nil { + return err + } + + if e, ok := files[name][int(id)]; ok { + files[name][int(id)] = append(e, data[2]) + } else { + files[name][int(id)] = []string{data[2]} + } + } + return nil +} + +func main() { + IMAGE_TYPES = make(map[string]int) + IMAGE_TYPES["thumb"] = 0 + IMAGE_TYPES["full"] = 1 + files = make(map[string]map[int][]string) + if err := loadList("gril"); err != nil { + log.Fatal(err) + } + if err := loadList("series"); err != nil { + log.Fatal(err) + } + + router := httprouter.New() + router.GET("/:type/:image_type/:id", serveImage) + log.Fatal(http.ListenAndServe("localhost:8081", router)) +} diff --git a/tools/image-on-demand/series.list b/tools/image-on-demand/series.list new file mode 100644 index 0000000..e69de29 -- cgit v0.10.1 From 82e4530eb98f065f79339829fca45f404ca6ffce Mon Sep 17 00:00:00 2001 From: jan Date: Sun, 9 Oct 2016 18:25:46 +0200 Subject: anderes format diff --git a/tools/image-on-demand/.main.go.swp b/tools/image-on-demand/.main.go.swp deleted file mode 100644 index 5b60469..0000000 Binary files a/tools/image-on-demand/.main.go.swp and /dev/null differ diff --git a/tools/image-on-demand/gril.list b/tools/image-on-demand/gril.list index 0e24253..0ff2750 100644 --- a/tools/image-on-demand/gril.list +++ b/tools/image-on-demand/gril.list @@ -1,2 +1,2 @@ -64 0 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou_thumb.jpg -64 1 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou.jpg +64 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou_thumb.jpg +64 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou.jpg diff --git a/tools/image-on-demand/main.go b/tools/image-on-demand/main.go index a3a401d..e2300b6 100644 --- a/tools/image-on-demand/main.go +++ b/tools/image-on-demand/main.go @@ -117,9 +117,9 @@ func loadList(name string) error { } if e, ok := files[name][int(id)]; ok { - files[name][int(id)] = append(e, data[2]) + files[name][int(id)] = append(e, data[1]) } else { - files[name][int(id)] = []string{data[2]} + files[name][int(id)] = []string{data[1]} } } return nil @@ -139,5 +139,6 @@ func main() { router := httprouter.New() router.GET("/:type/:image_type/:id", serveImage) + log.Println("starting image-on-demand on localhost:8081") log.Fatal(http.ListenAndServe("localhost:8081", router)) } -- cgit v0.10.1