2022-08-28 13:21:57 +02:00
|
|
|
// Package data provides common functionality for http servers
|
2018-12-23 01:16:50 +01:00
|
|
|
// The "go:generate" directive compiles static assets by running assets_generate.go
|
2022-08-28 13:21:57 +02:00
|
|
|
//
|
|
|
|
//go:generate go run assets_generate.go
|
2018-12-23 01:16:50 +01:00
|
|
|
package data
|
|
|
|
|
|
|
|
import (
|
2021-11-04 11:12:57 +01:00
|
|
|
"fmt"
|
2018-12-23 01:16:50 +01:00
|
|
|
"html/template"
|
2022-08-20 16:38:02 +02:00
|
|
|
"io"
|
|
|
|
"os"
|
2020-05-08 17:15:21 +02:00
|
|
|
"time"
|
2018-12-23 01:16:50 +01:00
|
|
|
|
2021-04-20 06:59:36 +02:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2021-04-20 06:59:36 +02:00
|
|
|
"github.com/rclone/rclone/fs/config/flags"
|
2018-12-23 01:16:50 +01:00
|
|
|
)
|
|
|
|
|
2021-04-20 06:59:36 +02:00
|
|
|
// Help describes the options for the serve package
|
2022-01-24 16:38:25 +01:00
|
|
|
var Help = `
|
|
|
|
#### Template
|
|
|
|
|
2022-06-19 19:55:37 +02:00
|
|
|
` + "`--template`" + ` allows a user to specify a custom markup template for HTTP
|
2022-06-19 18:59:54 +02:00
|
|
|
and WebDAV serve functions. The server exports the following markup
|
2021-04-20 06:59:36 +02:00
|
|
|
to be used within the template to server pages:
|
|
|
|
|
|
|
|
| Parameter | Description |
|
|
|
|
| :---------- | :---------- |
|
|
|
|
| .Name | The full path of a file/directory. |
|
|
|
|
| .Title | Directory listing of .Name |
|
|
|
|
| .Sort | The current sort used. This is changeable via ?sort= parameter |
|
|
|
|
| | Sort Options: namedirfirst,name,size,time (default namedirfirst) |
|
|
|
|
| .Order | The current ordering used. This is changeable via ?order= parameter |
|
|
|
|
| | Order Options: asc,desc (default asc) |
|
|
|
|
| .Query | Currently unused. |
|
|
|
|
| .Breadcrumb | Allows for creating a relative navigation |
|
|
|
|
|-- .Link | The relative to the root link of the Text. |
|
|
|
|
|-- .Text | The Name of the directory. |
|
|
|
|
| .Entries | Information about a specific file/directory. |
|
|
|
|
|-- .URL | The 'url' of an entry. |
|
|
|
|
|-- .Leaf | Currently same as 'URL' but intended to be 'just' the name. |
|
|
|
|
|-- .IsDir | Boolean for if an entry is a directory or not. |
|
|
|
|
|-- .Size | Size in Bytes of the entry. |
|
|
|
|
|-- .ModTime | The UTC timestamp of an entry. |
|
|
|
|
`
|
|
|
|
|
|
|
|
// Options for the templating functionality
|
|
|
|
type Options struct {
|
|
|
|
Template string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddFlags for the templating functionality
|
|
|
|
func AddFlags(flagSet *pflag.FlagSet, prefix string, Opt *Options) {
|
2021-08-16 11:30:01 +02:00
|
|
|
flags.StringVarP(flagSet, &Opt.Template, prefix+"template", "", Opt.Template, "User-specified template")
|
2021-04-20 06:59:36 +02:00
|
|
|
}
|
|
|
|
|
2020-05-10 19:53:38 +02:00
|
|
|
// AfterEpoch returns the time since the epoch for the given time
|
2020-05-08 17:15:21 +02:00
|
|
|
func AfterEpoch(t time.Time) bool {
|
|
|
|
return t.After(time.Time{})
|
|
|
|
}
|
|
|
|
|
2022-06-19 18:59:54 +02:00
|
|
|
// GetTemplate returns the HTML template for serving directories via HTTP/WebDAV
|
2020-04-30 20:24:11 +02:00
|
|
|
func GetTemplate(tmpl string) (tpl *template.Template, err error) {
|
|
|
|
var templateString string
|
|
|
|
if tmpl == "" {
|
|
|
|
templateFile, err := Assets.Open("index.html")
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("get template open: %w", err)
|
2020-04-30 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
defer fs.CheckClose(templateFile, &err)
|
|
|
|
|
2022-08-20 16:38:02 +02:00
|
|
|
templateBytes, err := io.ReadAll(templateFile)
|
2020-04-30 20:24:11 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("get template read: %w", err)
|
2020-04-30 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
templateString = string(templateBytes)
|
|
|
|
|
|
|
|
} else {
|
2022-08-20 16:38:02 +02:00
|
|
|
templateFile, err := os.ReadFile(tmpl)
|
2020-04-30 20:24:11 +02:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("get template open: %w", err)
|
2020-04-30 20:24:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
templateString = string(templateFile)
|
2018-12-23 01:16:50 +01:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:15:21 +02:00
|
|
|
funcMap := template.FuncMap{
|
|
|
|
"afterEpoch": AfterEpoch,
|
|
|
|
}
|
|
|
|
tpl, err = template.New("index").Funcs(funcMap).Parse(templateString)
|
2018-12-23 01:16:50 +01:00
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return nil, fmt.Errorf("get template parse: %w", err)
|
2018-12-23 01:16:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|