forked from tmedwards/tweego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
125 lines (111 loc) · 3.34 KB
/
util.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
/*
Copyright © 2014–2020 Thomas Michael Edwards. All rights reserved.
Use of this source code is governed by a Simplified BSD License which
can be found in the LICENSE file.
*/
package main
import (
"path/filepath"
"regexp"
"strings"
)
func mediaTypeFromFilename(filename string) string {
return mediaTypeFromExt(normalizedFileExt(filename))
}
func mediaTypeFromExt(ext string) string {
var mediaType string
switch ext {
// AUDIO NOTES:
//
// The preferred media type for WAVE audio is `audio/wave`, however,
// some browsers only recognize `audio/wav`, requiring its use instead.
case "aac", "flac", "ogg", "wav":
mediaType = "audio/" + ext
case "mp3":
mediaType = "audio/mpeg"
case "m4a":
mediaType = "audio/mp4"
case "oga", "opus":
mediaType = "audio/ogg"
case "wave":
mediaType = "audio/wav"
case "weba":
mediaType = "audio/webm"
// FONT NOTES:
//
// (ca. 2017) The IANA deprecated the various font subtypes of the
// "application" type in favor of the new "font" type. While the
// standards were new at that point, many browsers had long accepted
// such media types due to existing use in the wild—erroneous at
// that point or not.
// otf : application/font-sfnt → font/otf
// ttf : application/font-sfnt → font/ttf
// woff : application/font-woff → font/woff
// woff2 : application/font-woff2 → font/woff2
case "otf", "ttf", "woff", "woff2":
mediaType = "font/" + ext
// IMAGE NOTES:
case "gif", "jpeg", "png", "tiff", "webp":
mediaType = "image/" + ext
case "jpg":
mediaType = "image/jpeg"
case "svg":
mediaType = "image/svg+xml"
case "tif":
mediaType = "image/tiff"
// METADATA NOTES:
//
// Name aside, WebVTT files are generic media cue metadata files
// that may be used with either `<audio>` or `<video>` elements.
case "vtt": // WebVTT (Web Video Text Tracks)
mediaType = "text/vtt"
// VIDEO NOTES:
case "mp4", "webm":
mediaType = "video/" + ext
case "ogv":
mediaType = "video/ogg"
}
return mediaType
}
func normalizedFileExt(filename string) string {
ext := filepath.Ext(filename)
if ext == "" {
return ext
}
return strings.ToLower(ext[1:])
}
// Returns a trimmed and encoded slug of the passed string that should be safe
// for use as a DOM ID or class name.
func slugify(original string) string {
// NOTE: The range of illegal characters consists of: C0 controls, space, exclamation,
// double quote, number, dollar, percent, ampersand, single quote, left paren, right
// paren, asterisk, plus, comma, hyphen, period, forward slash, colon, semi-colon,
// less-than, equals, greater-than, question, at, left bracket, backslash, right
// bracket, caret, backquote/grave, left brace, pipe/vertical-bar, right brace, tilde,
// delete, C1 controls.
illegalRe := regexp.MustCompile(`[\x00-\x20!-/:-@[-^\x60{-\x9f]+`)
return illegalRe.ReplaceAllLiteralString(original, "_")
}
func stringSliceContains(haystack []string, needle string) bool {
if len(haystack) > 0 {
for _, val := range haystack {
if val == needle {
return true
}
}
}
return false
}
func stringSliceDelete(haystack []string, needle string) []string {
if len(haystack) > 0 {
for i, val := range haystack {
if val == needle {
copy(haystack[i:], haystack[i+1:])
haystack[len(haystack)-1] = ""
haystack = haystack[:len(haystack)-1]
break
}
}
}
return haystack
}