2023-09-11 11:25:39 -05:00
|
|
|
/*
|
2024-01-14 12:39:14 -06:00
|
|
|
Copyright © 2024 Seednode <seednode@seedno.de>
|
2023-09-11 11:25:39 -05:00
|
|
|
*/
|
|
|
|
|
2023-09-12 16:32:19 -05:00
|
|
|
package types
|
2023-09-11 11:25:39 -05:00
|
|
|
|
|
|
|
import (
|
2023-09-12 13:06:45 -05:00
|
|
|
"path/filepath"
|
2023-09-12 23:35:17 -05:00
|
|
|
"slices"
|
|
|
|
"strings"
|
2023-09-11 11:25:39 -05:00
|
|
|
)
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
var SupportedFormats = make(Types)
|
2023-09-12 23:35:17 -05:00
|
|
|
|
2023-09-12 20:56:39 -05:00
|
|
|
type Type interface {
|
2023-10-04 17:33:03 -05:00
|
|
|
// Returns either "inline" or "embed", depending on whether the file
|
|
|
|
// should be displayed inline (e.g. code) or embedded (e.g. images)
|
2023-09-15 15:13:45 -05:00
|
|
|
Type() string
|
2023-10-04 17:33:03 -05:00
|
|
|
|
|
|
|
// Returns a CSS string used to format the corresponding page
|
2023-09-12 20:56:39 -05:00
|
|
|
Css() string
|
2023-10-04 17:33:03 -05:00
|
|
|
|
|
|
|
// Returns an HTML <title> element for the specified file
|
2023-09-15 14:28:21 -05:00
|
|
|
Title(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
|
2023-10-04 17:33:03 -05:00
|
|
|
|
|
|
|
// Returns an HTML <body> element used to display the specified file
|
2023-09-15 14:28:21 -05:00
|
|
|
Body(rootUrl, fileUri, filePath, fileName, prefix, mime string) (string, error)
|
2023-10-04 17:33:03 -05:00
|
|
|
|
|
|
|
// Returns a map of file extensions to MIME type strings.
|
2023-09-12 20:56:39 -05:00
|
|
|
Extensions() map[string]string
|
2023-10-04 17:33:03 -05:00
|
|
|
|
2023-10-17 20:38:45 -05:00
|
|
|
// Given a file extension, returns the corresponding media type,
|
2023-10-04 17:33:03 -05:00
|
|
|
// if one exists. Otherwise, returns an empty string.
|
2023-10-09 09:47:17 -05:00
|
|
|
MediaType(extension string) string
|
2023-10-04 17:33:03 -05:00
|
|
|
|
|
|
|
// Optional function used to validate whether a given file matches this format.
|
|
|
|
// If no validation checks are needed, this function should always return true.
|
2023-09-12 20:56:39 -05:00
|
|
|
Validate(filePath string) bool
|
2023-09-11 11:25:39 -05:00
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
type Types map[string]Type
|
2023-09-11 11:25:39 -05:00
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func (t Types) Add(format Type) {
|
2023-09-14 17:37:22 -05:00
|
|
|
for k := range format.Extensions() {
|
2023-10-04 14:09:49 -05:00
|
|
|
_, exists := t[k]
|
2023-09-12 13:06:45 -05:00
|
|
|
if !exists {
|
2023-10-04 14:09:49 -05:00
|
|
|
t[k] = format
|
2023-09-11 11:25:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func (t Types) FileType(path string) Type {
|
|
|
|
fileType, exists := t[filepath.Ext(path)]
|
2023-09-12 13:06:45 -05:00
|
|
|
if exists {
|
2023-09-14 17:37:22 -05:00
|
|
|
return fileType
|
2023-09-11 11:25:39 -05:00
|
|
|
}
|
|
|
|
|
2023-09-14 17:37:22 -05:00
|
|
|
return nil
|
2023-09-11 11:25:39 -05:00
|
|
|
}
|
2023-09-12 23:35:17 -05:00
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func (t Types) Register(format Type) {
|
2023-09-13 09:26:15 -05:00
|
|
|
t.Add(format)
|
2023-09-12 23:35:17 -05:00
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func (t Types) Validate(path string) bool {
|
|
|
|
format, exists := t[filepath.Ext(path)]
|
2023-09-14 19:10:55 -05:00
|
|
|
if !exists {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return format.Validate(path)
|
|
|
|
}
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
func (t Types) GetExtensions() string {
|
2023-09-12 23:35:17 -05:00
|
|
|
var output strings.Builder
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
extensions := make([]string, len(t))
|
2023-09-12 23:35:17 -05:00
|
|
|
|
|
|
|
i := 0
|
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
for k := range t {
|
2023-09-12 23:35:17 -05:00
|
|
|
extensions[i] = k
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
slices.Sort(extensions)
|
|
|
|
|
|
|
|
for _, v := range extensions {
|
|
|
|
output.WriteString(v + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String()
|
|
|
|
}
|
|
|
|
|
2023-10-09 09:47:17 -05:00
|
|
|
func (t Types) GetMediaTypes() string {
|
2023-09-12 23:35:17 -05:00
|
|
|
var output strings.Builder
|
|
|
|
|
2023-10-09 09:47:17 -05:00
|
|
|
var mediaTypes []string
|
2023-09-12 23:35:17 -05:00
|
|
|
|
2023-10-04 14:09:49 -05:00
|
|
|
for _, j := range t {
|
2023-09-14 17:37:22 -05:00
|
|
|
extensions := j.Extensions()
|
2024-01-15 08:51:41 -06:00
|
|
|
|
2023-09-14 17:37:22 -05:00
|
|
|
for _, v := range extensions {
|
2024-01-15 08:51:41 -06:00
|
|
|
if v != "" {
|
|
|
|
mediaTypes = append(mediaTypes, v)
|
|
|
|
}
|
2023-09-14 17:37:22 -05:00
|
|
|
}
|
2023-09-12 23:35:17 -05:00
|
|
|
}
|
|
|
|
|
2023-10-09 09:47:17 -05:00
|
|
|
mediaTypes = removeDuplicateStr(mediaTypes)
|
2023-09-14 17:37:22 -05:00
|
|
|
|
2023-10-09 09:47:17 -05:00
|
|
|
slices.Sort(mediaTypes)
|
2023-09-12 23:35:17 -05:00
|
|
|
|
2023-10-09 09:47:17 -05:00
|
|
|
for _, v := range mediaTypes {
|
2023-09-12 23:35:17 -05:00
|
|
|
output.WriteString(v + "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.String()
|
|
|
|
}
|
2023-09-14 17:37:22 -05: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
|
|
|
|
}
|