From 7a54232d0d0ceeba3981ccd01a93da3efb841d77 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Tue, 15 Aug 2023 00:38:58 -0700 Subject: [PATCH] initial commits --- openfile.go | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 openfile.go diff --git a/openfile.go b/openfile.go new file mode 100644 index 0000000..4d86da2 --- /dev/null +++ b/openfile.go @@ -0,0 +1,52 @@ +package maymoon + +import ( + "sourcecode.social/reiver/go-errhttp" + + "errors" + "io/fs" +) + +var ( + errNilFile = errors.New("nil file") + errNilFileSystem = errors.New("nil file-system") +) + +func openFile(filesystem fs.FS, requestpath string) (fs.File, error) { + + if nil == filesystem { +/////////////// RETURN + return nil, errhttp.InternalServerErrorWrap(errNilFileSystem) + } + + var relpath string + { + var requestpath0 byte = requestpath[0] + + if '/' == requestpath0 { + relpath = requestpath[1:] + } + } + + var file fs.File + { + var err error + + file, err = filesystem.Open(relpath) + if nil != err { + if errors.Is(err, fs.ErrNotExist) { +/////////////////////////////// RETURN + return nil, errhttp.ErrNotFound + } + +/////////////////////// RETURN + return nil, errhttp.InternalServerErrorWrap(err) + } + if nil == file { +/////////////////////// RETURN + return nil, errhttp.InternalServerErrorWrap(errNilFile) + } + } + + return file, nil +}