-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibfs.go
135 lines (124 loc) · 3.63 KB
/
libfs.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
125
126
127
128
129
130
131
132
133
134
135
package frizzante
import (
"embed"
"errors"
"os"
"path/filepath"
)
// EmbeddedExists checks if fileName exists.
func EmbeddedExists(embeddedFileSystem embed.FS, fileName string) bool {
return EmbeddedIsFile(embeddedFileSystem, fileName) || EmbeddedIsDirectory(embeddedFileSystem, fileName)
}
// EmbeddedIsFile check if fileName exists and is a file.
func EmbeddedIsFile(embeddedFileSystem embed.FS, fileName string) bool {
_, err := embeddedFileSystem.ReadFile(fileName)
if err != nil {
return false
}
return true
}
// EmbeddedIsDirectory checks if fileName exists and is a directory.
func EmbeddedIsDirectory(embeddedFileSystem embed.FS, fileName string) bool {
_, err := embeddedFileSystem.ReadDir(fileName)
if err != nil {
return false
}
return true
}
// Exists checks if fileName exists.
func Exists(fileName string) bool {
_, statError := os.Stat(fileName)
return nil == statError || !errors.Is(statError, os.ErrNotExist)
}
// IsFile check if fileName exists and is a file.
func IsFile(fileName string) bool {
stat, statError := os.Stat(fileName)
if statError != nil {
return !errors.Is(statError, os.ErrNotExist)
}
return !stat.IsDir()
}
// IsDirectory checks if fileName exists and is a directory.
func IsDirectory(fileName string) bool {
stat, statError := os.Stat(fileName)
if statError != nil {
return !errors.Is(statError, os.ErrNotExist)
}
return stat.IsDir()
}
var mimes = map[string]string{
".html": "text/html",
".css": "text/css",
".txt": "text/plain",
".ttf": "font/ttf",
".woff": "font/woff",
".woff2": "font/woff2",
".ico": "image/x-icon",
".jpeg": "image/jpeg",
".jpg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".bmp": "image/bmp",
".svg": "image/svg+xml",
".tif": "image/tiff",
".tiff": "image/tiff",
".js": "text/javascript",
".json": "application/json",
".pdf": "application/pdf",
".avi": "video/x-msvideo",
".mp4": "video/mp4",
".mpeg": "video/mpeg",
".ogv": "video/ogg",
".webm": "video/webm",
".jpgv": "video/jpg",
".wasm": "application/wasm",
".mkv": "video/x-matroska",
".csv": "text/csv",
".ics": "text/calendar",
".sh": "application/x-sh",
".swf": "application/x-shockwave-flash",
".tar": "application/x-tar",
".xls": "application/vnd.ms-excel",
".xml": "application/xml",
".xul": "application/vnd.mozilla.xul+xml",
".zip": "application/zip",
".7z": "application/x-7z-compressed",
".apk": "application/vnd.android.package-archive",
".jar": "application/java-archive",
".vsd": "application/vnd.visio",
".xhtml": "application/xhtml+xml",
".mpkg": "application/vnd.apple.installer+xml",
".ppt": "application/vnd.ms-powerpoint",
".rar": "application/x-rar-compressed",
".rtf": "application/rtf",
".3gp": "video/3gpp",
".wav": "audio/x-wav",
".weba": "audio/webm",
".mp3": "audio/mpeg",
".3g2": "video/3gpp2",
".aac": "audio/aac",
".midi": "audio/midi",
".mid": "audio/midi",
".oga": "audio/og",
".abw": "application/x-abiword",
".arc": "application/octet-stream",
".azw": "application/vnd.amazon.ebook",
".bin": "application/octet-stream",
".bz": "application/x-bzip",
".bz2": "application/x-bzip2",
".csh": "application/x-csh",
".doc": "application/msword",
".epub": "application/epub+zip",
".odp": "application/vnd.oasis.opendocument.presentation",
".ods": "application/vnd.oasis.opendocument.spreadsheet",
".odt": "application/vnd.oasis.opendocument.text",
".ogx": "application/ogg",
}
func Mime(fileName string) string {
extensionName := filepath.Ext(fileName)
mime, ok := mimes[extensionName]
if ok {
return mime
}
return "text/plain"
}