aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <m.shulhan@gmail.com>2019-09-26 23:43:41 +0700
committerShulhan <m.shulhan@gmail.com>2019-09-27 00:24:49 +0700
commitf11baff970aef80d39e39397b0569c47e23ad5da (patch)
treeaee2cbce1235b10a717ea1777e1e31402298e8c4
parented84f4d36ea43a248398d692f7f2b5c9e6b34517 (diff)
downloadpakakeh.go-f11baff970aef80d39e39397b0569c47e23ad5da.tar.xz
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.
-rw-r--r--CHANGELOG.adoc2
-rw-r--r--CHANGELOG.html3
-rw-r--r--lib/memfs/memfs.go2
-rw-r--r--lib/memfs/node.go18
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.</p>
<li>
<p>http: add content and response type HTML and XML</p>
</li>
+<li>
+<p>memfs: export the Decode method on Node</p>
+</li>
</ul>
</div>
</div>
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
}
//