mirror of
https://github.com/Bornholm/formidable.git
synced 2024-12-30 18:49:33 +01:00
32 lines
560 B
Go
32 lines
560 B
Go
|
package file
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net/url"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
const SchemeFile = "file"
|
||
|
|
||
|
type LoaderHandler struct{}
|
||
|
|
||
|
func (h *LoaderHandler) Match(url *url.URL) bool {
|
||
|
return url.Scheme == SchemeFile || url.Scheme == ""
|
||
|
}
|
||
|
|
||
|
func (h *LoaderHandler) Open(url *url.URL) (io.ReadCloser, error) {
|
||
|
file, err := os.Open(filepath.Join(url.Host, url.Path))
|
||
|
if err != nil {
|
||
|
return nil, errors.Wrapf(err, "could not open file '%s'", url.Path)
|
||
|
}
|
||
|
|
||
|
return file, nil
|
||
|
}
|
||
|
|
||
|
func NewLoaderHandler() *LoaderHandler {
|
||
|
return &LoaderHandler{}
|
||
|
}
|