Open implements the fs.FS interface.
(path string)
| 35 | |
| 36 | // Open implements the fs.FS interface. |
| 37 | func (compressed FileSystem) Open(path string) (fs.File, error) { |
| 38 | // If we have the file in our embed FS, just return that as it could be a dir. |
| 39 | var f fs.File |
| 40 | if f, err := compressed.embed.Open(path); err == nil { |
| 41 | return f, nil |
| 42 | } |
| 43 | |
| 44 | f, err := compressed.embed.Open(path + gzipSuffix) |
| 45 | if err != nil { |
| 46 | return f, err |
| 47 | } |
| 48 | // Read the decompressed content into a buffer. |
| 49 | gr, err := gzip.NewReader(f) |
| 50 | if err != nil { |
| 51 | return f, err |
| 52 | } |
| 53 | defer gr.Close() |
| 54 | |
| 55 | c, err := io.ReadAll(gr) |
| 56 | if err != nil { |
| 57 | return f, err |
| 58 | } |
| 59 | // Wrap everything in our custom File. |
| 60 | return &File{file: f, content: c}, nil |
| 61 | } |
| 62 | |
| 63 | type File struct { |
| 64 | // The underlying file. |