initial commits

master
Charles Iliya Krempeaux 2023-08-15 00:38:58 -07:00
parent 1012adefc9
commit 7a54232d0d
1 changed files with 52 additions and 0 deletions

52
openfile.go 100644
View File

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