initial commit
This commit is contained in:
19
transcoder/gif.go
Normal file
19
transcoder/gif.go
Normal file
@ -0,0 +1,19 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"image/gif"
|
||||
)
|
||||
|
||||
type Gif struct{}
|
||||
|
||||
func (t *Gif) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
img, err := gif.Decode(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = gif.Encode(w, img, nil); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
39
transcoder/gzip.go
Normal file
39
transcoder/gzip.go
Normal file
@ -0,0 +1,39 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"github.com/barnacs/compy/proxy"
|
||||
)
|
||||
|
||||
type Gzip struct {
|
||||
proxy.Transcoder
|
||||
SkipGzipped bool
|
||||
}
|
||||
|
||||
func (t *Gzip) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
if t.decompress(r) {
|
||||
gzr, err := gzip.NewReader(r.Reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer gzr.Close()
|
||||
r.Reader = gzr
|
||||
r.Header().Del("Content-Encoding")
|
||||
w.Header().Del("Content-Encoding")
|
||||
}
|
||||
if compress(r) {
|
||||
gzw := gzip.NewWriter(w.Writer)
|
||||
defer gzw.Flush()
|
||||
w.Writer = gzw
|
||||
w.Header().Set("Content-Encoding", "gzip")
|
||||
}
|
||||
return t.Transcoder.Transcode(w, r)
|
||||
}
|
||||
|
||||
func (t *Gzip) decompress(r *proxy.ResponseReader) bool {
|
||||
return !t.SkipGzipped && r.Header().Get("Content-Encoding") == "gzip"
|
||||
}
|
||||
|
||||
func compress(r *proxy.ResponseReader) bool {
|
||||
return r.Header().Get("Content-Encoding") == ""
|
||||
}
|
13
transcoder/identity.go
Normal file
13
transcoder/identity.go
Normal file
@ -0,0 +1,13 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"io"
|
||||
)
|
||||
|
||||
type Identity struct{}
|
||||
|
||||
func (i *Identity) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
_, err := io.Copy(w, r)
|
||||
return err
|
||||
}
|
32
transcoder/jpeg.go
Normal file
32
transcoder/jpeg.go
Normal file
@ -0,0 +1,32 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"github.com/pixiv/go-libjpeg/jpeg"
|
||||
)
|
||||
|
||||
type Jpeg struct {
|
||||
decOptions *jpeg.DecoderOptions
|
||||
encOptions *jpeg.EncoderOptions
|
||||
}
|
||||
|
||||
func NewJpeg(quality int) *Jpeg {
|
||||
return &Jpeg{
|
||||
decOptions: &jpeg.DecoderOptions{},
|
||||
encOptions: &jpeg.EncoderOptions{
|
||||
Quality: quality,
|
||||
OptimizeCoding: true,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Jpeg) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
img, err := jpeg.Decode(r, t.decOptions)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = jpeg.Encode(w, img, t.encOptions); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
34
transcoder/minify.go
Normal file
34
transcoder/minify.go
Normal file
@ -0,0 +1,34 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"github.com/tdewolff/minify"
|
||||
"github.com/tdewolff/minify/css"
|
||||
"github.com/tdewolff/minify/html"
|
||||
"github.com/tdewolff/minify/js"
|
||||
"github.com/tdewolff/parse"
|
||||
)
|
||||
|
||||
func init() {
|
||||
parse.MaxBuf *= 8
|
||||
}
|
||||
|
||||
type Minifier struct {
|
||||
m minify.Minify
|
||||
}
|
||||
|
||||
func NewMinifier() *Minifier {
|
||||
m := minify.New()
|
||||
m.AddFunc("text/html", html.Minify)
|
||||
m.AddFunc("text/css", css.Minify)
|
||||
m.AddFunc("text/javascript", js.Minify)
|
||||
m.AddFunc("application/javascript", js.Minify)
|
||||
m.AddFunc("application/x-javascript", js.Minify)
|
||||
return &Minifier{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Minifier) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
return t.m.Minify(r.ContentType(), w, r)
|
||||
}
|
19
transcoder/png.go
Normal file
19
transcoder/png.go
Normal file
@ -0,0 +1,19 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"image/png"
|
||||
)
|
||||
|
||||
type Png struct{}
|
||||
|
||||
func (t *Png) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
img, err := png.Decode(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = png.Encode(w, img); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
34
transcoder/text.go
Normal file
34
transcoder/text.go
Normal file
@ -0,0 +1,34 @@
|
||||
package transcoder
|
||||
|
||||
import (
|
||||
"github.com/barnacs/compy/proxy"
|
||||
"github.com/tdewolff/minify"
|
||||
"github.com/tdewolff/minify/css"
|
||||
"github.com/tdewolff/minify/html"
|
||||
"github.com/tdewolff/minify/js"
|
||||
"github.com/tdewolff/parse"
|
||||
)
|
||||
|
||||
func init() {
|
||||
parse.MaxBuf *= 8
|
||||
}
|
||||
|
||||
type Text struct {
|
||||
m minify.Minify
|
||||
}
|
||||
|
||||
func NewText() *Text {
|
||||
m := minify.New()
|
||||
m.AddFunc("text/html", html.Minify)
|
||||
m.AddFunc("text/css", css.Minify)
|
||||
m.AddFunc("text/javascript", js.Minify)
|
||||
m.AddFunc("application/javascript", js.Minify)
|
||||
m.AddFunc("application/x-javascript", js.Minify)
|
||||
return &Text{
|
||||
m: m,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Text) Transcode(w *proxy.ResponseWriter, r *proxy.ResponseReader) error {
|
||||
return t.m.Minify(r.ContentType(), w, r)
|
||||
}
|
Reference in New Issue
Block a user