mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
templates: Add fileStat
function (#5497)
* Add isDir template function * Update modules/caddyhttp/templates/tplcontext.go Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com> * Fix funcIsDir return value on error * Fix funcIsDir return false when root file system not specified * Add stat function, remove isDir function * Remove isDir function (really) * Rename stat to fileStat --------- Co-authored-by: Mohammed Al Sahaf <msaa1990@gmail.com>
This commit is contained in:
parent
53b6fab125
commit
2943c41884
1 changed files with 17 additions and 0 deletions
|
@ -18,6 +18,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
@ -79,6 +80,7 @@ func (c *TemplateContext) NewTemplate(tplName string) *template.Template {
|
|||
"markdown": c.funcMarkdown,
|
||||
"splitFrontMatter": c.funcSplitFrontMatter,
|
||||
"listFiles": c.funcListFiles,
|
||||
"fileStat": c.funcFileStat,
|
||||
"env": c.funcEnv,
|
||||
"placeholder": c.funcPlaceholder,
|
||||
"fileExists": c.funcFileExists,
|
||||
|
@ -395,6 +397,21 @@ func (c TemplateContext) funcFileExists(filename string) (bool, error) {
|
|||
return false, nil
|
||||
}
|
||||
|
||||
// funcStat returns Stat of a filename
|
||||
func (c TemplateContext) funcFileStat(filename string) (fs.FileInfo, error) {
|
||||
if c.Root == nil {
|
||||
return nil, fmt.Errorf("root file system not specified")
|
||||
}
|
||||
|
||||
file, err := c.Root.Open(path.Clean(filename))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
return file.Stat()
|
||||
}
|
||||
|
||||
// funcHTTPError returns a structured HTTP handler error. EXPERIMENTAL; SUBJECT TO CHANGE.
|
||||
// Example usage: `{{if not (fileExists $includeFile)}}{{httpError 404}}{{end}}`
|
||||
func (c TemplateContext) funcHTTPError(statusCode int) (bool, error) {
|
||||
|
|
Loading…
Reference in a new issue