working compression support (yatwiki2-compatible)

This commit is contained in:
mappu 2017-07-09 12:13:43 +12:00
parent 8f2cb1e1d5
commit e67257f95c

42
DB.go
View File

@ -1,10 +1,13 @@
package yatwiki3 package yatwiki3
import ( import (
"bytes"
"compress/flate"
"crypto/md5" "crypto/md5"
"database/sql" "database/sql"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -121,23 +124,54 @@ func (this *WikiDB) ListTitles() ([]string, error) {
return ret, nil return ret, nil
} }
func (this *WikiDB) parseArticle(row *sql.Row) (*Article, error) { func (this *WikiDB) gzinflate(gzBody []byte) ([]byte, error) {
a := Article{} gzBodyReader := bytes.NewReader(gzBody)
err := row.Scan(&a.ID, &a.TitleID, &a.Modified, &a.Body, &a.Author)
gzReader := flate.NewReader(gzBodyReader)
defer gzReader.Close()
buffer := bytes.Buffer{}
_, err := io.Copy(&buffer, gzReader)
if err != nil { if err != nil {
return nil, err 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 return &a, nil
} }
func (this *WikiDB) parseArticleWithTitle(row *sql.Row) (*ArticleWithTitle, error) { func (this *WikiDB) parseArticleWithTitle(row *sql.Row) (*ArticleWithTitle, error) {
a := ArticleWithTitle{} 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 { if err != nil {
return nil, err return nil, err
} }
decompressed, err := this.gzinflate(gzBody)
if err != nil {
return nil, err
}
a.Body = decompressed
return &a, nil return &a, nil
} }