summaryrefslogtreecommitdiff
path: root/contrib/fs
diff options
context:
space:
mode:
authortjpcc <tjp@ctrl-c.club>2023-01-17 15:59:29 -0700
committertjpcc <tjp@ctrl-c.club>2023-01-17 15:59:29 -0700
commit2ef530daa47b301a40c1ee93cd43b8f36fc68c0b (patch)
treeb9753719f5f0e5312bb5008d40f40247ce14e15a /contrib/fs
parent30e21f8513d49661cb6e1583d301e34e898d48a9 (diff)
pull request, response, handlers out of the gemini package
Diffstat (limited to 'contrib/fs')
-rw-r--r--contrib/fs/dir.go23
-rw-r--r--contrib/fs/file.go9
2 files changed, 17 insertions, 15 deletions
diff --git a/contrib/fs/dir.go b/contrib/fs/dir.go
index b219e22..6292f67 100644
--- a/contrib/fs/dir.go
+++ b/contrib/fs/dir.go
@@ -8,6 +8,7 @@ import (
"strings"
"text/template"
+ "tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/gemini"
)
@@ -24,10 +25,10 @@ import (
//
// It requires that files from the provided fs.FS implement fs.ReadDirFile. If they don't,
// it will also produce "51 Not Found" responses for directory paths.
-func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler {
- return func(ctx context.Context, req *gemini.Request) *gemini.Response {
+func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gus.Handler {
+ return func(ctx context.Context, req *gus.Request) *gus.Response {
path, dirFile, resp := handleDir(req, fileSystem)
- if resp != nil {
+ if dirFile == nil {
return resp
}
defer dirFile.Close()
@@ -50,7 +51,7 @@ func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler {
}
}
- return gemini.NotFound("Resource does not exist.")
+ return nil
}
}
@@ -69,10 +70,10 @@ func DirectoryDefault(fileSystem fs.FS, fileNames ...string) gemini.Handler {
// - .FullPath: the complete path to the listed directory
// - .DirName: the name of the directory itself
// - .Entries: the []fs.DirEntry of the directory contents
-//
+//
// The template argument may be nil, in which case a simple default template is used.
-func DirectoryListing(fileSystem fs.FS, template *template.Template) gemini.Handler {
- return func(ctx context.Context, req *gemini.Request) *gemini.Response {
+func DirectoryListing(fileSystem fs.FS, template *template.Template) gus.Handler {
+ return func(ctx context.Context, req *gus.Request) *gus.Response {
path, dirFile, resp := handleDir(req, fileSystem)
if resp != nil {
return resp
@@ -132,7 +133,7 @@ func dirlistNamespace(path string, dirFile fs.ReadDirFile) (map[string]any, erro
return m, nil
}
-func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *gemini.Response) {
+func handleDir(req *gus.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *gus.Response) {
path := strings.Trim(req.Path, "/")
if path == "" {
path = "."
@@ -140,7 +141,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *
file, err := fileSystem.Open(path)
if isNotFound(err) {
- return "", nil, gemini.NotFound("Resource does not exist.")
+ return "", nil, nil
}
if err != nil {
return "", nil, gemini.Failure(err)
@@ -154,7 +155,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *
if !isDir {
file.Close()
- return "", nil, gemini.NotFound("Resource does not exist.")
+ return "", nil, nil
}
if !strings.HasSuffix(req.Path, "/") {
@@ -167,7 +168,7 @@ func handleDir(req *gemini.Request, fileSystem fs.FS) (string, fs.ReadDirFile, *
dirFile, ok := file.(fs.ReadDirFile)
if !ok {
file.Close()
- return "", nil, gemini.NotFound("Resource does not exist.")
+ return "", nil, nil
}
return path, dirFile, nil
diff --git a/contrib/fs/file.go b/contrib/fs/file.go
index cdcd1a9..8cb1aeb 100644
--- a/contrib/fs/file.go
+++ b/contrib/fs/file.go
@@ -6,15 +6,16 @@ import (
"mime"
"strings"
+ "tildegit.org/tjp/gus"
"tildegit.org/tjp/gus/gemini"
)
// FileHandler builds a handler function which serves up a file system.
-func FileHandler(fileSystem fs.FS) gemini.Handler {
- return func(ctx context.Context, req *gemini.Request) *gemini.Response {
+func FileHandler(fileSystem fs.FS) gus.Handler {
+ return func(ctx context.Context, req *gus.Request) *gus.Response {
file, err := fileSystem.Open(strings.TrimPrefix(req.Path, "/"))
if isNotFound(err) {
- return gemini.NotFound("Resource does not exist.")
+ return nil
}
if err != nil {
return gemini.Failure(err)
@@ -26,7 +27,7 @@ func FileHandler(fileSystem fs.FS) gemini.Handler {
}
if isDir {
- return gemini.NotFound("Resource does not exist.")
+ return nil
}
return gemini.Success(mediaType(req.Path), file)