package handlers import ( "net/http" "regexp" "server/database" "server/helper" "strconv" ) func Image(writer http.ResponseWriter, request *http.Request) { if request.Method != "GET" { helper.WriteErrorJson("expected GET method", writer, http.StatusBadRequest) return } re := regexp.MustCompile(`^/image/([0-9]+).png$`) submatches := re.FindStringSubmatch(request.URL.Path) if len(submatches) != 2 { http.Error(writer, "invalid URL", http.StatusBadRequest) return } uid, err := strconv.Atoi(submatches[1]) if err != nil { helper.WriteInternalError(err, writer) return } image_blob, err := database.GetImage(uid, request.URL.Query().Get("thumbnail") == "1") if err != nil { helper.WriteInternalError(err, writer) return } if image_blob == nil { http.Error(writer, "resource not found", http.StatusNotFound) return } writer.Write(image_blob) }