2023-09-12 00:11:45 +00:00
|
|
|
/*
|
|
|
|
Copyright © 2023 Seednode <seednode@seedno.de>
|
|
|
|
*/
|
|
|
|
|
2023-09-12 21:32:19 +00:00
|
|
|
package types
|
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-12 21:32:19 +00:00
|
|
|
func RegisterText() *Type {
|
|
|
|
return &Type{
|
|
|
|
Css: func() string {
|
|
|
|
var css strings.Builder
|
|
|
|
|
|
|
|
css.WriteString(`html,body{margin:0;padding:0;height:100%;}`)
|
2023-09-13 00:12:40 +00:00
|
|
|
css.WriteString(`a{color:inherit;display:block;height:100%;width:100%;text-decoration:none;overflow:hidden;}`)
|
2023-09-13 00:55:54 +00:00
|
|
|
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 21:32:19 +00:00
|
|
|
|
|
|
|
return css.String()
|
|
|
|
},
|
2023-09-12 00:11:45 +00:00
|
|
|
Title: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
|
|
|
return fmt.Sprintf(`<title>%s</title>`, fileName)
|
|
|
|
},
|
|
|
|
Body: func(queryParams, fileUri, filePath, fileName, mime string) string {
|
|
|
|
body, err := os.ReadFile(filePath)
|
|
|
|
if err != nil {
|
|
|
|
body = []byte{}
|
|
|
|
}
|
|
|
|
|
2023-09-13 00:12:40 +00:00
|
|
|
return fmt.Sprintf(`<a href="/%s"><textarea autofocus readonly>%s</textarea></a>`,
|
2023-09-12 00:11:45 +00:00
|
|
|
queryParams,
|
|
|
|
body)
|
|
|
|
},
|
2023-09-13 00:46:27 +00:00
|
|
|
Extensions: map[string]string{
|
|
|
|
`.css`: `text/css`,
|
|
|
|
`.csv`: `text/csv`,
|
|
|
|
`.html`: `text/html`,
|
|
|
|
`.js`: `text/javascript`,
|
|
|
|
`.json`: `application/json`,
|
|
|
|
`.md`: `text/markdown`,
|
|
|
|
`.txt`: `text/plain`,
|
|
|
|
`.xml`: `application/xml`,
|
2023-09-12 00:11:45 +00:00
|
|
|
},
|
|
|
|
MimeTypes: []string{
|
|
|
|
`application/json`,
|
|
|
|
`application/xml`,
|
|
|
|
`text/css`,
|
|
|
|
`text/csv`,
|
|
|
|
`text/javascript`,
|
|
|
|
`text/plain`,
|
|
|
|
`text/plain; charset=utf-8`,
|
|
|
|
},
|
2023-09-12 00:38:38 +00:00
|
|
|
Validate: func(path string) bool {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
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-12 00:11:45 +00:00
|
|
|
}
|
|
|
|
}
|