diff options
author | rtz12 <koenig@fagott.pw> | 2016-10-09 14:45:18 (UTC) |
---|---|---|
committer | rtz12 <koenig@fagott.pw> | 2016-10-09 14:45:18 (UTC) |
commit | bbfd2e21ab39bb3e30aaf463d38c946dcdfe0345 (patch) | |
tree | 7d479c5fa002fb72013ef24d3fcbe9040392758c | |
parent | 49148d8ccf177d7158cfde8679b5c9eb3b1f3ad1 (diff) | |
parent | 82e4530eb98f065f79339829fca45f404ca6ffce (diff) |
Merge branch 'master' of projekte.fagott.pw:grilist
-rw-r--r-- | tools/image-on-demand/gril.list | 2 | ||||
-rw-r--r-- | tools/image-on-demand/gril/full/63.jpg | bin | 0 -> 15084 bytes | |||
-rwxr-xr-x | tools/image-on-demand/gril/full/64.jpg | bin | 0 -> 11172 bytes | |||
-rw-r--r-- | tools/image-on-demand/gril/thumb/63.jpg | bin | 0 -> 23901 bytes | |||
-rwxr-xr-x | tools/image-on-demand/gril/thumb/64.jpg | bin | 0 -> 18445 bytes | |||
-rw-r--r-- | tools/image-on-demand/main.go | 144 | ||||
-rw-r--r-- | tools/image-on-demand/series.list | 0 |
7 files changed, 146 insertions, 0 deletions
diff --git a/tools/image-on-demand/gril.list b/tools/image-on-demand/gril.list new file mode 100644 index 0000000..0ff2750 --- /dev/null +++ b/tools/image-on-demand/gril.list | |||
@@ -0,0 +1,2 @@ | |||
1 | 64 http://ami.animecharactersdatabase.com/images/Clannad/fujibayashi_ryou_thumb.jpg | ||
2 | 64 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 --- /dev/null +++ b/tools/image-on-demand/gril/full/63.jpg | |||
Binary files 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 --- /dev/null +++ b/tools/image-on-demand/gril/full/64.jpg | |||
Binary files 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 --- /dev/null +++ b/tools/image-on-demand/gril/thumb/63.jpg | |||
Binary files 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 --- /dev/null +++ b/tools/image-on-demand/gril/thumb/64.jpg | |||
Binary files differ | |||
diff --git a/tools/image-on-demand/main.go b/tools/image-on-demand/main.go new file mode 100644 index 0000000..e2300b6 --- /dev/null +++ b/tools/image-on-demand/main.go | |||
@@ -0,0 +1,144 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "io" | ||
6 | "io/ioutil" | ||
7 | "log" | ||
8 | "net/http" | ||
9 | "os" | ||
10 | "path" | ||
11 | "strconv" | ||
12 | "strings" | ||
13 | |||
14 | "github.com/julienschmidt/httprouter" | ||
15 | ) | ||
16 | |||
17 | var files map[string]map[int][]string | ||
18 | var IMAGE_TYPES map[string]int | ||
19 | |||
20 | func serveImage(w http.ResponseWriter, r *http.Request, p httprouter.Params) { | ||
21 | log.Printf("GET %s", r.URL.Path) | ||
22 | abort := func(code int, reason string) { | ||
23 | http.Error(w, reason, code) | ||
24 | log.Printf("GET failed: %d %s", code, reason) | ||
25 | } | ||
26 | |||
27 | file, ok := files[p.ByName("type")] | ||
28 | if !ok { | ||
29 | abort(400, "Invalid Type") | ||
30 | return | ||
31 | } | ||
32 | |||
33 | simageType := p.ByName("image_type") | ||
34 | id := p.ByName("id") | ||
35 | |||
36 | localPath := path.Join("./", p.ByName("type"), simageType, id) | ||
37 | if _, err := os.Stat(localPath); err == nil { | ||
38 | log.Println("-> Serving from local file") | ||
39 | f, err := os.Open(localPath) | ||
40 | defer f.Close() | ||
41 | if err != nil { | ||
42 | abort(500, "Could not open File") | ||
43 | return | ||
44 | } | ||
45 | w.WriteHeader(200) | ||
46 | io.Copy(w, f) | ||
47 | return | ||
48 | } | ||
49 | |||
50 | if idx := strings.Index(id, ".jpg"); idx != -1 { | ||
51 | id = id[:idx] | ||
52 | } | ||
53 | |||
54 | numId, err := strconv.ParseInt(id, 10, 0) | ||
55 | if err != nil { | ||
56 | abort(400, "Invalid ID") | ||
57 | return | ||
58 | } | ||
59 | |||
60 | imageType, ok := IMAGE_TYPES[simageType] | ||
61 | if !ok { | ||
62 | abort(400, "Invalid image type") | ||
63 | return | ||
64 | } | ||
65 | |||
66 | images, ok := file[int(numId)] | ||
67 | if !ok { | ||
68 | abort(404, "Invalid Target") | ||
69 | return | ||
70 | } | ||
71 | |||
72 | if imageType < 0 || int(imageType) >= len(images) { | ||
73 | abort(400, "Invalid image type") | ||
74 | return | ||
75 | } | ||
76 | log.Println("-> downloading from acd") | ||
77 | |||
78 | res, err := http.Get(images[int(imageType)]) | ||
79 | if err != nil { | ||
80 | abort(500, err.Error()) | ||
81 | return | ||
82 | } | ||
83 | |||
84 | data, err := ioutil.ReadAll(res.Body) | ||
85 | if err != nil { | ||
86 | abort(500, err.Error()) | ||
87 | return | ||
88 | } | ||
89 | |||
90 | if err := ioutil.WriteFile(localPath, data, 0755); err != nil { | ||
91 | abort(500, err.Error()) | ||
92 | return | ||
93 | } | ||
94 | |||
95 | w.Write(data) | ||
96 | } | ||
97 | |||
98 | func loadList(name string) error { | ||
99 | data, err := ioutil.ReadFile(fmt.Sprintf("%s.list", name)) | ||
100 | if err != nil { | ||
101 | return err | ||
102 | } | ||
103 | |||
104 | files[name] = make(map[int][]string) | ||
105 | |||
106 | entries := strings.Split(string(data), "\n") | ||
107 | |||
108 | for _, entry := range entries { | ||
109 | if entry == "" { | ||
110 | continue | ||
111 | } | ||
112 | data := strings.Split(entry, " ") | ||
113 | |||
114 | id, err := strconv.ParseInt(data[0], 10, 0) | ||
115 | if err != nil { | ||
116 | return err | ||
117 | } | ||
118 | |||
119 | if e, ok := files[name][int(id)]; ok { | ||
120 | files[name][int(id)] = append(e, data[1]) | ||
121 | } else { | ||
122 | files[name][int(id)] = []string{data[1]} | ||
123 | } | ||
124 | } | ||
125 | return nil | ||
126 | } | ||
127 | |||
128 | func main() { | ||
129 | IMAGE_TYPES = make(map[string]int) | ||
130 | IMAGE_TYPES["thumb"] = 0 | ||
131 | IMAGE_TYPES["full"] = 1 | ||
132 | files = make(map[string]map[int][]string) | ||
133 | if err := loadList("gril"); err != nil { | ||
134 | log.Fatal(err) | ||
135 | } | ||
136 | if err := loadList("series"); err != nil { | ||
137 | log.Fatal(err) | ||
138 | } | ||
139 | |||
140 | router := httprouter.New() | ||
141 | router.GET("/:type/:image_type/:id", serveImage) | ||
142 | log.Println("starting image-on-demand on localhost:8081") | ||
143 | log.Fatal(http.ListenAndServe("localhost:8081", router)) | ||
144 | } | ||
diff --git a/tools/image-on-demand/series.list b/tools/image-on-demand/series.list new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tools/image-on-demand/series.list | |||