-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathfile.go
90 lines (71 loc) · 1.82 KB
/
file.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
// Copyright 2015 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package model
import (
"github.com/andreaskoch/allmark/dataaccess"
"strings"
)
// A File represents a file ressource that is associated with an Item.
type File struct {
dataaccess.File
}
// IsImageFile returns true if the supplied file model is an image.
func IsImageFile(file *File) bool {
mimetype, err := GetMimeType(file)
if err != nil {
return false
}
return IsImage(mimetype)
}
// IsImage returns true if the supplied mime type is an image.
func IsImage(mimetype string) bool {
return strings.HasPrefix(mimetype, "image/")
}
// IsTextFile returns true if the supplied file model is a text file.
func IsTextFile(file *File) bool {
mimetype, err := GetMimeType(file)
if err != nil {
return false
}
if strings.HasPrefix(mimetype, "text/") {
return true
}
if strings.Contains(mimetype, "json") {
return true
}
if strings.Contains(mimetype, "javascript") {
return true
}
if strings.Contains(mimetype, "xml") {
return true
}
if strings.Contains(mimetype, "cert") {
return true
}
return false
}
// IsAudioFile returns true if the supplied file model is an audio file.
func IsAudioFile(file *File) bool {
mimetype, err := GetMimeType(file)
if err != nil {
return false
}
return strings.HasPrefix(mimetype, "audio/")
}
// IsVideoFile returns true if the supplied file model is a video file.
func IsVideoFile(file *File) bool {
mimetype, err := GetMimeType(file)
if err != nil {
return false
}
return strings.HasPrefix(mimetype, "video/")
}
// GetMimeType returns the mime type if the given file model.
func GetMimeType(file *File) (string, error) {
mimetype, err := file.MimeType()
if err != nil {
return "", err
}
return mimetype, nil
}