Skip to content

Commit

Permalink
Add support to fs.fs on serve static files (#1640)
Browse files Browse the repository at this point in the history
* substitute *os.File by fs.File

* refactor error handling by using the new recommended form

* finish implementation

* substitute seek(offset,0) by seek(offset, io.SeekStart)

* add unit test

* use io.SeekStart on Seek method
  • Loading branch information
peczenyj authored Nov 5, 2023
1 parent 42bd7bb commit 4010b16
Show file tree
Hide file tree
Showing 3 changed files with 841 additions and 47 deletions.
10 changes: 7 additions & 3 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"fmt"
"io"
"os"
"io/fs"
"sync"

"github.com/klauspost/compress/flate"
Expand Down Expand Up @@ -421,7 +421,7 @@ func newCompressWriterPoolMap() []*sync.Pool {
return m
}

func isFileCompressible(f *os.File, minCompressRatio float64) bool {
func isFileCompressible(f fs.File, minCompressRatio float64) bool {
// Try compressing the first 4kb of the file
// and see if it can be compressed by more than
// the given minCompressRatio.
Expand All @@ -433,7 +433,11 @@ func isFileCompressible(f *os.File, minCompressRatio float64) bool {
}
_, err := copyZeroAlloc(zw, lr)
releaseStacklessGzipWriter(zw, CompressDefaultCompression)
f.Seek(0, 0) //nolint:errcheck
seeker, ok := f.(io.Seeker)
if !ok {
return false
}
seeker.Seek(0, io.SeekStart) //nolint:errcheck
if err != nil {
return false
}
Expand Down
Loading

0 comments on commit 4010b16

Please sign in to comment.