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 +}