diff options
| author | Shulhan <ms@kilabit.info> | 2021-12-19 01:33:12 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2021-12-19 02:13:57 +0700 |
| commit | ba9c2ea6bd2a3c39f20fe7b2cc702e7fd4c99c44 (patch) | |
| tree | b7f3db4fd854c186ed15da8dff7c8e8961b5ecc5 /lib/memfs/node.go | |
| parent | 9b8841034d7f4b6f92078f6dc682bd010fb37e72 (diff) | |
| download | pakakeh.go-ba9c2ea6bd2a3c39f20fe7b2cc702e7fd4c99c44.tar.xz | |
lib/memfs: remove field ContentEncoding from EmbedOptions and Node
The original idea for option ContentEncoding in EmbedOptions and Node
is to save spaces, compressing the content on disk on embedding and
doing transport, when the MemFS instance is used to serve the (embedded)
contents of file system.
This option turns out break the HTTP content negotiation [1] of
accept-encoding header, if the HTTP server does not handle it properly,
which default Go HTTP server does not.
In order to prevent this issue in the future, for anyone who use the
memfs for serving static HTTP contents, we remove the options and store
the embedded content as is and let the HTTP server handle how the
compression by itself.
[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation
Diffstat (limited to 'lib/memfs/node.go')
| -rw-r--r-- | lib/memfs/node.go | 92 |
1 files changed, 8 insertions, 84 deletions
diff --git a/lib/memfs/node.go b/lib/memfs/node.go index f134f814..4311102d 100644 --- a/lib/memfs/node.go +++ b/lib/memfs/node.go @@ -6,7 +6,6 @@ package memfs import ( "bytes" - "compress/gzip" "encoding/base64" "errors" "fmt" @@ -35,14 +34,13 @@ type Node struct { os.FileInfo http.File - SysPath string // The original file path in system. - Path string // Absolute file path in memory. - Content []byte // Content of file. - ContentType string // File type per MIME, for example "application/json". - ContentEncoding string // File type encoding, for example "gzip". - Parent *Node // Pointer to parent directory. - Childs []*Node // List of files in directory. - GenFuncName string // The function name for embedded Go code. + SysPath string // The original file path in system. + Path string // Absolute file path in memory. + Content []byte // Content of file. + ContentType string // File type per MIME, for example "application/json". + Parent *Node // Pointer to parent directory. + Childs []*Node // List of files in directory. + GenFuncName string // The function name for embedded Go code. name string // File name. modTime time.Time // ModTime contains file modification time. @@ -145,77 +143,6 @@ func (node *Node) Close() error { return nil } -// -// Decode the contents of node (for example, uncompress with gzip) and return -// it. -// -func (node *Node) Decode() ([]byte, error) { - if len(node.ContentEncoding) == 0 { - node.plainv = node.Content - return node.plainv, nil - } - - logp := "Decode" - node.plainv = node.plainv[:0] - - if node.ContentEncoding == EncodingGzip { - r, err := gzip.NewReader(bytes.NewReader(node.Content)) - if err != nil { - return nil, fmt.Errorf("%s: %w", logp, err) - } - - buf := make([]byte, 1024) - for { - n, err := r.Read(buf) - if n > 0 { - node.plainv = append(node.plainv, buf[:n]...) - } - if err != nil { - if err == io.EOF { - break - } - return nil, fmt.Errorf("%s: %w", logp, err) - } - buf = buf[0:] - } - } - - return node.plainv, nil -} - -// -// Encode compress and set the content of Node. -// -func (node *Node) Encode(content []byte) (err error) { - logp := "Encode" - - node.plainv = content - node.lowerv = bytes.ToLower(content) - - switch node.ContentEncoding { - case EncodingGzip: - var buf bytes.Buffer - gz := gzip.NewWriter(&buf) - - _, err = gz.Write(content) - if err != nil { - _ = gz.Close() - return fmt.Errorf("%s: %w", logp, err) - } - - err = gz.Close() - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } - - node.Content = libbytes.Copy(buf.Bytes()) - - default: - node.Content = content - } - return nil -} - func (node *Node) IsDir() bool { return node.mode.IsDir() } @@ -319,11 +246,8 @@ func (node *Node) Save(content []byte) (err error) { if err != nil { return fmt.Errorf("%s: %w", logp, err) } - err = node.Encode(content) - if err != nil { - return fmt.Errorf("%s: %w", logp, err) - } + node.Content = content node.modTime = time.Now() node.size = int64(len(content)) return nil |
