-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.go
51 lines (46 loc) · 818 Bytes
/
content.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
package imguuid
import (
"context"
"fmt"
"net/http"
"os"
)
// ContentCheck ...
func ContentCheck(ctx context.Context, paths <-chan string, c chan<- string) {
for path := range paths {
select {
case c <- detectContectType(path):
case <-ctx.Done():
return
}
}
}
func detectContectType(path string) string {
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
return ""
}
defer func() {
_ = file.Close()
}()
buf := make([]byte, 512)
_, err = file.Read(buf)
if err != nil {
fmt.Println(err)
return ""
}
filetype := http.DetectContentType(buf)
switch filetype {
case "image/jpeg", "image/jpg":
return path
case "image/png":
return path
default:
}
return ""
}
// DetectContentType ...
func DetectContentType(path string) string {
return detectContectType(path)
}