From e67257f95c350eb5259af63cdda5a427ca012019 Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 9 Jul 2017 12:13:43 +1200 Subject: [PATCH] working compression support (yatwiki2-compatible) --- DB.go | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/DB.go b/DB.go index 1b7fe79..85cbf9e 100644 --- a/DB.go +++ b/DB.go @@ -1,10 +1,13 @@ package yatwiki3 import ( + "bytes" + "compress/flate" "crypto/md5" "database/sql" "encoding/hex" "fmt" + "io" "net/http" "time" @@ -121,23 +124,54 @@ func (this *WikiDB) ListTitles() ([]string, error) { return ret, nil } -func (this *WikiDB) parseArticle(row *sql.Row) (*Article, error) { - a := Article{} - err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &a.Body, &a.Author) +func (this *WikiDB) gzinflate(gzBody []byte) ([]byte, error) { + gzBodyReader := bytes.NewReader(gzBody) + + gzReader := flate.NewReader(gzBodyReader) + defer gzReader.Close() + + buffer := bytes.Buffer{} + _, err := io.Copy(&buffer, gzReader) if err != nil { return nil, err } + return buffer.Bytes(), nil +} + +func (this *WikiDB) parseArticle(row *sql.Row) (*Article, error) { + a := Article{} + var gzBody []byte + err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &gzBody, &a.Author) + if err != nil { + return nil, err + } + + decompressed, err := this.gzinflate(gzBody) + if err != nil { + return nil, err + } + + a.Body = decompressed + return &a, nil } func (this *WikiDB) parseArticleWithTitle(row *sql.Row) (*ArticleWithTitle, error) { a := ArticleWithTitle{} - err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &a.Body, &a.Author, &a.Title) + var gzBody []byte + err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &gzBody, &a.Author, &a.Title) if err != nil { return nil, err } + decompressed, err := this.gzinflate(gzBody) + if err != nil { + return nil, err + } + + a.Body = decompressed + return &a, nil }