13 const PARAM_IDS = "ids"
16 // ============================================================================
17 func efsPathForId(efsId int) string {
20 idStr := fmt.Sprintf("%010d", efsId)
21 path := fmt.Sprintf("%s/%s/%s/%s/%s/%s.dat", config.efsBasePath, idStr[0:2], idStr[2:4], idStr[4:6], idStr[6:8], idStr)
26 func handler(w http.ResponseWriter, r *http.Request) {
29 fmt.Fprintln(w, "ERROR!", err)
33 action := strings.Split(r.URL.Path[1:], "/")[0]
43 fmt.Fprintf(w, "Unrecognized request: %s\n", r.URL.Path[1:])
44 fmt.Fprintf(w, "id: %s\n", r.FormValue("id"))
46 fmt.Fprintln(w, "URL: ", r.URL)
47 fmt.Fprintln(w, "Query: ", r.URL.Query())
52 func handleApp(w http.ResponseWriter, r *http.Request) {
53 fmt.Println("handleApp():", r.URL.Path)
55 // Security check: prevent walking up the directory
56 pos := strings.Index(r.Url.Path, "../")
58 fmt.Fprintln(w, "Paths containing \"../\" are not permitted:", r.URL.Path)
62 fileName := "../app" + r.URL.Path
63 _, err := os.Stat(fileName)
65 fmt.Fprintln(w, "Failed to find file:", fileName, err)
69 http.ServeFile(w, r, "../app/" + r.URL.Path[1:])
73 func handleDownload(w http.ResponseWriter, r *http.Request) {
74 path := r.URL.Path[1:]
75 tok := strings.Split(path, "/")
77 fmt.Fprintln(w, "Unexpected path for download:", path)
80 efsId, err := strconv.Atoi(tok[1])
81 if (nil != err) || (0 == efsId) {
82 fmt.Fprintln(w, "Invalid id for download:", path, err)
86 mimeType := queryMimeTypeByEfsId(efsId)
87 if 0 == len(mimeType) {
88 fmt.Fprintln(w, "No MIME type found for id:", efsId)
92 efsPath := efsPathForId(efsId)
95 fd, err = os.Open(efsPath)
97 fmt.Fprintln(w, "Failed to read file for id:", efsId, err)
105 func handleInfo(w http.ResponseWriter, r *http.Request) {
106 idParams := r.Form[PARAM_IDS]
107 if 1 != len(idParams) {
108 fmt.Fprintln(w, "ERROR! Detected either zero or multiple ids= parameters. Exactly one expected.")
112 idParam := idParams[0]
113 idStrings := strings.Split(idParam, ",")
114 ids := make([]int, len(idStrings))
116 for i, v := range(idStrings) {
117 ids[i], err = strconv.Atoi(v)
123 books := queryBooksByIds(ids)
126 jsonValue, err = json.Marshal(books)
128 fmt.Fprintln(w, "ERROR!", err)
134 func handleSearch(w http.ResponseWriter, r *http.Request) {
136 fmt.Println("DEBUG: handleSearch(): " + r.URL.Path)
138 fields := []Field{Author, Title, Series}
140 terms := make([]SearchTerm, len(fields))
143 for _, fv := range(fields) {
144 paramName := fv.String()
145 paramValues := r.Form[paramName]
146 for _, pv := range(paramValues) {
147 fmt.Println("DEBUG: handleSearch(): ", paramName, "=", pv)
148 if count >= len(terms) {
149 fmt.Printf("WARNING: limit of %d search terms exceeded. One or more terms ignored.", len(terms))
152 terms[count] = SearchTerm{Attribute:fv, Text:pv}
157 terms = terms[:count]
159 ids := queryIds(terms)
161 jsonValue, err := json.Marshal(ids)
163 fmt.Fprintln(w, "ERROR!", err)