all repos — homestead @ main

Code for my website

internal/storage/file.go (view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package storage

import (
	"io"
	"strings"
	"time"

	"github.com/Southclaws/fault"
	"github.com/Southclaws/fault/fmsg"
	"go.alanpearce.eu/homestead/internal/buffer"
	"go.alanpearce.eu/homestead/internal/builder/template"
)

type File struct {
	Path         string
	ContentType  string
	LastModified time.Time
	Etag         string
	Title        string
	StyleHash    string
	Encodings    map[string]*buffer.Buffer
}

func (f *File) AvailableEncodings() []string {
	encs := make([]string, 0, len(f.Encodings))
	for enc := range f.Encodings {
		encs = append(encs, enc)
	}

	return encs
}

func (f *File) CalculateStyleHash() (err error) {
	buf := f.Encodings["identity"]
	if buf == nil {
		return fault.New("buffer not initialised")
	}

	if _, err := buf.Seek(0, io.SeekStart); err != nil {
		return fault.Wrap(err, fmsg.With("could not seek buffer"))
	}

	mime, _, _ := strings.Cut(f.ContentType, ";")
	switch mime {
	case "text/html":
		f.StyleHash, err = template.GetHTMLStyleHash(buf)
		if err != nil {
			return fault.Wrap(err, fmsg.With("could not calculate HTML style hash"))
		}
	default:
		return
	}
	if _, err := buf.Seek(0, io.SeekStart); err != nil {
		return fault.Wrap(err, fmsg.With("could not seek buffer"))
	}

	return
}