aboutsummaryrefslogtreecommitdiff
path: root/tools/image-on-demand/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'tools/image-on-demand/main.go')
-rw-r--r--tools/image-on-demand/main.go144
1 files changed, 144 insertions, 0 deletions
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 @@
1package main
2
3import (
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
17var files map[string]map[int][]string
18var IMAGE_TYPES map[string]int
19
20func 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
98func 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
128func 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}