gzipped icon indicating copy to clipboard operation
gzipped copied to clipboard

Use with `embed.FS`

Open muety opened this issue 4 years ago • 3 comments

Awesome project, exactly what I was looking for! Would be cool, though, if you could pass an embed.FS (or even better, just any fs.FS) instead of a directory name to serve files from. What do you think?

muety avatar Dec 20 '21 14:12 muety

Stdlib now has an http.FS adaptor to convert an fs.FS into an http.FileSystem, but unfortunately fs.FS doesn't support the file existence check gzipped.Dir does. There's fs.StatFS which adds Stat to the FS interface, but unfortunately embed.FS doesn't support that.

For embed.FS it should be pretty easy to write an adaptor which implements Exists(filename). For fs.FS in general, it's trickier, because the interface only has Open. Supporting fs.StatFS probably makes sense though.

lpar avatar Dec 20 '21 20:12 lpar

I think it should be quite easy to come up with a wrapper around any fs.FS that uses fs.Stat() for the existence check. From my understanding, that static function takes any fs.FS as an input, calls Stat() on it if it actually implements fs.StatFS and otherwise falls back to the (slower) way of Open()ing the file to get stat. But I might have gotten something wrong.

muety avatar Dec 20 '21 21:12 muety

Aha, I didn't know about fs.Stat(). That makes things much easier. Yes, in that case a general purpose wrapper should be fairly easy.

I'm thinking that rather than change the API again, it might be better to add an alternative to gzipped.FileServer that takes any fs.FS and uses fs.Stat() internally. Subtree selection would still be possible using fs.Sub.

lpar avatar Dec 20 '21 21:12 lpar