gophertype/pkg/responses/filesystem.go

87 lines
2.1 KiB
Go
Raw Normal View History

2019-11-26 03:55:28 +00:00
package responses
import (
"errors"
"fmt"
2019-11-26 03:55:28 +00:00
"io/ioutil"
"os"
"path/filepath"
2019-11-26 03:55:28 +00:00
"strings"
"git.kirsle.net/apps/gophertype/pkg/bundled"
"git.kirsle.net/apps/gophertype/pkg/settings"
2019-11-26 03:55:28 +00:00
)
// GetFile returns the template file's data, wherever it is.
// Checks the embedded bindata, then the user root on disk, then error.
// If it can be found, returns the contents or error.
func GetFile(path string) ([]byte, error) {
// Check the user root first.
if b, err := ioutil.ReadFile(filepath.Join(settings.UserRoot, path)); err == nil {
2019-11-26 03:55:28 +00:00
return b, nil
}
// Fall back on embedded bindata.
if b, err := bundled.Asset(path); err == nil {
2019-11-26 03:55:28 +00:00
return b, nil
}
return []byte{}, fmt.Errorf("GetFile(%s): not found in user root or bindata", path)
2019-11-26 03:55:28 +00:00
}
// GetFileExists checks if the file exists but doesn't return its data.
func GetFileExists(path string) bool {
// Check bindata.
if _, err := bundled.Asset(path); err == nil {
2019-11-26 03:55:28 +00:00
return true
}
// Check the user root.
if stat, err := os.Stat(filepath.Join(settings.UserRoot, path)); err == nil && !stat.IsDir() {
2019-11-26 03:55:28 +00:00
return true
}
return false
}
/*
ResolveFile searches for the existence of a file from a fuzzy URL path.
`path` is a request path like "/about"
This function would return e.g. "about.gohtml" as being a file path that is
sure to return data in GetFile().
Path finding rules follow expected behavior from dominant web servers:
- If the exact path is found, return it immediately.
- Try assuming a ".gohtml" or ".md" file extension for the path.
- Try checking if the path is a directory with an "index.gohtml" inside it, etc.
*/
func ResolveFile(path string) (string, error) {
// Ensure the path doesn't begin with a slash.
path = strings.TrimLeft(path, "/")
// Try the exact path.
if GetFileExists(path) {
return path, nil
}
// Try fuzzy file matches.
var tries = []string{
path + ".gohtml",
path + ".md",
path + "/index.gohtml",
path + "/index.html",
}
for _, try := range tries {
fmt.Printf("Try: %s\n", try)
2019-11-26 03:55:28 +00:00
path = strings.TrimLeft(try, "/")
if GetFileExists(path) {
return path, nil
}
}
return "", errors.New("not found")
}