mirror of
https://github.com/photoprism/photoprism.git
synced 2025-09-26 21:01:58 +08:00
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:
20
pkg/fs/fs.go
20
pkg/fs/fs.go
@@ -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()
|
||||
}
|
||||
|
@@ -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"))
|
||||
|
Reference in New Issue
Block a user