File: encoding_wrappers.go

package info (click to toggle)
miniflux 2.2.16-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,188 kB
  • sloc: xml: 4,853; javascript: 1,158; sh: 257; makefile: 161
file content (58 lines) | stat: -rw-r--r-- 1,199 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package fetcher // import "miniflux.app/v2/internal/reader/fetcher"

import (
	"compress/gzip"
	"io"

	"github.com/andybalholm/brotli"
)

type brotliReadCloser struct {
	body         io.ReadCloser
	brotliReader io.Reader
}

func NewBrotliReadCloser(body io.ReadCloser) *brotliReadCloser {
	return &brotliReadCloser{
		body:         body,
		brotliReader: brotli.NewReader(body),
	}
}

func (b *brotliReadCloser) Read(p []byte) (n int, err error) {
	return b.brotliReader.Read(p)
}

func (b *brotliReadCloser) Close() error {
	return b.body.Close()
}

type gzipReadCloser struct {
	body       io.ReadCloser
	gzipReader io.Reader
	gzipErr    error
}

func NewGzipReadCloser(body io.ReadCloser) *gzipReadCloser {
	return &gzipReadCloser{body: body}
}

func (gz *gzipReadCloser) Read(p []byte) (n int, err error) {
	if gz.gzipReader == nil {
		if gz.gzipErr == nil {
			gz.gzipReader, gz.gzipErr = gzip.NewReader(gz.body)
		}
		if gz.gzipErr != nil {
			return 0, gz.gzipErr
		}
	}

	return gz.gzipReader.Read(p)
}

func (gz *gzipReadCloser) Close() error {
	return gz.body.Close()
}