-
Notifications
You must be signed in to change notification settings - Fork 21
/
media_parser.go
124 lines (91 loc) · 2.7 KB
/
media_parser.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package jpegstructure
import (
"bufio"
"bytes"
"io"
"os"
"github.com/dsoprea/go-logging"
"github.com/dsoprea/go-utility/image"
)
// JpegMediaParser is a `riimage.MediaParser` that knows how to parse JPEG
// images.
type JpegMediaParser struct {
}
// NewJpegMediaParser returns a new JpegMediaParser.
func NewJpegMediaParser() *JpegMediaParser {
// TODO(dustin): Add test
return new(JpegMediaParser)
}
// Parse parses a JPEG uses an `io.ReadSeeker`. Even if it fails, it will return
// the list of segments encountered prior to the failure.
func (jmp *JpegMediaParser) Parse(rs io.ReadSeeker, size int) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
s := bufio.NewScanner(rs)
// Since each segment can be any size, our buffer must allowed to grow as
// large as the file.
buffer := []byte{}
s.Buffer(buffer, size)
js := NewJpegSplitter(nil)
s.Split(js.Split)
for s.Scan() != false {
}
// Always return the segments that were parsed, at least until there was an
// error.
ec = js.Segments()
log.PanicIf(s.Err())
return ec, nil
}
// ParseFile parses a JPEG file. Even if it fails, it will return the list of
// segments encountered prior to the failure.
func (jmp *JpegMediaParser) ParseFile(filepath string) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
// TODO(dustin): Add test
f, err := os.Open(filepath)
log.PanicIf(err)
defer f.Close()
stat, err := f.Stat()
log.PanicIf(err)
size := stat.Size()
sl, err := jmp.Parse(f, int(size))
// Always return the segments that were parsed, at least until there was an
// error.
ec = sl
log.PanicIf(err)
return ec, nil
}
// ParseBytes parses a JPEG byte-slice. Even if it fails, it will return the
// list of segments encountered prior to the failure.
func (jmp *JpegMediaParser) ParseBytes(data []byte) (ec riimage.MediaContext, err error) {
defer func() {
if state := recover(); state != nil {
err = log.Wrap(state.(error))
}
}()
br := bytes.NewReader(data)
sl, err := jmp.Parse(br, len(data))
// Always return the segments that were parsed, at least until there was an
// error.
ec = sl
log.PanicIf(err)
return ec, nil
}
// LooksLikeFormat indicates whether the data looks like a JPEG image.
func (jmp *JpegMediaParser) LooksLikeFormat(data []byte) bool {
if len(data) < 4 {
return false
}
// https://cs.opensource.google/go/go/+/master:src/net/http/sniff.go;l=126;drc=8b364451e2e2f2f816ed877a4639d9342279f299
return bytes.HasPrefix(data, []byte("\xFF\xD8\xFF"))
}
var (
// Enforce interface conformance.
_ riimage.MediaParser = new(JpegMediaParser)
)