1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00
amfora/renderer/page.go

142 lines
3.9 KiB
Go
Raw Normal View History

2020-06-20 00:09:01 -04:00
package renderer
import (
"bytes"
2020-06-20 00:09:01 -04:00
"errors"
"io"
2020-06-20 00:09:01 -04:00
"mime"
"os"
2020-06-20 00:09:01 -04:00
"strings"
2020-12-20 15:54:47 -05:00
"time"
2020-06-20 00:09:01 -04:00
"github.com/makeworld-the-better-one/amfora/structs"
"github.com/makeworld-the-better-one/go-gemini"
"github.com/spf13/viper"
2020-06-20 00:09:01 -04:00
"golang.org/x/text/encoding/ianaindex"
)
var ErrTooLarge = errors.New("page content would be too large")
var ErrTimedOut = errors.New("page download timed out")
var ErrCantDisplay = errors.New("invalid content for a page")
var ErrBadEncoding = errors.New("unsupported encoding")
2020-08-27 17:57:19 -04:00
var ErrBadMediatype = errors.New("displayable mediatype is not handled in the code, implementation error")
2020-06-20 00:09:01 -04:00
// isUTF8 returns true for charsets that are compatible with UTF-8 and don't need to be decoded.
func isUTF8(charset string) bool {
utfCharsets := []string{"", "utf-8", "us-ascii"}
for i := range utfCharsets {
if strings.ToLower(charset) == utfCharsets[i] {
return true
}
}
return false
}
// CanDisplay returns true if the response is supported by Amfora
// for displaying on the screen.
// It also doubles as a function to detect whether something can be stored in a Page struct.
func CanDisplay(res *gemini.Response) bool {
if gemini.SimplifyStatus(res.Status) != 20 {
// No content
return false
}
mediatype, params, err := mime.ParseMediaType(res.Meta)
if err != nil {
return false
}
if !strings.HasPrefix(mediatype, "text/") {
2020-06-20 00:09:01 -04:00
// Amfora doesn't support other filetypes
return false
}
if isUTF8(params["charset"]) {
return true
}
enc, err := ianaindex.MIME.Encoding(params["charset"]) // Lowercasing is done inside
// Encoding sometimes returns nil, see #3 on this repo and golang/go#19421
return err == nil && enc != nil
}
// MakePage creates a formatted, rendered Page from the given network response and params.
// You must set the Page.Width value yourself.
func MakePage(url string, res *gemini.Response, width, leftMargin int, proxied bool) (*structs.Page, error) {
2020-06-20 00:09:01 -04:00
if !CanDisplay(res) {
return nil, ErrCantDisplay
2020-06-20 00:09:01 -04:00
}
buf := new(bytes.Buffer)
2020-07-26 12:12:54 -04:00
_, err := io.CopyN(buf, res.Body, viper.GetInt64("a-general.page_max_size")+1)
if err == nil {
2020-07-26 12:12:54 -04:00
// Content was larger than max size
return nil, ErrTooLarge
} else if err != io.EOF {
2020-11-23 21:26:44 -05:00
if os.IsTimeout(err) {
// I would use
// errors.Is(err, os.ErrDeadlineExceeded)
// but that isn't supported before Go 1.15.
return nil, ErrTimedOut
}
// Some other error
2020-06-20 00:09:01 -04:00
return nil, err
}
// Otherwise, the error is EOF, which is what we want.
2020-06-20 00:09:01 -04:00
mediatype, params, _ := mime.ParseMediaType(res.Meta)
// Convert content first
var utfText string
if isUTF8(params["charset"]) {
2020-08-05 13:31:59 -04:00
utfText = buf.String()
2020-06-20 00:09:01 -04:00
} else {
encoding, err := ianaindex.MIME.Encoding(params["charset"])
if encoding == nil || err != nil {
// Some encoding doesn't exist and wasn't caught in CanDisplay()
return nil, ErrBadEncoding
2020-06-20 00:09:01 -04:00
}
2020-08-05 13:31:59 -04:00
utfText, err = encoding.NewDecoder().String(buf.String())
2020-06-20 00:09:01 -04:00
if err != nil {
return nil, err
}
}
if mediatype == "text/gemini" {
rendered, links := RenderGemini(utfText, width, leftMargin, proxied)
2020-06-20 00:09:01 -04:00
return &structs.Page{
Mediatype: structs.TextGemini,
RawMediatype: mediatype,
URL: url,
Raw: utfText,
Content: rendered,
Links: links,
2020-12-20 15:54:47 -05:00
MadeAt: time.Now(),
2020-06-20 00:09:01 -04:00
}, nil
} else if strings.HasPrefix(mediatype, "text/") {
2020-07-28 16:58:32 -04:00
if mediatype == "text/x-ansi" || strings.HasSuffix(url, ".ans") || strings.HasSuffix(url, ".ansi") {
2020-07-10 17:45:14 -04:00
// ANSI
return &structs.Page{
Mediatype: structs.TextAnsi,
RawMediatype: mediatype,
URL: url,
Raw: utfText,
Content: RenderANSI(utfText, leftMargin),
Links: []string{},
2020-12-20 15:54:47 -05:00
MadeAt: time.Now(),
2020-07-10 17:45:14 -04:00
}, nil
}
// Treated as plaintext
return &structs.Page{
Mediatype: structs.TextPlain,
RawMediatype: mediatype,
URL: url,
Raw: utfText,
Content: RenderPlainText(utfText, leftMargin),
Links: []string{},
2020-12-20 15:54:47 -05:00
MadeAt: time.Now(),
}, nil
2020-06-20 00:09:01 -04:00
}
2020-08-27 17:57:19 -04:00
return nil, ErrBadMediatype
2020-06-20 00:09:01 -04:00
}