-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package staticserve | ||
|
||
import ( | ||
"io" | ||
"io/fs" | ||
"path/filepath" | ||
) | ||
|
||
// NewFS reads the file at fpath from fsys and then calls New with | ||
// the filename part of fpath. | ||
func NewFS(fsys fs.FS, fpath string) (ss *StaticServe, err error) { | ||
var f fs.File | ||
if f, err = fsys.Open(fpath); err == nil { | ||
defer f.Close() | ||
var b []byte | ||
if b, err = io.ReadAll(f); err == nil { | ||
ss, err = New(filepath.Base(fpath), b) | ||
} | ||
} | ||
return | ||
} |
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,22 @@ | ||
package staticserve | ||
|
||
import ( | ||
"embed" | ||
"testing" | ||
) | ||
|
||
//go:embed assets | ||
var assetsFS embed.FS | ||
|
||
func TestNewFS(t *testing.T) { | ||
ss, err := NewFS(assetsFS, "assets/subdir/test.txt") | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
if ss.ContentType != "text/plain; charset=utf-8" { | ||
t.Error(ss.ContentType) | ||
} | ||
if ss.Name != "test.u9cvw0b8o4xe.txt" { | ||
t.Error(ss.Name) | ||
} | ||
} |
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