Skip to content

Commit e30b27b

Browse files
committed
change param to readseeker
1 parent c768e06 commit e30b27b

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

validator.go

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,35 @@ import (
55
"io"
66
)
77

8-
type function func(io.Reader) bool
8+
type function func(io.ReadSeeker) bool
99

1010
// IsOneOf function will validate File with multiple function
11-
func IsOneOf(r io.Reader, functions ...function) bool {
11+
func IsOneOf(r io.ReadSeeker, functions ...function) bool {
1212
for _, f := range functions {
13-
bufRead := &bytes.Buffer{}
14-
tee := io.TeeReader(r, bufRead)
15-
16-
valid := f(tee)
17-
13+
valid := f(r)
1814
if valid {
1915
return true
2016
}
21-
22-
r = bufRead
2317
}
2418

2519
return false
2620
}
2721

28-
func readUntil(l int, r io.Reader) ([]byte, error) {
22+
func readUntil(l int, r io.ReadSeeker) ([]byte, error) {
2923
buff := make([]byte, l)
3024

3125
_, err := r.Read(buff)
3226
if err != nil {
3327
return nil, err
3428
}
3529

30+
r.Seek(0, io.SeekStart)
31+
3632
return buff, nil
3733
}
3834

3935
// IsPng function will return true if File is a valid PNG
40-
func IsPng(r io.Reader) bool {
36+
func IsPng(r io.ReadSeeker) bool {
4137
l := len(PNG)
4238

4339
buff, err := readUntil(l, r)
@@ -54,7 +50,7 @@ func IsPng(r io.Reader) bool {
5450
}
5551

5652
// IsJpeg function will return true if File is a valid JPEG
57-
func IsJpeg(r io.Reader) bool {
53+
func IsJpeg(r io.ReadSeeker) bool {
5854
l := len(JPEG)
5955

6056
buff, err := readUntil(l, r)
@@ -71,7 +67,7 @@ func IsJpeg(r io.Reader) bool {
7167
}
7268

7369
// IsPdf function will return true if File is a valid PDF
74-
func IsPdf(r io.Reader) bool {
70+
func IsPdf(r io.ReadSeeker) bool {
7571
l := len(PDF)
7672

7773
buff, err := readUntil(l, r)
@@ -88,7 +84,7 @@ func IsPdf(r io.Reader) bool {
8884
}
8985

9086
// IsGif function will return true if File is a valid GIF
91-
func IsGif(r io.Reader) bool {
87+
func IsGif(r io.ReadSeeker) bool {
9288
l := len(GIF)
9389

9490
buff, err := readUntil(l, r)
@@ -105,7 +101,7 @@ func IsGif(r io.Reader) bool {
105101
}
106102

107103
// IsAvif function will return true if File is a valid AVIF
108-
func IsAvif(r io.Reader) bool {
104+
func IsAvif(r io.ReadSeeker) bool {
109105
l := len(AVIF)
110106

111107
buff, err := readUntil(l, r)
@@ -122,7 +118,7 @@ func IsAvif(r io.Reader) bool {
122118
}
123119

124120
// IsBmp function will return true if File is a valid BMP
125-
func IsBmp(r io.Reader) bool {
121+
func IsBmp(r io.ReadSeeker) bool {
126122
l := len(BMP)
127123

128124
buff, err := readUntil(l, r)
@@ -139,7 +135,7 @@ func IsBmp(r io.Reader) bool {
139135
}
140136

141137
// IsDib function will return true if File is a valid DIB
142-
func IsDib(r io.Reader) bool {
138+
func IsDib(r io.ReadSeeker) bool {
143139
l := len(DIB)
144140

145141
buff, err := readUntil(l, r)
@@ -156,7 +152,7 @@ func IsDib(r io.Reader) bool {
156152
}
157153

158154
// IsTiff function will return true if File is a valid TIFF
159-
func IsTiff(r io.Reader) bool {
155+
func IsTiff(r io.ReadSeeker) bool {
160156
l := len(TIFF)
161157

162158
buff, err := readUntil(l, r)
@@ -173,7 +169,7 @@ func IsTiff(r io.Reader) bool {
173169
}
174170

175171
// IsMp3 function will return true if File is a valid MP3
176-
func IsMp3(r io.Reader) bool {
172+
func IsMp3(r io.ReadSeeker) bool {
177173
l := len(MP3)
178174

179175
buff, err := readUntil(l, r)
@@ -190,7 +186,7 @@ func IsMp3(r io.Reader) bool {
190186
}
191187

192188
// IsMpg function will return true if File is a valid MPG
193-
func IsMpg(r io.Reader) bool {
189+
func IsMpg(r io.ReadSeeker) bool {
194190
l1 := len(MPG_0)
195191

196192
buff, err := readUntil(l1, r)
@@ -208,7 +204,7 @@ func IsMpg(r io.Reader) bool {
208204
}
209205

210206
// IsFlv function will return true if File is a valid FLV
211-
func IsFlv(r io.Reader) bool {
207+
func IsFlv(r io.ReadSeeker) bool {
212208
l := len(FLV)
213209

214210
buff, err := readUntil(l, r)
@@ -225,7 +221,7 @@ func IsFlv(r io.Reader) bool {
225221
}
226222

227223
// IsApk function will return true if File is a valid APK
228-
func IsApk(r io.Reader) bool {
224+
func IsApk(r io.ReadSeeker) bool {
229225
l := len(APK)
230226

231227
buff, err := readUntil(l, r)
@@ -242,7 +238,7 @@ func IsApk(r io.Reader) bool {
242238
}
243239

244240
// IsMsOffice function will return true if File is a valid MS OFFICE Document (DOCX|PPTX|XLSX)
245-
func IsMsOffice(r io.Reader) bool {
241+
func IsMsOffice(r io.ReadSeeker) bool {
246242
l := len(MS_OFFICE)
247243

248244
buff, err := readUntil(l, r)
@@ -259,7 +255,7 @@ func IsMsOffice(r io.Reader) bool {
259255
}
260256

261257
// IsJar function will return true if File is a valid JAR (Java Archive)
262-
func IsJar(r io.Reader) bool {
258+
func IsJar(r io.ReadSeeker) bool {
263259
l := len(JAR)
264260

265261
buff, err := readUntil(l, r)
@@ -276,7 +272,7 @@ func IsJar(r io.Reader) bool {
276272
}
277273

278274
// IsSwf function will return true if File is a valid SWF
279-
func IsSwf(r io.Reader) bool {
275+
func IsSwf(r io.ReadSeeker) bool {
280276
l := len(SWF)
281277

282278
buff, err := readUntil(l, r)
@@ -293,7 +289,7 @@ func IsSwf(r io.Reader) bool {
293289
}
294290

295291
// Is3gp function will return true if File is a valid 3gp
296-
func Is3gp(r io.Reader) bool {
292+
func Is3gp(r io.ReadSeeker) bool {
297293
l1 := len(THREE_GP_0)
298294

299295
buff, err := readUntil(l1, r)
@@ -312,7 +308,7 @@ func Is3gp(r io.Reader) bool {
312308
}
313309

314310
// IsMkv function will return true if File is a valid MKV
315-
func IsMkv(r io.Reader) bool {
311+
func IsMkv(r io.ReadSeeker) bool {
316312
l := len(MKV)
317313

318314
buff, err := readUntil(l, r)
@@ -329,7 +325,7 @@ func IsMkv(r io.Reader) bool {
329325
}
330326

331327
// IsRar function will return true if File is a valid RAR
332-
func IsRar(r io.Reader) bool {
328+
func IsRar(r io.ReadSeeker) bool {
333329
l := len(RAR)
334330

335331
buff, err := readUntil(l, r)
@@ -346,7 +342,7 @@ func IsRar(r io.Reader) bool {
346342
}
347343

348344
// IsGzip function will return true if File is a valid GZIP
349-
func IsGzip(r io.Reader) bool {
345+
func IsGzip(r io.ReadSeeker) bool {
350346
l := len(GZIP)
351347

352348
buff, err := readUntil(l, r)
@@ -363,7 +359,7 @@ func IsGzip(r io.Reader) bool {
363359
}
364360

365361
// IsZip function will return true if File is a valid ZIP
366-
func IsZip(r io.Reader) bool {
362+
func IsZip(r io.ReadSeeker) bool {
367363
l := len(ZIP)
368364

369365
buff, err := readUntil(l, r)
@@ -380,7 +376,7 @@ func IsZip(r io.Reader) bool {
380376
}
381377

382378
// IsWebp function will return true if File is a valid Webp
383-
func IsWebp(r io.Reader) bool {
379+
func IsWebp(r io.ReadSeeker) bool {
384380
l := len(WEBP)
385381

386382
buff, err := readUntil(l, r)
@@ -396,7 +392,7 @@ func IsWebp(r io.Reader) bool {
396392
return true
397393
}
398394

399-
func IsMp4(r io.Reader) bool {
395+
func IsMp4(r io.ReadSeeker) bool {
400396
l := len(MP4_0)
401397

402398
buff, err := readUntil(l, r)

validator_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func TestPdf(t *testing.T) {
9-
buff := bytes.NewBuffer([]byte("%PDF-1.3"))
9+
buff := bytes.NewReader([]byte("%PDF-1.3"))
1010

1111
valid := IsPdf(buff)
1212
if !valid {
@@ -15,7 +15,7 @@ func TestPdf(t *testing.T) {
1515
}
1616

1717
func TestMpg0(t *testing.T) {
18-
buff := bytes.NewBuffer(MPG_0)
18+
buff := bytes.NewReader(MPG_0)
1919

2020
valid := IsMpg(buff)
2121
if !valid {
@@ -24,7 +24,7 @@ func TestMpg0(t *testing.T) {
2424
}
2525

2626
func TestMpg1(t *testing.T) {
27-
buff := bytes.NewBuffer(MPG_1)
27+
buff := bytes.NewReader(MPG_1)
2828

2929
valid := IsMpg(buff)
3030
if !valid {

0 commit comments

Comments
 (0)