2023-09-11 16:25:39 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
package types
|
2023-09-11 16:25:39 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-12 18:06:45 +00:00
|
|
|
"path/filepath"
|
2023-09-13 04:35:17 +00:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2023-09-11 16:25:39 +00:00
|
|
|
)
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
var SupportedFormats = make(Types)
|
2023-09-13 04:35:17 +00:00
|
|
|
|
2023-09-13 01:56:39 +00:00
|
|
|
type Type interface {
|
2023-09-15 20:13:45 +00:00
|
|
|
Type() string
|
2023-09-13 01:56:39 +00:00
|
|
|
Css() string
|
2023-09-15 19:28:21 +00:00
|
|
|
Title(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
|
|
|
|
Body(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
|
2023-09-13 01:56:39 +00:00
|
|
|
Extensions() map[string]string
|
2023-09-14 22:37:22 +00:00
|
|
|
MimeType(string) string
|
2023-09-13 01:56:39 +00:00
|
|
|
Validate(filePath string) bool
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
type Types map[string]Type
|
2023-09-11 16:25:39 +00:00
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) Add(format Type) {
|
2023-09-14 22:37:22 +00:00
|
|
|
for k := range format.Extensions() {
|
2023-10-04 19:09:49 +00:00
|
|
|
_, exists := t[k]
|
2023-09-12 18:06:45 +00:00
|
|
|
if !exists {
|
2023-10-04 19:09:49 +00:00
|
|
|
t[k] = format
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) FileType(path string) Type {
|
|
|
|
fileType, exists := t[filepath.Ext(path)]
|
2023-09-12 18:06:45 +00:00
|
|
|
if exists {
|
2023-09-14 22:37:22 +00:00
|
|
|
return fileType
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
return nil
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) Register(format Type) {
|
2023-09-13 14:26:15 +00:00
|
|
|
t.Add(format)
|
2023-09-13 04:35:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) Validate(path string) bool {
|
|
|
|
format, exists := t[filepath.Ext(path)]
|
2023-09-15 00:10:55 +00:00
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return format.Validate(path)
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) GetExtensions() string {
|
2023-09-13 04:35:17 +00:00
|
|
|
var output strings.Builder
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
extensions := make([]string, len(t))
|
2023-09-13 04:35:17 +00:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
for k := range t {
|
2023-09-13 04:35:17 +00:00
|
|
|
extensions[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Sort(extensions)
|
|
|
|
|
|
|
|
for _, v := range extensions {
|
|
|
|
output.WriteString(v + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String()
|
|
|
|
}
|
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
func (t Types) GetMimeTypes() string {
|
2023-09-13 04:35:17 +00:00
|
|
|
var output strings.Builder
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
var mimeTypes []string
|
2023-09-13 04:35:17 +00:00
|
|
|
|
2023-10-04 19:09:49 +00:00
|
|
|
for _, j := range t {
|
2023-09-14 22:37:22 +00:00
|
|
|
extensions := j.Extensions()
|
|
|
|
for _, v := range extensions {
|
|
|
|
mimeTypes = append(mimeTypes, v)
|
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 22:37:22 +00:00
|
|
|
mimeTypes = removeDuplicateStr(mimeTypes)
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
slices.Sort(mimeTypes)
|
|
|
|
|
|
|
|
for _, v := range mimeTypes {
|
|
|
|
output.WriteString(v + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String()
|
|
|
|
}
|
2023-09-14 22:37:22 +00:00
|
|
|
|
|
|
|
func removeDuplicateStr(strSlice []string) []string {
|
|
|
|
allKeys := make(map[string]bool)
|
|
|
|
list := []string{}
|
|
|
|
for _, item := range strSlice {
|
|
|
|
if _, value := allKeys[item]; !value {
|
|
|
|
allKeys[item] = true
|
|
|
|
list = append(list, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|