all repos — homestead @ cb0c1a6f85c4577b242228a7310d137bf461b30f

Code for my website

move style hashing to file layer

Alan Pearce
commit

cb0c1a6f85c4577b242228a7310d137bf461b30f

parent

0ee23f01ab6304fba37c1d2b66b16de9792e285c

1 file changed, 20 insertions(+), 7 deletions(-)

changed files
M internal/storage/sqlite/writer.gointernal/storage/sqlite/writer.go
@@ -66,6 +66,7 @@ url_id INTEGER NOT NULL,
content_type TEXT NOT NULL, last_modified INTEGER NOT NULL, etag TEXT NOT NULL, + style_hash TEXT NOT NULL, FOREIGN KEY (url_id) REFERENCES url (url_id) ); CREATE UNIQUE INDEX IF NOT EXISTS file_url_content_type
@@ -97,8 +98,8 @@ return nil, errors.WithMessage(err, "preparing insert URL statement")
} w.queries.insertFile, err = db.Prepare(` - INSERT INTO file (url_id, content_type, last_modified, etag) - VALUES (:url_id, :content_type, :last_modified, :etag) + INSERT INTO file (url_id, content_type, last_modified, etag, style_hash) + VALUES (:url_id, :content_type, :last_modified, :etag, :style_hash) `) if err != nil { return nil, errors.WithMessage(err, "preparing insert file statement")
@@ -134,6 +135,7 @@ sql.Named("url_id", urlID),
sql.Named("content_type", file.ContentType), sql.Named("last_modified", file.LastModified.Unix()), sql.Named("etag", file.Etag), + sql.Named("style_hash", file.StyleHash), ) if err != nil { return 0, errors.WithMessage(err, "inserting file into database")
@@ -184,9 +186,10 @@ Path: post.URL,
ContentType: contentType(post.URL), LastModified: post.Date, Etag: etag, + Encodings: map[string]*buffer.Buffer{}, } - return s.write(file, content) + return s.WriteFile(file, content) } func (s *Writer) Write(pathname string, content *buffer.Buffer) error {
@@ -202,26 +205,36 @@ Path: pathname,
ContentType: contentType(pathname), LastModified: time.Now(), Etag: etag, + Encodings: map[string]*buffer.Buffer{}, } - return s.write(file, content) + return s.WriteFile(file, content) } -func (s *Writer) write(file *storage.File, content *buffer.Buffer) error { +func (s *Writer) WriteFile(file *storage.File, content *buffer.Buffer) error { s.log.Debug("storing content", "pathname", file.Path) - bytes := content.Bytes() urlID, err := s.storeURL(file.Path) if err != nil { return errors.WithMessage(err, "storing URL") } + if file.Encodings == nil { + file.Encodings = map[string]*buffer.Buffer{} + } + file.Encodings["identity"] = content + + err = file.CalculateStyleHash() + if err != nil { + return errors.WithMessage(err, "calculating file hash") + } + fileID, err := s.storeFile(urlID, file) if err != nil { return errors.WithMessage(err, "storing file") } - err = s.storeEncoding(fileID, "identity", bytes) + err = s.storeEncoding(fileID, "identity", content.Bytes()) if err != nil { return err }