-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
56 lines (47 loc) · 1.16 KB
/
wrapper.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
package main
import (
"os"
"path"
"strings"
"github.com/nickryand/magic"
)
// Wrap a FileObj if there is support for the File's MIME type.
// Otherwise, use the MIME type determine the destination path
// basename.
func getWrappedFile(file *FileObj) (interface{}, error) {
mimeType, err := getFileType(file.filename)
if err != nil {
return nil, err
}
switch {
case strings.HasPrefix(mimeType, "image/jpeg"):
return &ImageObj{file}, nil
case strings.HasPrefix(mimeType, "image"):
file.destpath = path.Join(file.destpath, "image")
case strings.HasPrefix(mimeType, "video"):
file.destpath = path.Join(file.destpath, "video")
default:
file.destpath = path.Join(file.destpath, "other")
}
return file, nil
}
func getFileType(path string) (string, error) {
// Ensure file actually exists before we attempt to figure out what
// type of file it is.
if _, err := os.Stat(path); err != nil {
return "", err
}
conn, err := magic.Open(magic.FlagMimeType)
if err != nil {
return "", err
}
defer conn.Close()
if err = conn.Load(""); err != nil {
return "", err
}
output, err := conn.File(path)
if err != nil {
return "", err
}
return output, nil
}