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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package sqlite
import (
"database/sql"
"strings"
"time"
"gitlab.com/tozd/go/errors"
"go.alanpearce.eu/website/internal/buffer"
"go.alanpearce.eu/website/internal/storage"
"go.alanpearce.eu/x/log"
)
type Reader struct {
db *sql.DB
log *log.Logger
queries struct {
getFile *sql.Stmt
}
}
func NewReader(db *sql.DB, log *log.Logger) (r *Reader, err error) {
r = &Reader{
log: log,
db: db,
}
r.queries.getFile, err = r.db.Prepare(`
SELECT
file.content_type,
file.last_modified,
file.etag,
content.body
FROM url
INNER JOIN file
ON file.url_id = url.id
INNER JOIN content
ON content.file_id = file.id
WHERE
url.path = ?
AND
content.encoding = 'identity'
`)
if err != nil {
return nil, errors.WithMessage(err, "preparing select statement")
}
return r, nil
}
func (r *Reader) GetFile(filename string) (*storage.File, error) {
file := &storage.File{
Encodings: make(map[string]*buffer.Buffer, 1),
}
content := []byte{}
var unixTime int64
err := r.queries.getFile.QueryRow(filename).Scan(
&file.ContentType,
&unixTime,
&file.Etag,
&content,
)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, errors.WithMessage(err, "querying database")
}
file.LastModified = time.Unix(unixTime, 0)
file.Encodings["identity"] = buffer.NewBuffer(content)
return file, nil
}
// TODO write specialised query
func (r *Reader) CanonicalisePath(path string) (cPath string, differs bool) {
cPath = path
if strings.HasSuffix(path, "/index.html") {
cPath, differs = strings.CutSuffix(path, "index.html")
} else if !strings.HasSuffix(path, "/") {
file, err := r.GetFile(path + "/")
if err != nil {
r.log.Warn("error canonicalising path", "path", path, "error", err)
return
}
if file != nil {
cPath, differs = path+"/", true
}
}
return
}
|