gziphandler icon indicating copy to clipboard operation
gziphandler copied to clipboard

Add NotContentTypes option

Open jared2501 opened this issue 6 years ago • 5 comments

Use case: I would like to gzip all content-types, except images/videos.

jared2501 avatar Feb 22 '19 00:02 jared2501

hey @fsouza, @jprobinson - would ya'll be interested in this change? If so, happy to add tests / refactor.

jared2501 avatar Feb 22 '19 00:02 jared2501

@jared2501 this seems like a reasonable addition to me. The code looks like it's in a good place. Go ahead and add some tests and I'll bring this in. Thanks!

jprobinson avatar Feb 25 '19 18:02 jprobinson

oh sorry I missed this. Yeah I second @jp's comment and I was actually thinking about this over the weekend (we have a gizmo service that generates video thumbnails and we don't want to gzip jpg images :D).

fsouza avatar Feb 25 '19 18:02 fsouza

Hi! Because I like functional options so much, I might personally implement this api as:

interface contentTypeMatcher {
  Matches(contentType string) bool
}

func ContentTypeMatchers(matchers ... contentTypeMatcher) option {
   // ...
}

gzip.GzipHandlerWithOpts(gzip.ContentTypeMatchers(
  gzip.MatchLiteral("text/plain"),
  gzip.MatchLiteral("image/jpg", invert=True), // does go have named params? i forgot
))

...so that in some magical future, you might do such weird things as:

gzip.MatchPrefix("text/")
gzip.MatchRegex("*/*+json")
gzip.MatchFunc(func(ct string) bool {
  return time.Now().Hour < 12
})

but then I'd probably add a helper that looks just like what you're proposing, so I dunno.

adammck avatar Feb 25 '19 22:02 adammck

I have opened a PR for this also: https://github.com/nytimes/gziphandler/pull/98

mkraft avatar May 09 '20 17:05 mkraft