Pkg: Add fs.Exists() function to check for any existing file/dir/link

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2025-09-21 23:09:33 +02:00
parent c312c0d109
commit 458a320bb8
2 changed files with 28 additions and 4 deletions

View File

@@ -67,13 +67,25 @@ func SocketExists(socketName string) bool {
return true
}
// FileExists returns true if specified file exists and is not a directory.
func FileExists(fileName string) bool {
if fileName == "" {
// Exists returns true if the specified file system path exists,
// regardless of whether it is a file, directory, or link.
func Exists(fsPath string) bool {
if fsPath == "" {
return false
}
info, err := os.Stat(fileName)
_, err := os.Stat(fsPath)
return err == nil
}
// FileExists returns true if a file exists at the specified path.
func FileExists(filePath string) bool {
if filePath == "" {
return false
}
info, err := os.Stat(filePath)
return err == nil && !info.IsDir()
}

View File

@@ -21,7 +21,19 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func TestExists(t *testing.T) {
assert.True(t, Exists("./testdata"))
assert.True(t, Exists("./testdata/"))
assert.True(t, Exists("./testdata/test.jpg"))
assert.True(t, Exists("./testdata/test.jpg"))
assert.True(t, Exists("./testdata/empty.jpg"))
assert.False(t, Exists("./foo.jpg"))
assert.False(t, Exists(""))
}
func TestFileExists(t *testing.T) {
assert.False(t, FileExists("./testdata"))
assert.False(t, FileExists("./testdata/"))
assert.True(t, FileExists("./testdata/test.jpg"))
assert.True(t, FileExists("./testdata/test.jpg"))
assert.True(t, FileExists("./testdata/empty.jpg"))