Beispiel #1
0
func FileHandler(store fs.LocalStore) http.HandlerFunc {
	return func(resp http.ResponseWriter, req *http.Request) {
		setBinaryResp(resp)

		strong, hasStrong := mux.Vars(req)["strong"]
		if !hasStrong {
			writeResponseError(resp, http.StatusInternalServerError,
				"Missing parameter: strong")
			return
		}

		offsetStr, hasOffset := mux.Vars(req)["offset"]
		if !hasOffset {
			writeResponseError(resp, http.StatusInternalServerError,
				"Missing parameter: offset")
			return
		}
		offset, err := strconv.Atoi64(offsetStr)
		if err != nil {
			writeResponseError(resp, http.StatusInternalServerError,
				fmt.Sprintf("Invalid format for length: %s", offsetStr))
			return
		}

		lengthStr, hasLength := mux.Vars(req)["length"]
		if !hasLength {
			writeResponseError(resp, http.StatusInternalServerError,
				"Missing parameter: length")
			return
		}
		length, err := strconv.Atoi64(lengthStr)
		if err != nil {
			writeResponseError(resp, http.StatusInternalServerError,
				fmt.Sprintf("Invalid format for length: %s", lengthStr))
			return
		}

		buffer := bytes.NewBuffer([]byte{})
		n, err := store.ReadInto(strong, offset, length, buffer)
		if err != nil {
			writeResponseError(resp, http.StatusInternalServerError, err.String())
			return
		}
		if n < length {
			writeResponseError(resp, http.StatusInternalServerError, io.ErrShortWrite.String())
		}

		resp.Write(buffer.Bytes())
	}
}