Skip to content

Commit 37adf36

Browse files
authored
Merge pull request #17 from xEsk/main
Implemented a new NoCompress option
2 parents 8cd37d8 + 23a1ac6 commit 37adf36

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

src/app/app.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ func NewApp(params *param.Params) App {
5555
return App{params: params, server: nil, cache: cache}
5656
}
5757

58+
func (app *App) ShouldSkipCompression(filePath string) bool {
59+
ext := strings.ToLower(filepath.Ext(filePath))
60+
for _, blocked := range app.params.NoCompress {
61+
if strings.ToLower(blocked) == ext {
62+
return true
63+
}
64+
}
65+
return false
66+
}
67+
5868
func (app *App) CompressFiles() {
5969
if !app.params.Gzip && !app.params.Brotli {
6070
return
@@ -73,6 +83,10 @@ func (app *App) CompressFiles() {
7383
return nil
7484
}
7585

86+
if app.ShouldSkipCompression(filePath) {
87+
return nil
88+
}
89+
7690
if info.Size() > app.params.Threshold {
7791
data, _ := os.ReadFile(filePath)
7892

@@ -231,6 +245,14 @@ func (app *App) HandlerFuncNew(w http.ResponseWriter, r *http.Request) {
231245
return
232246
}
233247

248+
if r.Header.Get("Range") != "" || app.ShouldSkipCompression(requestedPath) {
249+
if responseItem.ContentType != "" {
250+
w.Header().Set("Content-Type", responseItem.ContentType)
251+
}
252+
http.ServeContent(w, r, responseItem.Name, responseItem.ModTime, bytes.NewReader(responseItem.Content))
253+
return
254+
}
255+
234256
if slices.Contains(app.params.IgnoreCacheControlPaths, r.URL.Path) || path.Ext(responseItem.Name) == ".html" {
235257
w.Header().Set("Cache-Control", "no-store")
236258
} else {

src/param/param.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ var Flags = []cli.Flag{
8080
Name: "log-pretty",
8181
Value: false,
8282
},
83+
&cli.StringSliceFlag{
84+
EnvVars: []string{"NO_COMPRESS"},
85+
Name: "no-compress",
86+
Value: nil,
87+
},
8388
}
8489

8590
type Params struct {
@@ -96,6 +101,7 @@ type Params struct {
96101
CacheBuffer int
97102
Logger bool
98103
LogPretty bool
104+
NoCompress []string
99105
//DirectoryListing bool
100106
}
101107

@@ -119,6 +125,7 @@ func ContextToParams(c *cli.Context) (*Params, error) {
119125
CacheBuffer: c.Int("cache-buffer"),
120126
Logger: c.Bool("logger"),
121127
LogPretty: c.Bool("log-pretty"),
128+
NoCompress: c.StringSlice("no-compress"),
122129
//DirectoryListing: c.Bool("directory-listing"),
123130
}, nil
124131
}

0 commit comments

Comments
 (0)