initial commits
							parent
							
								
									1012adefc9
								
							
						
					
					
						commit
						7a54232d0d
					
				| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue