From a56766a613b4baeb8755498efdf7f4d62e16e346 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Wed, 8 Dec 2021 19:02:43 -0700 Subject: [PATCH] Support files that are not io.ReadSeekers --- hashfs.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/hashfs.go b/hashfs.go index 6f776e5..e872946 100644 --- a/hashfs.go +++ b/hashfs.go @@ -10,6 +10,7 @@ import ( "os" "path" "regexp" + "strconv" "strings" "sync" ) @@ -192,5 +193,17 @@ func (h *fsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // Flush header and write content. - http.ServeContent(w, r, filename, fi.ModTime(), f.(io.ReadSeeker)) + switch f := f.(type) { + case io.ReadSeeker: + http.ServeContent(w, r, filename, fi.ModTime(), f.(io.ReadSeeker)) + default: + // Set content length. + w.Header().Set("Content-Length", strconv.FormatInt(fi.Size(), 10)) + + // Flush header and write content. + w.WriteHeader(http.StatusOK) + if r.Method != "HEAD" { + io.Copy(w, f) + } + } }