Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

Commit

Permalink
Document caveats with compressed fs.FS (#344)
Browse files Browse the repository at this point in the history
* Document caveats with compressed fs.FS

* Update README.md

Co-authored-by: Matt Holt <[email protected]>
  • Loading branch information
WeidiDeng and mholt authored Sep 2, 2022
1 parent b95d6b6 commit b4f84f1
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,31 @@ if err != nil {
}
```

#### Use with `http.FileServer`

It can be used with http.FileServer to browse archives and directories in a browser. However, due to how http.FileServer works, don't directly use http.FileServer with compressed files; instead wrap it like following:

```go
fileServer := http.FileServer(http.FS(archiveFS))
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
// disable range request
writer.Header().Set("Accept-Ranges", "none")
request.Header.Del("Range")

// disable content-type sniffing
ctype := mime.TypeByExtension(filepath.Ext(request.URL.Path))
writer.Header()["Content-Type"] = nil
if ctype != "" {
writer.Header().Set("Content-Type", ctype)
}
fileServer.ServeHTTP(writer, request)
})
```

http.FileServer will try to sniff the Content-Type by default if it can't be inferred from file name. To do this, the http package will try to read from the file and then Seek back to file start, which the libray can't achieve currently. The same goes with Range requests. Seeking in archives is not currently supported by archiver due to limitations in dependencies.

If content-type is desirable, you can [register it](https://pkg.go.dev/mime#AddExtensionType) yourself.

### Compress data

Compression formats let you open writers to compress data:
Expand Down

0 comments on commit b4f84f1

Please sign in to comment.