aboutsummaryrefslogtreecommitdiff
path: root/tools/image-on-demand/main.go
blob: e2300b6e0158e986043449f53b64a2d5077b5117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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[1])
		} else {
			files[name][int(id)] = []string{data[1]}
		}
	}
	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.Println("starting image-on-demand on localhost:8081")
	log.Fatal(http.ListenAndServe("localhost:8081", router))
}