2023-09-11 16:25:39 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
package formats
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-09-11 21:05:38 +00:00
|
|
|
"net/http"
|
2023-09-11 16:25:39 +00:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SupportedFormat struct {
|
2023-09-12 00:43:18 +00:00
|
|
|
Css string
|
|
|
|
Title func(queryParams, fileUri, filePath, fileName, mime string) string
|
|
|
|
Body func(queryParams, fileUri, filePath, fileName, mime string) string
|
2023-09-11 16:25:39 +00:00
|
|
|
Extensions []string
|
2023-09-11 21:05:38 +00:00
|
|
|
MimeTypes []string
|
2023-09-12 00:43:18 +00:00
|
|
|
Validate func(filePath string) bool
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SupportedFormats struct {
|
|
|
|
types []*SupportedFormat
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SupportedFormats) Add(t *SupportedFormat) {
|
|
|
|
s.types = append(s.types, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SupportedFormats) Extensions() []string {
|
2023-09-11 21:05:38 +00:00
|
|
|
var extensions []string
|
2023-09-11 16:25:39 +00:00
|
|
|
|
|
|
|
for _, t := range s.types {
|
2023-09-11 21:05:38 +00:00
|
|
|
extensions = append(extensions, t.Extensions...)
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
return extensions
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
func (s *SupportedFormats) MimeTypes() []string {
|
|
|
|
var mimeTypes []string
|
|
|
|
|
|
|
|
for _, t := range s.types {
|
|
|
|
mimeTypes = append(mimeTypes, t.MimeTypes...)
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
return mimeTypes
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
func (s *SupportedFormats) Type(mimeType string) *SupportedFormat {
|
2023-09-11 16:25:39 +00:00
|
|
|
for i := range s.types {
|
2023-09-11 21:05:38 +00:00
|
|
|
for _, m := range s.types[i].MimeTypes {
|
|
|
|
if mimeType == m {
|
|
|
|
return s.types[i]
|
|
|
|
}
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
return nil
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func FileType(path string, types *SupportedFormats) (bool, *SupportedFormat, string, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return false, nil, "", nil
|
|
|
|
case err != nil:
|
|
|
|
return false, nil, "", err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
head := make([]byte, 512)
|
2023-09-11 16:25:39 +00:00
|
|
|
file.Read(head)
|
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
mimeType := http.DetectContentType(head)
|
2023-09-11 16:25:39 +00:00
|
|
|
|
2023-09-11 21:05:38 +00:00
|
|
|
for _, v := range types.MimeTypes() {
|
|
|
|
if mimeType == v {
|
|
|
|
fileType := types.Type(mimeType)
|
2023-09-11 16:25:39 +00:00
|
|
|
|
2023-09-12 00:38:38 +00:00
|
|
|
return fileType.Validate(path), fileType, mimeType, nil
|
2023-09-11 16:25:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, "", nil
|
|
|
|
}
|