From 1e0f8e0aaeaf1bd2ee39c02e922238b641bcf88b Mon Sep 17 00:00:00 2001 From: tjp Date: Mon, 13 Nov 2023 07:25:39 -0700 Subject: refactor contribs to work with a Protocol interface --- contrib/fs/spartan.go | 110 ++++---------------------------------------------- 1 file changed, 7 insertions(+), 103 deletions(-) (limited to 'contrib/fs/spartan.go') diff --git a/contrib/fs/spartan.go b/contrib/fs/spartan.go index bee274a..d97edd1 100644 --- a/contrib/fs/spartan.go +++ b/contrib/fs/spartan.go @@ -1,10 +1,6 @@ package fs import ( - "context" - "os" - "path/filepath" - "strings" "text/template" sr "tildegit.org/tjp/sliderule" @@ -15,30 +11,7 @@ import ( // // It only serves responses for paths which correspond to regular files or symlinks to them. func SpartanFileHandler(fsroot, urlroot string) sr.Handler { - fsroot = strings.TrimRight(fsroot, "/") - - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, urlroot) { - return nil - } - requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - - fpath := filepath.Join(fsroot, requestpath) - if isPrivate(fpath) { - return nil - } - if isf, err := isFile(fpath); err != nil { - return spartan.ServerError(err) - } else if !isf { - return nil - } - - file, err := os.Open(fpath) - if err != nil { - return spartan.ServerError(err) - } - return spartan.Success(mediaType(fpath), file) - }) + return fileHandler(spartan.ServerProtocol, fsroot, urlroot) } // SpartanDirectoryDefault serves up default files for directory path requests. @@ -52,47 +25,7 @@ func SpartanFileHandler(fsroot, urlroot string) sr.Handler { // redirects to the URL with the slash appended. This is necessary for relative links // in the directory's contents to function properly. func SpartanDirectoryDefault(fsroot, urlroot string, filenames ...string) sr.Handler { - fsroot = strings.TrimRight(fsroot, "/") - - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasPrefix(request.Path, urlroot) { - return nil - } - - if !strings.HasSuffix(request.Path, "/") { - u := *request.URL - u.Path += "/" - return spartan.Redirect(u.String()) - } - - requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - fpath := filepath.Join(fsroot, requestpath) - if isPrivate(fpath) { - return nil - } - if isd, err := isDir(fpath); err != nil { - return spartan.ServerError(err) - } else if !isd { - return nil - } - - for _, fname := range filenames { - candidatepath := filepath.Join(fpath, fname) - if isf, err := isFile(candidatepath); err != nil { - return spartan.ServerError(err) - } else if !isf { - continue - } - - file, err := os.Open(candidatepath) - if err != nil { - return spartan.ServerError(err) - } - return spartan.Success(mediaType(candidatepath), file) - } - - return nil - }) + return directoryDefault(spartan.ServerProtocol, fsroot, urlroot, true, filenames...) } // SpartanDirectoryListing produces a listing of the contents of any requested directories. @@ -105,40 +38,11 @@ func SpartanDirectoryDefault(fsroot, urlroot string, filenames ...string) sr.Han // // The template may be nil, in which case DefaultSpartanDirectoryList is used instead. The // template is then processed with RenderDirectoryListing. -func SpartanDirectoryListing(fsroot, urlroot string, template *template.Template) sr.Handler { - fsroot = strings.TrimRight(fsroot, "/") - - return sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response { - if !strings.HasSuffix(request.Path, "/") { - u := *request.URL - u.Path += "/" - return spartan.Redirect(u.String()) - } - if !strings.HasPrefix(request.Path, urlroot) { - return nil - } - requestpath := strings.Trim(strings.TrimPrefix(request.Path, urlroot), "/") - - fpath := filepath.Join(fsroot, requestpath) - if isPrivate(fpath) { - return nil - } - if isd, err := isDir(fpath); err != nil { - return spartan.ServerError(err) - } else if !isd { - return nil - } - - if template == nil { - template = DefaultSpartanDirectoryList - } - body, err := RenderDirectoryListing(fpath, requestpath, template, request.Server) - if err != nil { - return spartan.ServerError(err) - } - - return spartan.Success("text/gemini", body) - }) +func SpartanDirectoryListing(fsroot, urlroot string, tmpl *template.Template) sr.Handler { + if tmpl == nil { + tmpl = DefaultSpartanDirectoryList + } + return directoryListing(spartan.ServerProtocol, fsroot, urlroot, "file.gmi", true, tmpl) } // DefaultSpartanDirectoryList is a tmeplate which renders a reasonable gemtext dir listing. -- cgit v1.2.3