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())
51 func handleDownload(w http.ResponseWriter, r *http.Request) {
52 path := r.URL.Path[1:]
53 tok := strings.Split(path, "/")
55 fmt.Fprintln(w, "Unexpected path for download:", path)
58 efsId, err := strconv.Atoi(tok[1])
59 if (nil != err) || (0 == efsId) {
60 fmt.Fprintln(w, "Invalid id for download:", path, err)
64 mimeType := queryMimeTypeByEfsId(efsId)
65 if 0 == len(mimeType) {
66 fmt.Fprintln(w, "No MIME type found for id:", efsId)
70 efsPath := efsPathForId(efsId)
73 fd, err = os.Open(efsPath)
75 fmt.Fprintln(w, "Failed to read file for id:", efsId, err)
83 func handleInfo(w http.ResponseWriter, r *http.Request) {
84 idParams := r.Form[PARAM_IDS]
85 if 1 != len(idParams) {
86 fmt.Fprintln(w, "ERROR! Detected either zero or multiple ids= parameters. Exactly one expected.")
90 idParam := idParams[0]
91 idStrings := strings.Split(idParam, ",")
92 ids := make([]int, len(idStrings))
93 for i, v := range(idStrings) {
95 ids[i], err = strconv.Atoi(v)
101 books := queryBooksByIds(ids)
105 jsonValue, err = json.Marshal(books)
107 fmt.Fprintln(w, "ERROR!", err)
113 func handleSearch(w http.ResponseWriter, r *http.Request) {
114 fields := []Field{Author, Title, Series}
116 terms := make([]SearchTerm, len(fields))
119 for _, fv := range(fields) {
120 paramName := fv.String()
121 paramValues := r.Form[paramName]
122 for _, pv := range(paramValues) {
123 if count >= len(terms) {
124 fmt.Printf("WARNING: limit of %v search terms exceeded. One or more terms ignored.")
127 terms[count] = SearchTerm{Attribute:fv, Text:pv}
132 terms = terms[:count]
134 ids := queryIds(terms)
136 jsonValue, err := json.Marshal(ids)
138 fmt.Fprintln(w, "ERROR!", err)