-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve filesystem support (Go 1.16+). Add field echo.Filesystem, met…
…hods: echo.FileFS, echo.StaticFS, group.FileFS, group.StaticFS. Following methods will use echo.Filesystem to server files: echo.File, echo.Static, group.File, group.Static, Context.File
- Loading branch information
Showing
15 changed files
with
819 additions
and
97 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,33 @@ | ||
//go:build !go1.16 | ||
// +build !go1.16 | ||
|
||
package echo | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func (c *context) File(file string) (err error) { | ||
f, err := os.Open(file) | ||
if err != nil { | ||
return NotFoundHandler(c) | ||
} | ||
defer f.Close() | ||
|
||
fi, _ := f.Stat() | ||
if fi.IsDir() { | ||
file = filepath.Join(file, indexPage) | ||
f, err = os.Open(file) | ||
if err != nil { | ||
return NotFoundHandler(c) | ||
} | ||
defer f.Close() | ||
if fi, err = f.Stat(); err != nil { | ||
return | ||
} | ||
} | ||
http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), f) | ||
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,47 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
package echo | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"io/fs" | ||
"net/http" | ||
"path/filepath" | ||
) | ||
|
||
func (c *context) File(file string) error { | ||
return fsFile(c, file, c.echo.Filesystem) | ||
} | ||
|
||
func (c *context) FileFS(file string, filesystem fs.FS) error { | ||
return fsFile(c, file, filesystem) | ||
} | ||
|
||
func fsFile(c Context, file string, filesystem fs.FS) error { | ||
f, err := filesystem.Open(file) | ||
if err != nil { | ||
return ErrNotFound | ||
} | ||
defer f.Close() | ||
|
||
fi, _ := f.Stat() | ||
if fi.IsDir() { | ||
file = filepath.Join(file, indexPage) | ||
f, err = filesystem.Open(file) | ||
if err != nil { | ||
return ErrNotFound | ||
} | ||
defer f.Close() | ||
if fi, err = f.Stat(); err != nil { | ||
return err | ||
} | ||
} | ||
ff, ok := f.(io.ReadSeeker) | ||
if !ok { | ||
return errors.New("file does not implement io.ReadSeeker") | ||
} | ||
http.ServeContent(c.Response(), c.Request(), fi.Name(), fi.ModTime(), ff) | ||
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,135 @@ | ||
//go:build go1.16 | ||
// +build go1.16 | ||
|
||
package echo | ||
|
||
import ( | ||
testify "github.com/stretchr/testify/assert" | ||
"io/fs" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestContext_File(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
whenFile string | ||
whenFS fs.FS | ||
expectStatus int | ||
expectStartsWith []byte | ||
expectError string | ||
}{ | ||
{ | ||
name: "ok, from default file system", | ||
whenFile: "_fixture/images/walle.png", | ||
whenFS: nil, | ||
expectStatus: http.StatusOK, | ||
expectStartsWith: []byte{0x89, 0x50, 0x4e}, | ||
}, | ||
{ | ||
name: "ok, from custom file system", | ||
whenFile: "walle.png", | ||
whenFS: os.DirFS("_fixture/images"), | ||
expectStatus: http.StatusOK, | ||
expectStartsWith: []byte{0x89, 0x50, 0x4e}, | ||
}, | ||
{ | ||
name: "nok, not existent file", | ||
whenFile: "not.png", | ||
whenFS: os.DirFS("_fixture/images"), | ||
expectStatus: http.StatusOK, | ||
expectStartsWith: nil, | ||
expectError: "code=404, message=Not Found", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
e := New() | ||
if tc.whenFS != nil { | ||
e.Filesystem = tc.whenFS | ||
} | ||
|
||
handler := func(ec Context) error { | ||
return ec.(*context).File(tc.whenFile) | ||
} | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/match.png", nil) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
|
||
err := handler(c) | ||
|
||
testify.Equal(t, tc.expectStatus, rec.Code) | ||
if tc.expectError != "" { | ||
testify.EqualError(t, err, tc.expectError) | ||
} else { | ||
testify.NoError(t, err) | ||
} | ||
|
||
body := rec.Body.Bytes() | ||
if len(body) > len(tc.expectStartsWith) { | ||
body = body[:len(tc.expectStartsWith)] | ||
} | ||
testify.Equal(t, tc.expectStartsWith, body) | ||
}) | ||
} | ||
} | ||
|
||
func TestContext_FileFS(t *testing.T) { | ||
var testCases = []struct { | ||
name string | ||
whenFile string | ||
whenFS fs.FS | ||
expectStatus int | ||
expectStartsWith []byte | ||
expectError string | ||
}{ | ||
{ | ||
name: "ok", | ||
whenFile: "walle.png", | ||
whenFS: os.DirFS("_fixture/images"), | ||
expectStatus: http.StatusOK, | ||
expectStartsWith: []byte{0x89, 0x50, 0x4e}, | ||
}, | ||
{ | ||
name: "nok, not existent file", | ||
whenFile: "not.png", | ||
whenFS: os.DirFS("_fixture/images"), | ||
expectStatus: http.StatusOK, | ||
expectStartsWith: nil, | ||
expectError: "code=404, message=Not Found", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
e := New() | ||
|
||
handler := func(ec Context) error { | ||
return ec.(*context).FileFS(tc.whenFile, tc.whenFS) | ||
} | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/match.png", nil) | ||
rec := httptest.NewRecorder() | ||
c := e.NewContext(req, rec) | ||
|
||
err := handler(c) | ||
|
||
testify.Equal(t, tc.expectStatus, rec.Code) | ||
if tc.expectError != "" { | ||
testify.EqualError(t, err, tc.expectError) | ||
} else { | ||
testify.NoError(t, err) | ||
} | ||
|
||
body := rec.Body.Bytes() | ||
if len(body) > len(tc.expectStartsWith) { | ||
body = body[:len(tc.expectStartsWith)] | ||
} | ||
testify.Equal(t, tc.expectStartsWith, body) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.