2019-11-26 03:55:28 +00:00
|
|
|
package responses
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-02-17 23:50:04 +00:00
|
|
|
"fmt"
|
2019-11-26 03:55:28 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-02-17 23:50:04 +00:00
|
|
|
"path/filepath"
|
2019-11-26 03:55:28 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.kirsle.net/apps/gophertype/pkg/bundled"
|
2020-02-17 23:50:04 +00:00
|
|
|
"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) {
|
2020-02-17 23:50:04 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:50:04 +00:00
|
|
|
// Fall back on embedded bindata.
|
|
|
|
if b, err := bundled.Asset(path); err == nil {
|
2019-11-26 03:55:28 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
2020-02-17 23:50:04 +00:00
|
|
|
|
|
|
|
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.
|
2021-05-31 23:11:01 +00:00
|
|
|
if _, err := bundled.Asset(path); err == nil {
|
2019-11-26 03:55:28 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:50:04 +00:00
|
|
|
// 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 {
|
2021-05-31 23:11:01 +00:00
|
|
|
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")
|
|
|
|
}
|