2023-09-12 00:11:45 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
package text
|
2023-09-12 00:11:45 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-12 00:38:38 +00:00
|
|
|
"errors"
|
2023-09-12 00:11:45 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-09-12 21:32:19 +00:00
|
|
|
"strings"
|
2023-09-12 00:11:45 +00:00
|
|
|
"unicode/utf8"
|
2023-09-13 04:35:17 +00:00
|
|
|
|
|
|
|
"seedno.de/seednode/roulette/types"
|
2023-09-12 00:11:45 +00:00
|
|
|
)
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
type Format struct{}
|
2023-09-12 21:32:19 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) Css() string {
|
2023-09-13 01:56:39 +00:00
|
|
|
var css strings.Builder
|
2023-09-12 21:32:19 +00:00
|
|
|
|
2023-09-13 01:56:39 +00:00
|
|
|
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
|
|
|
|
css.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;overflow:hidden;}`)
|
|
|
|
css.WriteString(`textarea{border:none;caret-color:transparent;outline:none;margin:.5rem;`)
|
|
|
|
css.WriteString(`height:99%;width:99%;white-space:pre;overflow:auto;}`)
|
2023-09-12 00:11:45 +00:00
|
|
|
|
2023-09-13 01:56:39 +00:00
|
|
|
return css.String()
|
|
|
|
}
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) Title(queryParams, fileUri, filePath, fileName, mime string) string {
|
2023-09-13 01:56:39 +00:00
|
|
|
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
|
|
|
}
|
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) Body(queryParams, fileUri, filePath, fileName, mime string) string {
|
2023-09-13 01:56:39 +00:00
|
|
|
body, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
body = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`,
|
|
|
|
queryParams,
|
|
|
|
body)
|
|
|
|
}
|
2023-09-12 00:38:38 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) Extensions() map[string]string {
|
2023-09-13 01:56:39 +00:00
|
|
|
return map[string]string{
|
|
|
|
`.css`: `text/css`,
|
|
|
|
`.csv`: `text/csv`,
|
2023-09-13 03:35:15 +00:00
|
|
|
`.htm`: `text/html`,
|
2023-09-13 01:56:39 +00:00
|
|
|
`.html`: `text/html`,
|
|
|
|
`.js`: `text/javascript`,
|
|
|
|
`.json`: `application/json`,
|
|
|
|
`.md`: `text/markdown`,
|
|
|
|
`.txt`: `text/plain`,
|
|
|
|
`.xml`: `application/xml`,
|
|
|
|
}
|
|
|
|
}
|
2023-09-12 00:38:38 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) MimeTypes() []string {
|
2023-09-13 01:56:39 +00:00
|
|
|
return []string{
|
|
|
|
`application/json`,
|
|
|
|
`application/xml`,
|
|
|
|
`text/css`,
|
|
|
|
`text/csv`,
|
2023-09-13 03:35:15 +00:00
|
|
|
`text/html`,
|
2023-09-13 01:56:39 +00:00
|
|
|
`text/javascript`,
|
|
|
|
`text/plain`,
|
|
|
|
`text/plain; charset=utf-8`,
|
2023-09-12 00:11:45 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 01:56:39 +00:00
|
|
|
|
2023-09-13 04:35:17 +00:00
|
|
|
func (t Format) Validate(filePath string) bool {
|
2023-09-13 01:56:39 +00:00
|
|
|
file, err := os.Open(filePath)
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, os.ErrNotExist):
|
|
|
|
return false
|
|
|
|
case err != nil:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
head := make([]byte, 512)
|
|
|
|
file.Read(head)
|
|
|
|
|
|
|
|
return utf8.Valid(head)
|
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func New() Format {
|
|
|
|
return Format{}
|
|
|
|
}
|
2023-09-13 04:35:17 +00:00
|
|
|
|
2023-09-13 14:26:15 +00:00
|
|
|
func init() {
|
|
|
|
types.SupportedFormats.Register(New())
|
2023-09-13 04:35:17 +00:00
|
|
|
}
|