-
Notifications
You must be signed in to change notification settings - Fork 0
/
filespec.go
162 lines (121 loc) · 3.74 KB
/
filespec.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package mediaserver
import (
"mime"
"net/url"
"strings"
"github.com/benpate/rosetta/convert"
"github.com/benpate/rosetta/list"
"github.com/rs/zerolog/log"
)
// FileSpec represents all the parameters available for requesting a file.
// This can be generated directly from a URL.
type FileSpec struct {
Filename string
Extension string
Width int
Height int
MimeType string
}
// NewFileSpec reads a URL and returns a fully populated FileSpec
func NewFileSpec(file *url.URL, defaultType string) FileSpec {
fullname := list.Slash(file.Path).Last()
filename, extension := list.Dot(fullname).SplitTail()
if extension == "" {
extension = strings.ToLower(defaultType)
} else {
extension = "." + strings.ToLower(extension)
}
mimeType := mime.TypeByExtension(extension)
height := convert.Int(file.Query().Get("height"))
width := convert.Int(file.Query().Get("width"))
return FileSpec{
Filename: filename.String(),
Extension: extension,
Width: width,
Height: height,
MimeType: mimeType,
}
}
// MimeCategory returns the first half of the mime type
func (ms *FileSpec) MimeCategory() string {
return list.Slash(ms.MimeType).First()
}
// CachePath returns the complete path (within the cache directory) to the file requested by this FileSpec
func (ms *FileSpec) CachePath() string {
return ms.CacheDir() + "/" + ms.CacheFilename()
}
// CacheDir returns the name of the directory within the cache where versions of this file will be stored.
func (ms *FileSpec) CacheDir() string {
return ms.Filename
}
// CacheFilename returns the filename to be used when retrieving this from the FileSpec cache.
func (ms *FileSpec) CacheFilename() string {
var buffer strings.Builder
buffer.WriteString("cached")
if ms.MimeCategory() == "image" {
if ms.Width != 0 {
buffer.WriteString("_w" + convert.String(ms.Width))
}
if ms.Height != 0 {
buffer.WriteString("_h" + convert.String(ms.Height))
}
}
buffer.WriteString(ms.Extension)
return buffer.String()
}
func (ms *FileSpec) AspectRatio() float64 {
if (ms.Width == 0) || (ms.Height == 0) {
return 0
}
return float64(ms.Width) / float64(ms.Height)
}
// Resize returns TRUE if the FileSpec is requesting that the file be resized.
func (ms *FileSpec) Resize() bool {
return (ms.Width > 0) || (ms.Height > 0)
}
// CacheWidth returns the width of the file to save in the cache
func (ms *FileSpec) CacheWidth() int {
return round100(ms.Width)
}
// CacheHeight returns the height of the file to save in the cache
func (ms *FileSpec) CacheHeight() int {
return round100(ms.Height)
}
func (ms *FileSpec) ffmpegArguments() []string {
// Build the command line arguments
result := make([]string, 0)
switch ms.MimeCategory() {
case "image":
// Determine new image dimensions
width := convert.String(first(ms.CacheWidth(), -1))
height := convert.String(first(ms.CacheHeight(), -1))
filters := make([]string, 0)
if ms.Resize() {
if ms.Width == ms.Height {
filters = append(filters, "crop='min(iw,ih)':'min(iw,ih)'")
}
filters = append(filters, "scale='min("+width+",iw)':'min("+height+",ih)'")
}
if len(filters) > 0 {
result = append(result, "-vf", strings.Join(filters, ", "))
}
result = append(result, "-f", ms.Extension[1:])
case "audio":
switch ms.Extension {
case ".flac":
result = append(result, "-c:a", "flac")
result = append(result, "-f", "flac")
case ".m4a", ".aac":
result = append(result, "-c:a", "aac")
result = append(result, "-movflags", "+faststart")
result = append(result, "-f", "adts")
default:
ms.Extension = ".mp3"
result = append(result, "-c:a", "libmp3lame")
result = append(result, "-f", "mp3")
}
case "video":
}
log.Debug().Msg("FFMPEG Arguments: " + strings.Join(result, " "))
return result
}