mirror of
https://github.com/photoprism/photoprism.git
synced 2025-12-12 00:34:13 +01:00
Config: Improve the naming of file and directory variables
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
@@ -75,8 +75,6 @@ func (c *Config) WallpaperUri() string {
|
||||
wallpaperUri = c.StaticAssetUri(path.Join(wallpaperPath, fileName))
|
||||
} else if fs.FileExists(c.CustomStaticFile(path.Join(wallpaperPath, fileName))) {
|
||||
wallpaperUri = c.CustomStaticAssetUri(path.Join(wallpaperPath, fileName))
|
||||
} else if fs.FileExists(path.Join(c.ThemePath(), fileName)) {
|
||||
wallpaperUri = c.BaseUri("/" + path.Join("_theme", fileName))
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -62,13 +62,13 @@ func (m *Folder) BeforeCreate(scope *gorm.Scope) error {
|
||||
}
|
||||
|
||||
// NewFolder creates a new file system directory entity.
|
||||
func NewFolder(root, pathName string, modTime time.Time) Folder {
|
||||
func NewFolder(root, dir string, modTime time.Time) Folder {
|
||||
now := Now()
|
||||
|
||||
pathName = strings.Trim(pathName, string(os.PathSeparator))
|
||||
dir = strings.Trim(dir, string(os.PathSeparator))
|
||||
|
||||
if pathName == RootPath {
|
||||
pathName = ""
|
||||
if dir == RootPath {
|
||||
dir = ""
|
||||
}
|
||||
|
||||
year := 0
|
||||
@@ -82,7 +82,7 @@ func NewFolder(root, pathName string, modTime time.Time) Folder {
|
||||
result := Folder{
|
||||
FolderUID: rnd.GenerateUID('d'),
|
||||
Root: root,
|
||||
Path: pathName,
|
||||
Path: dir,
|
||||
FolderType: MediaUnknown,
|
||||
FolderOrder: sortby.Name,
|
||||
FolderCountry: UnknownCountry.ID,
|
||||
@@ -195,17 +195,17 @@ func (m *Folder) Create() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// FindFolder returns an existing row if exists.
|
||||
func FindFolder(root, pathName string) *Folder {
|
||||
pathName = strings.Trim(pathName, string(os.PathSeparator))
|
||||
// FindFolder returns an existing folder if it exists.
|
||||
func FindFolder(root, dir string) *Folder {
|
||||
dir = strings.Trim(dir, string(os.PathSeparator))
|
||||
|
||||
if pathName == RootPath {
|
||||
pathName = ""
|
||||
if dir == RootPath {
|
||||
dir = ""
|
||||
}
|
||||
|
||||
result := Folder{}
|
||||
|
||||
if err := Db().Where("path = ? AND root = ?", pathName, root).First(&result).Error; err == nil {
|
||||
if err := Db().Where("path = ? AND root = ?", dir, root).First(&result).Error; err == nil {
|
||||
return &result
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ func TestNewFolder(t *testing.T) {
|
||||
assert.Equal(t, "gb", folder.FolderCountry)
|
||||
})
|
||||
|
||||
t.Run("empty", func(t *testing.T) {
|
||||
t.Run("RootOriginalsNoDir", func(t *testing.T) {
|
||||
folder := NewFolder(RootOriginals, "", time.Time{})
|
||||
assert.Equal(t, "", folder.Path)
|
||||
assert.Equal(t, "Originals", folder.FolderTitle)
|
||||
@@ -73,7 +73,7 @@ func TestNewFolder(t *testing.T) {
|
||||
assert.Equal(t, UnknownID, folder.FolderCountry)
|
||||
})
|
||||
|
||||
t.Run("root", func(t *testing.T) {
|
||||
t.Run("RootOriginalsRootDir", func(t *testing.T) {
|
||||
folder := NewFolder(RootOriginals, RootPath, time.Time{})
|
||||
assert.Equal(t, "", folder.Path)
|
||||
assert.Equal(t, "Originals", folder.FolderTitle)
|
||||
@@ -82,9 +82,11 @@ func TestNewFolder(t *testing.T) {
|
||||
assert.Equal(t, UnknownID, folder.FolderCountry)
|
||||
})
|
||||
|
||||
t.Run("pathName equals root path", func(t *testing.T) {
|
||||
t.Run("NoRootWithRootDir", func(t *testing.T) {
|
||||
folder := NewFolder("", RootPath, time.Now().UTC())
|
||||
assert.Equal(t, "", folder.Path)
|
||||
assert.Equal(t, "", folder.FolderTitle)
|
||||
assert.Equal(t, UnknownID, folder.FolderCountry)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -142,16 +144,17 @@ func TestFolder_Title(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestFolder_RootPath(t *testing.T) {
|
||||
t.Run("/rainbow", func(t *testing.T) {
|
||||
t.Run("Rainbow", func(t *testing.T) {
|
||||
folder := Folder{FolderTitle: "Beautiful beach", Root: "/", Path: "rainbow"}
|
||||
assert.Equal(t, "/rainbow", folder.RootPath())
|
||||
})
|
||||
}
|
||||
|
||||
func TestFindFolder(t *testing.T) {
|
||||
t.Run("nil", func(t *testing.T) {
|
||||
t.Run("NotFound", func(t *testing.T) {
|
||||
assert.Nil(t, FindFolder("vvfgt", "jgfuyf"))
|
||||
})
|
||||
t.Run("pathName === rootPath", func(t *testing.T) {
|
||||
t.Run("PathNameIsRootPath", func(t *testing.T) {
|
||||
assert.Nil(t, FindFolder("vvfgt", RootPath))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,15 +7,15 @@ import (
|
||||
)
|
||||
|
||||
// Duplicates finds duplicate files in the range of limit and offset sorted by file name.
|
||||
func Duplicates(limit, offset int, pathName string) (files entity.Duplicates, err error) {
|
||||
if strings.HasPrefix(pathName, "/") {
|
||||
pathName = pathName[1:]
|
||||
func Duplicates(limit, offset int, dir string) (files entity.Duplicates, err error) {
|
||||
if strings.HasPrefix(dir, "/") {
|
||||
dir = dir[1:]
|
||||
}
|
||||
|
||||
stmt := Db()
|
||||
|
||||
if pathName != "" {
|
||||
stmt = stmt.Where("file_name LIKE ?", pathName+"/%")
|
||||
if dir != "" {
|
||||
stmt = stmt.Where("file_name LIKE ?", dir+"/%")
|
||||
}
|
||||
|
||||
err = stmt.Order("file_name").Limit(limit).Offset(offset).Find(&files).Error
|
||||
|
||||
@@ -11,16 +11,16 @@ import (
|
||||
)
|
||||
|
||||
// FilesByPath returns a slice of files in a given originals folder.
|
||||
func FilesByPath(limit, offset int, rootName, pathName string, public bool) (files entity.Files, err error) {
|
||||
if strings.HasPrefix(pathName, "/") {
|
||||
pathName = pathName[1:]
|
||||
func FilesByPath(limit, offset int, root, dir string, public bool) (files entity.Files, err error) {
|
||||
if strings.HasPrefix(dir, "/") {
|
||||
dir = dir[1:]
|
||||
}
|
||||
|
||||
stmt := Db().
|
||||
Table("files").Select("files.*").
|
||||
Joins("JOIN photos ON photos.id = files.photo_id AND photos.deleted_at IS NULL").
|
||||
Where("files.file_missing = 0 AND files.file_root = ?", rootName).
|
||||
Where("photos.photo_path = ?", pathName)
|
||||
Where("files.file_missing = 0 AND files.file_root = ?", root).
|
||||
Where("photos.photo_path = ?", dir)
|
||||
|
||||
if public {
|
||||
stmt = stmt.Where("photos.photo_private = 0")
|
||||
@@ -34,9 +34,9 @@ func FilesByPath(limit, offset int, rootName, pathName string, public bool) (fil
|
||||
}
|
||||
|
||||
// Files returns not-missing and not-deleted file entities in the range of limit and offset sorted by id.
|
||||
func Files(limit, offset int, pathName string, includeMissing bool) (files entity.Files, err error) {
|
||||
if strings.HasPrefix(pathName, "/") {
|
||||
pathName = pathName[1:]
|
||||
func Files(limit, offset int, dir string, includeMissing bool) (files entity.Files, err error) {
|
||||
if strings.HasPrefix(dir, "/") {
|
||||
dir = dir[1:]
|
||||
}
|
||||
|
||||
stmt := Db()
|
||||
@@ -45,8 +45,8 @@ func Files(limit, offset int, pathName string, includeMissing bool) (files entit
|
||||
stmt = stmt.Where("file_missing = 0")
|
||||
}
|
||||
|
||||
if pathName != "" {
|
||||
stmt = stmt.Where("files.file_name LIKE ?", pathName+"/%")
|
||||
if dir != "" {
|
||||
stmt = stmt.Where("files.file_name LIKE ?", dir+"/%")
|
||||
}
|
||||
|
||||
err = stmt.Order("id").Limit(limit).Offset(offset).Find(&files).Error
|
||||
|
||||
@@ -307,8 +307,8 @@ func (imp *Import) DestinationFilename(mainFile *MediaFile, mediaFile *MediaFile
|
||||
|
||||
// Find and return available filename.
|
||||
iteration := 0
|
||||
pathName := filepath.Join(imp.originalsPath(), folder, dateCreated.Format("2006/01"))
|
||||
result := filepath.Join(pathName, fileName+fileExtension)
|
||||
dir := filepath.Join(imp.originalsPath(), folder, dateCreated.Format("2006/01"))
|
||||
result := filepath.Join(dir, fileName+fileExtension)
|
||||
|
||||
for fs.FileExists(result) {
|
||||
if mediaFile.Hash() == fs.Hash(result) {
|
||||
@@ -317,7 +317,7 @@ func (imp *Import) DestinationFilename(mainFile *MediaFile, mediaFile *MediaFile
|
||||
|
||||
iteration++
|
||||
|
||||
result = filepath.Join(pathName, fileName+"."+fmt.Sprintf("%05d", iteration)+fileExtension)
|
||||
result = filepath.Join(dir, fileName+"."+fmt.Sprintf("%05d", iteration)+fileExtension)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
|
||||
@@ -153,39 +153,42 @@ func (l *IgnoreList) File(fileName string) error {
|
||||
}
|
||||
|
||||
// Path adds the ignore patterns found in the ignore config file of the specified directory, if any.
|
||||
func (l *IgnoreList) Path(pathName string) error {
|
||||
if pathName == "" {
|
||||
return errors.New("empty config path name")
|
||||
func (l *IgnoreList) Path(dir string) error {
|
||||
if dir == "" {
|
||||
return errors.New("missing directory name")
|
||||
} else if l.configFile == "" {
|
||||
return errors.New("empty config file name")
|
||||
return errors.New("missing config file name")
|
||||
}
|
||||
|
||||
return l.File(filepath.Join(pathName, l.configFile))
|
||||
return l.File(filepath.Join(dir, l.configFile))
|
||||
}
|
||||
|
||||
// Ignore returns true if the file name should be ignored.
|
||||
// Ignore checks if the file or folder name should be ignored.
|
||||
func (l *IgnoreList) Ignore(name string) bool {
|
||||
pathName := filepath.Dir(name)
|
||||
// Determine the parent directory path
|
||||
// and the base name without the path.
|
||||
dir := filepath.Dir(name)
|
||||
baseName := filepath.Base(name)
|
||||
|
||||
// Change name to lowercase for case-insensitive comparison.
|
||||
if l.caseSensitive == false {
|
||||
pathName = strings.ToLower(pathName)
|
||||
dir = strings.ToLower(dir)
|
||||
baseName = strings.ToLower(baseName)
|
||||
}
|
||||
|
||||
// Check if name matches the config file name.
|
||||
// Ignore if name matches the config file name.
|
||||
if l.configFile != "" && baseName == l.configFile {
|
||||
_ = l.File(name)
|
||||
return true
|
||||
}
|
||||
|
||||
// Use mutex for thread safety.
|
||||
l.ignoredMutex.Lock()
|
||||
defer l.ignoredMutex.Unlock()
|
||||
|
||||
// Check if any name patterns should be ignored.
|
||||
// Iterate through configured patterns to determine if the name should be ignored.
|
||||
for _, pattern := range l.ignore {
|
||||
if pattern.Ignore(pathName, baseName) {
|
||||
if pattern.Ignore(dir, baseName) {
|
||||
l.ignored = append(l.ignored, name)
|
||||
|
||||
if l.Log != nil {
|
||||
@@ -196,8 +199,10 @@ func (l *IgnoreList) Ignore(name string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore hidden files and folders whose name e.g. starts with a "."?
|
||||
if l.ignoreHidden && FileNameHidden(name) {
|
||||
l.hidden = append(l.hidden, name)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
@@ -12,57 +12,57 @@ import (
|
||||
|
||||
func TestWriteFile(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
pathName := "./testdata/_WriteFile_Success"
|
||||
fileName := filepath.Join(pathName, "notyetexisting.jpg")
|
||||
dir := "./testdata/_WriteFile_Success"
|
||||
filePath := filepath.Join(dir, "notyetexisting.jpg")
|
||||
fileData := []byte("foobar")
|
||||
|
||||
if err := MkdirAll(pathName); err != nil {
|
||||
if err := MkdirAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(fileName)
|
||||
_ = os.Remove(filePath)
|
||||
|
||||
if err := os.RemoveAll(pathName); err != nil {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.True(t, PathExists(pathName))
|
||||
assert.True(t, PathExists(dir))
|
||||
|
||||
fileErr := WriteFile(fileName, fileData)
|
||||
fileErr := WriteFile(filePath, fileData)
|
||||
|
||||
assert.NoError(t, fileErr)
|
||||
assert.FileExists(t, fileName)
|
||||
assert.FileExists(t, filePath)
|
||||
})
|
||||
}
|
||||
|
||||
func TestWriteString(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
pathName := "./testdata/_WriteString_Success"
|
||||
fileName := filepath.Join(pathName, PPIgnoreFilename)
|
||||
dir := "./testdata/_WriteString_Success"
|
||||
filePath := filepath.Join(dir, PPIgnoreFilename)
|
||||
fileData := "*"
|
||||
|
||||
if err := MkdirAll(pathName); err != nil {
|
||||
if err := MkdirAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(fileName)
|
||||
_ = os.Remove(filePath)
|
||||
|
||||
if err := os.RemoveAll(pathName); err != nil {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.True(t, PathExists(pathName))
|
||||
assert.True(t, PathExists(dir))
|
||||
|
||||
fileErr := WriteString(fileName, fileData)
|
||||
fileErr := WriteString(filePath, fileData)
|
||||
|
||||
assert.NoError(t, fileErr)
|
||||
assert.FileExists(t, fileName)
|
||||
assert.FileExists(t, filePath)
|
||||
|
||||
readLines, readErr := ReadLines(fileName)
|
||||
readLines, readErr := ReadLines(filePath)
|
||||
|
||||
assert.NoError(t, readErr)
|
||||
assert.Len(t, readLines, 1)
|
||||
@@ -72,29 +72,29 @@ func TestWriteString(t *testing.T) {
|
||||
|
||||
func TestWriteUnixTime(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
pathName := "./testdata/_WriteUnixTime_Success"
|
||||
fileName := filepath.Join(pathName, PPStorageFilename)
|
||||
dir := "./testdata/_WriteUnixTime_Success"
|
||||
filePath := filepath.Join(dir, PPStorageFilename)
|
||||
|
||||
if err := MkdirAll(pathName); err != nil {
|
||||
if err := MkdirAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(fileName)
|
||||
_ = os.Remove(filePath)
|
||||
|
||||
if err := os.RemoveAll(pathName); err != nil {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.True(t, PathExists(pathName))
|
||||
assert.True(t, PathExists(dir))
|
||||
|
||||
unixTime, fileErr := WriteUnixTime(fileName)
|
||||
unixTime, fileErr := WriteUnixTime(filePath)
|
||||
|
||||
assert.NoError(t, fileErr)
|
||||
assert.FileExists(t, fileName)
|
||||
assert.FileExists(t, filePath)
|
||||
|
||||
readLines, readErr := ReadLines(fileName)
|
||||
readLines, readErr := ReadLines(filePath)
|
||||
|
||||
assert.NoError(t, readErr)
|
||||
assert.Len(t, readLines, 1)
|
||||
@@ -104,38 +104,38 @@ func TestWriteUnixTime(t *testing.T) {
|
||||
|
||||
func TestWriteFileFromReader(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
pathName := "./testdata/_WriteFileFromReader_Success"
|
||||
dir := "./testdata/_WriteFileFromReader_Success"
|
||||
|
||||
fileName1 := filepath.Join(pathName, "1.txt")
|
||||
fileName2 := filepath.Join(pathName, "2.txt")
|
||||
filePath1 := filepath.Join(dir, "1.txt")
|
||||
filePath2 := filepath.Join(dir, "2.txt")
|
||||
|
||||
if err := MkdirAll(pathName); err != nil {
|
||||
if err := MkdirAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(fileName1)
|
||||
_ = os.Remove(fileName2)
|
||||
_ = os.Remove(filePath1)
|
||||
_ = os.Remove(filePath2)
|
||||
|
||||
if err := os.RemoveAll(pathName); err != nil {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.True(t, PathExists(pathName))
|
||||
assert.True(t, PathExists(dir))
|
||||
|
||||
unixTime, writeErr := WriteUnixTime(fileName1)
|
||||
unixTime, writeErr := WriteUnixTime(filePath1)
|
||||
assert.NoError(t, writeErr)
|
||||
assert.True(t, unixTime >= time.Now().Unix())
|
||||
|
||||
fileReader, readerErr := os.Open(fileName1)
|
||||
fileReader, readerErr := os.Open(filePath1)
|
||||
assert.NoError(t, readerErr)
|
||||
|
||||
fileErr := WriteFileFromReader(fileName2, fileReader)
|
||||
fileErr := WriteFileFromReader(filePath2, fileReader)
|
||||
|
||||
assert.NoError(t, fileErr)
|
||||
|
||||
readLines, readErr := ReadLines(fileName2)
|
||||
readLines, readErr := ReadLines(filePath2)
|
||||
|
||||
assert.NoError(t, readErr)
|
||||
assert.Len(t, readLines, 1)
|
||||
@@ -145,39 +145,39 @@ func TestWriteFileFromReader(t *testing.T) {
|
||||
|
||||
func TestCacheFileFromReader(t *testing.T) {
|
||||
t.Run("Success", func(t *testing.T) {
|
||||
pathName := "./testdata/_CacheFileFromReader_Success"
|
||||
dir := "./testdata/_CacheFileFromReader_Success"
|
||||
|
||||
fileName1 := filepath.Join(pathName, "1.txt")
|
||||
fileName2 := filepath.Join(pathName, "2.txt")
|
||||
fileName3 := filepath.Join(pathName, "3.txt")
|
||||
filePath1 := filepath.Join(dir, "1.txt")
|
||||
filePath2 := filepath.Join(dir, "2.txt")
|
||||
filePath3 := filepath.Join(dir, "3.txt")
|
||||
|
||||
if err := MkdirAll(pathName); err != nil {
|
||||
if err := MkdirAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = os.Remove(fileName1)
|
||||
_ = os.Remove(fileName2)
|
||||
_ = os.Remove(fileName3)
|
||||
_ = os.Remove(filePath1)
|
||||
_ = os.Remove(filePath2)
|
||||
_ = os.Remove(filePath3)
|
||||
|
||||
if err := os.RemoveAll(pathName); err != nil {
|
||||
if err := os.RemoveAll(dir); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
assert.True(t, PathExists(pathName))
|
||||
assert.True(t, PathExists(dir))
|
||||
|
||||
unixTime, writeErr := WriteUnixTime(fileName1)
|
||||
unixTime, writeErr := WriteUnixTime(filePath1)
|
||||
assert.NoError(t, writeErr)
|
||||
assert.True(t, unixTime >= time.Now().Unix())
|
||||
|
||||
fileReader, readerErr := os.Open(fileName1)
|
||||
fileReader, readerErr := os.Open(filePath1)
|
||||
assert.NoError(t, readerErr)
|
||||
|
||||
cacheFile, cacheErr := CacheFileFromReader(fileName2, fileReader)
|
||||
cacheFile, cacheErr := CacheFileFromReader(filePath2, fileReader)
|
||||
|
||||
assert.NoError(t, cacheErr)
|
||||
assert.Equal(t, fileName2, cacheFile)
|
||||
assert.Equal(t, filePath2, cacheFile)
|
||||
|
||||
readLines, readErr := ReadLines(cacheFile)
|
||||
|
||||
@@ -185,14 +185,14 @@ func TestCacheFileFromReader(t *testing.T) {
|
||||
assert.Len(t, readLines, 1)
|
||||
assert.Equal(t, strconv.FormatInt(unixTime, 10), readLines[0])
|
||||
|
||||
if err := WriteString(fileName3, "0"); err != nil {
|
||||
if err := WriteString(filePath3, "0"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cacheFile, cacheErr = CacheFileFromReader(fileName3, fileReader)
|
||||
cacheFile, cacheErr = CacheFileFromReader(filePath3, fileReader)
|
||||
|
||||
assert.NoError(t, cacheErr)
|
||||
assert.Equal(t, fileName3, cacheFile)
|
||||
assert.Equal(t, filePath3, cacheFile)
|
||||
|
||||
readLines, readErr = ReadLines(cacheFile)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user