Added fuzzy finder to search, made sync check as middleware, .id file is never used
Build / build (push) Successful in 45s

This commit is contained in:
2026-07-02 09:30:24 +02:00
parent a8df738108
commit eda60e0c07
9 changed files with 522 additions and 282 deletions
+22 -54
View File
@@ -175,17 +175,17 @@ func SyncResult() SyncResponse {
}
}
func SyncSoundtracksNewFull() {
syncSoundtracksNew(true)
func SyncSoundtracksFull() {
syncSoundtracks(true)
Reset()
}
func SyncSoundtracksNewOnlyChanges() {
syncSoundtracksNew(false)
func SyncSoundtracksOnlyChanges() {
syncSoundtracks(false)
Reset()
}
func syncSoundtracksNew(full bool) {
func syncSoundtracks(full bool) {
musicPath := os.Getenv("MUSIC_PATH")
fmt.Printf("dir: %s\n", musicPath)
logging.GetLogger().Debug("Folder to sync", zap.String("MUSIC_PATH", musicPath))
@@ -233,11 +233,11 @@ func syncSoundtracksNew(full bool) {
for _, dir := range directories {
pool.Submit(func() {
defer syncWg.Done()
syncSoundtrackNew(dir, foldersToSkip, musicPath, full)
syncSoundtrack(dir, foldersToSkip, musicPath, full)
})
}
syncWg.Wait()
checkBrokenSongsNew()
checkBrokenSongs()
soundtracksAfterSync, err = repo.FindAllSoundtracks(BackendCtx())
handleError("FindAllSoundtracks After", err, "")
@@ -250,7 +250,7 @@ func syncSoundtracksNew(full bool) {
Syncing = false
}
func checkBrokenSongsNew() {
func checkBrokenSongs() {
allSongs, err := repo.FetchAllSongs(BackendCtx())
handleError("FetchAllSongs", err, "")
var brokenWg sync.WaitGroup
@@ -261,7 +261,7 @@ func checkBrokenSongsNew() {
for _, song := range allSongs {
poolBroken.Submit(func() {
defer brokenWg.Done()
checkBrokenSongNew(song)
checkBrokenSong(song)
})
}
brokenWg.Wait()
@@ -271,7 +271,7 @@ func checkBrokenSongsNew() {
}
}
func checkBrokenSongNew(song repository.Song) {
func checkBrokenSong(song repository.Song) {
//Check if file exists and open
openFile, err := os.Open(song.Path)
if err != nil {
@@ -286,7 +286,7 @@ func checkBrokenSongNew(song repository.Song) {
}
}
func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string, full bool) {
func syncSoundtrack(file os.DirEntry, foldersToSkip []string, baseDir string, full bool) {
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
logging.GetLogger().Debug("Syncing soundtrack", zap.String("soundtrack", file.Name()))
soundtrackDir := baseDir + file.Name() + "/"
@@ -328,46 +328,14 @@ func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string,
}
switch status {
case NewSoundtrack:
if id != -1 {
for _, entry := range entries {
fileInfo, err := entry.Info()
if err != nil {
logging.GetLogger().Error("Failed to get file info", zap.String("error", err.Error()))
continue
}
id = getIdFromFileNew(fileInfo)
if id != -1 {
break
}
}
err = repo.InsertSoundtrackWithExistingId(BackendCtx(), repository.InsertSoundtrackWithExistingIdParams{ID: id, SoundtrackName: file.Name(), Path: soundtrackDir, Hash: dirHash})
handleError("InsertSoundtrackWithExistingId", err, "")
if err != nil {
logging.GetLogger().Debug("Soundtrack already exists, removing old ID file",
zap.Int32("id", id),
zap.String("soundtrack_dir", soundtrackDir))
fileName := soundtrackDir + "/." + strconv.Itoa(int(id)) + ".id"
logging.GetLogger().Debug("Removing ID file", zap.String("filename", fileName))
err := os.Remove(fileName)
if err != nil {
logging.GetLogger().Error("Failed to remove ID file", zap.String("filename", fileName), zap.String("error", err.Error()))
}
newDirHash := getHashForDir(soundtrackDir)
id = insertSoundtrackNew(file.Name(), soundtrackDir, newDirHash)
}
} else {
id = insertSoundtrackNew(file.Name(), soundtrackDir, dirHash)
}
id = insertSoundtrack(file.Name(), soundtrackDir, dirHash)
logging.GetLogger().Debug("New soundtrack detected",
zap.Int32("id", id),
zap.String("soundtrack", file.Name()),
zap.String("hash", dirHash),
zap.String("status", status.String()))
soundtracksAdded = append(soundtracksAdded, file.Name())
newCheckSongs(entries, soundtrackDir, id)
checkSongs(entries, soundtrackDir, id)
case SoundtrackChanged:
logging.GetLogger().Debug("Soundtrack changed",
zap.Int32("id", id),
@@ -377,7 +345,7 @@ func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string,
err = repo.UpdateSoundtrackHash(BackendCtx(), repository.UpdateSoundtrackHashParams{Hash: dirHash, ID: id})
handleError("UpdateSoundtrackHash", err, "")
soundtracksChangedContent = append(soundtracksChangedContent, file.Name())
newCheckSongs(entries, soundtrackDir, id)
checkSongs(entries, soundtrackDir, id)
case TitleChanged:
logging.GetLogger().Debug("Soundtrack title changed",
zap.Int32("id", id),
@@ -387,7 +355,7 @@ func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string,
zap.String("status", status.String()))
err = repo.UpdateSoundtrackName(BackendCtx(), repository.UpdateSoundtrackNameParams{Name: file.Name(), Path: soundtrackDir, ID: id})
handleError("UpdateSoundtrackName", err, "")
newCheckSongs(entries, soundtrackDir, id)
checkSongs(entries, soundtrackDir, id)
if soundtracksChangedTitle == nil {
soundtracksChangedTitle = make(map[string]string)
}
@@ -405,7 +373,7 @@ func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string,
}
}
if !found {
newCheckSongs(entries, soundtrackDir, id)
checkSongs(entries, soundtrackDir, id)
soundtracksReAdded = append(soundtracksReAdded, file.Name())
logging.GetLogger().Debug("Soundtrack added again",
zap.Int32("id", id),
@@ -430,7 +398,7 @@ func syncSoundtrackNew(file os.DirEntry, foldersToSkip []string, baseDir string,
zap.Int("percent", int((foldersSynced/numberOfFoldersToSync)*100)))
}
func insertSoundtrackNew(name string, path string, hash string) int32 {
func insertSoundtrack(name string, path string, hash string) int32 {
var duplicateError = errors.New("ERROR: duplicate key value violates unique")
id, err := repo.InsertSoundtrack(BackendCtx(), repository.InsertSoundtrackParams{SoundtrackName: name, Path: path, Hash: hash})
handleError("InsertSoundtrack", err, "")
@@ -440,14 +408,14 @@ func insertSoundtrackNew(name string, path string, hash string) int32 {
logging.GetLogger().Debug("Resetting soundtrack ID sequence")
_, err = repo.ResetSoundtrackIdSeq(BackendCtx())
handleError("ResetSoundtrackIdSeq", err, "")
id = insertSoundtrackNew(name, path, hash)
id = insertSoundtrack(name, path, hash)
}
}
return id
}
func newCheckSongs(entries []os.DirEntry, soundtrackDir string, id int32) int32 {
func checkSongs(entries []os.DirEntry, soundtrackDir string, id int32) int32 {
//hasher := md5.New()
var numberOfSongs int32
numberOfFiles := len(entries)
@@ -457,7 +425,7 @@ func newCheckSongs(entries []os.DirEntry, soundtrackDir string, id int32) int32
for _, entry := range entries {
poolSong.Submit(func() {
defer songWg.Done()
if newCheckSong(entry, soundtrackDir, id) {
if checkSong(entry, soundtrackDir, id) {
numberOfSongs++
}
})
@@ -466,7 +434,7 @@ func newCheckSongs(entries []os.DirEntry, soundtrackDir string, id int32) int32
return numberOfSongs
}
func newCheckSong(entry os.DirEntry, soundtrackDir string, id int32) bool {
func checkSong(entry os.DirEntry, soundtrackDir string, id int32) bool {
fileInfo, err := entry.Info()
if err != nil {
logging.GetLogger().Error("Failed to get file info", zap.String("filename", entry.Name()), zap.String("error", err.Error()))
@@ -570,7 +538,7 @@ func getHashForFile(path string) string {
return hex.EncodeToString(hasher.Sum(nil))
}
func getIdFromFileNew(file os.FileInfo) int32 {
func getIdFromFile(file os.FileInfo) int32 {
name := file.Name()
if !file.IsDir() && strings.HasSuffix(name, ".id") {
name = strings.Replace(name, ".id", "", 1)