From f11baff970aef80d39e39397b0569c47e23ad5da Mon Sep 17 00:00:00 2001
From: Shulhan
Date: Thu, 26 Sep 2019 23:43:41 +0700
Subject: memfs: export the Decode method and return the decoded content
Also, fix the buffer that is not being reset while reading and decoding
the content.
---
CHANGELOG.adoc | 2 ++
CHANGELOG.html | 3 +++
lib/memfs/memfs.go | 2 +-
lib/memfs/node.go | 18 +++++++++++-------
4 files changed, 17 insertions(+), 8 deletions(-)
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 5dc9ef25..c5a34b05 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -65,6 +65,8 @@ to Search the content.
* http: add content and response type HTML and XML
+* memfs: export the Decode method on Node
+
== share v0.8.2 (2019-09-05)
diff --git a/CHANGELOG.html b/CHANGELOG.html
index b78dd110..6c14f8b7 100644
--- a/CHANGELOG.html
+++ b/CHANGELOG.html
@@ -205,6 +205,9 @@ to Search the content.
http: add content and response type HTML and XML
+
+memfs: export the Decode method on Node
+
diff --git a/lib/memfs/memfs.go b/lib/memfs/memfs.go
index 8ab6f8e2..68d100a3 100644
--- a/lib/memfs/memfs.go
+++ b/lib/memfs/memfs.go
@@ -511,7 +511,7 @@ func (mfs *MemFS) Search(words []string, snippetLen int) (results []SearchResult
}
if len(node.lowerv) == 0 {
- err := node.decode()
+ _, err := node.Decode()
if err != nil {
log.Printf("memfs.Search: " + err.Error())
continue
diff --git a/lib/memfs/node.go b/lib/memfs/node.go
index c461481f..1b9f20b2 100644
--- a/lib/memfs/node.go
+++ b/lib/memfs/node.go
@@ -90,10 +90,14 @@ func NewNode(parent *Node, fi os.FileInfo, withContent bool) (node *Node, err er
return node, nil
}
-func (leaf *Node) decode() (err error) {
+//
+// Decode the contents of node (for example, uncompress with gzip) and return
+// it.
+//
+func (leaf *Node) Decode() ([]byte, error) {
if len(leaf.ContentEncoding) == 0 {
leaf.plainv = leaf.V
- return nil
+ return leaf.plainv, nil
}
leaf.plainv = leaf.plainv[:0]
@@ -101,26 +105,26 @@ func (leaf *Node) decode() (err error) {
if leaf.ContentEncoding == EncodingGzip {
r, err := gzip.NewReader(bytes.NewReader(leaf.V))
if err != nil {
- return err
+ return nil, err
}
buf := make([]byte, 1024)
-
for {
n, err := r.Read(buf)
if n > 0 {
- leaf.plainv = append(leaf.plainv, buf...)
+ leaf.plainv = append(leaf.plainv, buf[:n]...)
}
if err != nil {
if err == io.EOF {
break
}
- return err
+ return nil, err
}
+ buf = buf[0:]
}
}
- return nil
+ return leaf.plainv, nil
}
//
--
cgit v1.3