go-maymoon/openfile.go

53 lines
945 B
Go

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
}