-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assets: add file system layer for zipped embed assets
This commits adds an optional decompression layer to a embed.FS instance. If a file can't be found during Open, the filename plus a .gz ending is tried. If this succeeds, the content is decompressed and served. Signed-off-by: Jan Fajerski <[email protected]>
- Loading branch information
1 parent
ce7006e
commit 1871a70
Showing
9 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Copyright 2018 The Prometheus Authors | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
include ../Makefile.common | ||
|
||
.PHONY: test | ||
@echo ">> Running assets tests" | ||
test:: deps check_license unused common-test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2021 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package assets | ||
|
||
import ( | ||
"compress/gzip" | ||
"embed" | ||
"io" | ||
"io/fs" | ||
"time" | ||
) | ||
|
||
const ( | ||
gzipSuffix = ".gz" | ||
) | ||
|
||
type FileSystem struct { | ||
embed embed.FS | ||
} | ||
|
||
func New(fs embed.FS) FileSystem { | ||
return FileSystem{fs} | ||
} | ||
|
||
// Open implements the fs.FS interface. | ||
func (compressed FileSystem) Open(path string) (fs.File, error) { | ||
// If we have the file in our embed FS, just return that as it could be a dir. | ||
var f fs.File | ||
if f, err := compressed.embed.Open(path); err == nil { | ||
return f, nil | ||
} | ||
|
||
f, err := compressed.embed.Open(path + gzipSuffix) | ||
if err != nil { | ||
return f, err | ||
} | ||
// Read the decompressed content into a buffer. | ||
gr, err := gzip.NewReader(f) | ||
if err != nil { | ||
return f, err | ||
} | ||
defer gr.Close() | ||
|
||
c, err := io.ReadAll(gr) | ||
if err != nil { | ||
return f, err | ||
} | ||
// Wrap everything in our custom File. | ||
return &File{file: f, content: c}, nil | ||
} | ||
|
||
type File struct { | ||
// The underlying file. | ||
file fs.File | ||
// The decrompressed content, needed to return an accurate size. | ||
content []byte | ||
// Offset for calls to Read(). | ||
offset int | ||
} | ||
|
||
// Stat implements the fs.File interface. | ||
func (f File) Stat() (fs.FileInfo, error) { | ||
stat, err := f.file.Stat() | ||
if err != nil { | ||
return stat, err | ||
} | ||
return FileInfo{stat, int64(len(f.content))}, nil | ||
} | ||
|
||
// Read implements the fs.File interface. | ||
func (f *File) Read(buf []byte) (int, error) { | ||
if len(buf) > len(f.content)-f.offset { | ||
buf = buf[0:len(f.content[f.offset:])] | ||
} | ||
n := copy(buf, f.content[f.offset:]) | ||
if n == len(f.content)-f.offset { | ||
return n, io.EOF | ||
} | ||
f.offset += n | ||
return n, nil | ||
} | ||
|
||
// Close implements the fs.File interface. | ||
func (f File) Close() error { | ||
return f.file.Close() | ||
} | ||
|
||
type FileInfo struct { | ||
fi fs.FileInfo | ||
actualSize int64 | ||
} | ||
|
||
// Name implements the fs.FileInfo interface. | ||
func (fi FileInfo) Name() string { | ||
name := fi.fi.Name() | ||
return name[:len(name)-len(gzipSuffix)] | ||
} | ||
|
||
// Size implements the fs.FileInfo interface. | ||
func (fi FileInfo) Size() int64 { return fi.actualSize } | ||
|
||
// Mode implements the fs.FileInfo interface. | ||
func (fi FileInfo) Mode() fs.FileMode { return fi.fi.Mode() } | ||
|
||
// ModTime implements the fs.FileInfo interface. | ||
func (fi FileInfo) ModTime() time.Time { return fi.fi.ModTime() } | ||
|
||
// IsDir implements the fs.FileInfo interface. | ||
func (fi FileInfo) IsDir() bool { return fi.fi.IsDir() } | ||
|
||
// Sys implements the fs.FileInfo interface. | ||
func (fi FileInfo) Sys() interface{} { return nil } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2021 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package assets | ||
|
||
import ( | ||
"embed" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
//go:embed testdata | ||
var EmbedFS embed.FS | ||
|
||
var testFS = New(EmbedFS) | ||
|
||
func TestFS(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
path string | ||
expectedSize int64 | ||
expectedContent string | ||
}{ | ||
{ | ||
name: "uncompressed file", | ||
path: "testdata/uncompressed", | ||
expectedSize: 4, | ||
expectedContent: "foo\n", | ||
}, | ||
{ | ||
name: "compressed file", | ||
path: "testdata/compressed", | ||
expectedSize: 4, | ||
expectedContent: "foo\n", | ||
}, | ||
{ | ||
name: "both, open uncompressed", | ||
path: "testdata/both", | ||
expectedSize: 4, | ||
expectedContent: "foo\n", | ||
}, | ||
{ | ||
name: "both, open compressed", | ||
path: "testdata/both.gz", | ||
expectedSize: 29, | ||
// we don't check content for a explicitly compressed file | ||
expectedContent: "", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
f, err := testFS.Open(c.path) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
stat, err := f.Stat() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
size := stat.Size() | ||
if size != c.expectedSize { | ||
t.Fatalf("size is wrong, expected %d, got %d", c.expectedSize, size) | ||
} | ||
|
||
if strings.HasSuffix(c.path, ".gz") { | ||
// don't read the comressed content | ||
return | ||
} | ||
|
||
content, err := ioutil.ReadAll(f) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(content) != c.expectedContent { | ||
t.Fatalf("content is wrong, expected %s, got %s", c.expectedContent, string(content)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/prometheus/common/assets | ||
|
||
go 1.17 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
foo |