From cf8225666603f7eee37aba28fea82d3ecf43a4a6 Mon Sep 17 00:00:00 2001 From: Chigozirim Igweamaka Date: Sun, 30 Jun 2024 15:00:45 +0100 Subject: [PATCH] Implement eraze command to delete song files and db collections --- cmdHandlers.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++++-- utils/dbClient.go | 9 +++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/cmdHandlers.go b/cmdHandlers.go index 08e393f..4572706 100644 --- a/cmdHandlers.go +++ b/cmdHandlers.go @@ -7,6 +7,8 @@ import ( "log" "log/slog" "net/http" + "os" + "path/filepath" "song-recognition/shazam" "song-recognition/spotify" "song-recognition/utils" @@ -188,3 +190,51 @@ func serveHTTP(socketServer *socketio.Server, serveHTTPS bool, port string) { log.Fatalf("HTTP server ListenAndServe: %v", err) } } + +func erase(songsDir string) { + logger := utils.GetLogger() + ctx := context.Background() + + // wipe db + dbClient, err := utils.NewDbClient() + if err != nil { + msg := fmt.Sprintf("Error creating DB client: %v\n", err) + logger.ErrorContext(ctx, msg, slog.Any("error", err)) + } + + err = dbClient.DeleteCollection("fingerprints") + if err != nil { + msg := fmt.Sprintf("Error deleting collection: %v\n", err) + logger.ErrorContext(ctx, msg, slog.Any("error", err)) + } + + err = dbClient.DeleteCollection("songs") + if err != nil { + msg := fmt.Sprintf("Error deleting collection: %v\n", err) + logger.ErrorContext(ctx, msg, slog.Any("error", err)) + } + + // delete song files + err = filepath.Walk(songsDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if !info.IsDir() { + ext := filepath.Ext(path) + if ext == ".wav" || ext == ".m4a" { + err := os.Remove(path) + if err != nil { + return err + } + } + } + return nil + }) + if err != nil { + msg := fmt.Sprintf("Error walking through directory %s: %v\n", songsDir, err) + logger.ErrorContext(ctx, msg, slog.Any("error", err)) + } + + fmt.Println("Erase successful") +} diff --git a/main.go b/main.go index a2eea4e..5e8a822 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( func main() { if len(os.Args) < 2 { - fmt.Println("Expected 'find', 'download', or 'serve' subcommands") + fmt.Println("Expected 'find', 'download', 'erase', or 'serve' subcommands") os.Exit(1) } @@ -33,8 +33,10 @@ func main() { port := serveCmd.String("p", "5000", "Port to use") serveCmd.Parse(os.Args[2:]) serve(*protocol, *port) + case "erase": + erase(SONGS_DIR) default: - fmt.Println("Expected 'find', 'download', or 'serve' subcommands") + fmt.Println("Expected 'find', 'download', 'erase', or 'serve' subcommands") os.Exit(1) } } diff --git a/utils/dbClient.go b/utils/dbClient.go index fe2b726..0dde6ed 100644 --- a/utils/dbClient.go +++ b/utils/dbClient.go @@ -214,3 +214,12 @@ func (db *DbClient) DeleteSongByID(songID uint32) error { return nil } + +func (db *DbClient) DeleteCollection(collectionName string) error { + collection := db.client.Database("song-recognition").Collection(collectionName) + err := collection.Drop(context.Background()) + if err != nil { + return fmt.Errorf("error deleting collection: %v", err) + } + return nil +}