mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-19 01:44:19 +00:00
Implement eraze command to delete song files and db collections
This commit is contained in:
parent
3ffa381e2d
commit
cf82256666
3 changed files with 63 additions and 2 deletions
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"song-recognition/shazam"
|
"song-recognition/shazam"
|
||||||
"song-recognition/spotify"
|
"song-recognition/spotify"
|
||||||
"song-recognition/utils"
|
"song-recognition/utils"
|
||||||
|
|
@ -188,3 +190,51 @@ func serveHTTP(socketServer *socketio.Server, serveHTTPS bool, port string) {
|
||||||
log.Fatalf("HTTP server ListenAndServe: %v", err)
|
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")
|
||||||
|
}
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -8,7 +8,7 @@ import (
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) < 2 {
|
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)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -33,8 +33,10 @@ func main() {
|
||||||
port := serveCmd.String("p", "5000", "Port to use")
|
port := serveCmd.String("p", "5000", "Port to use")
|
||||||
serveCmd.Parse(os.Args[2:])
|
serveCmd.Parse(os.Args[2:])
|
||||||
serve(*protocol, *port)
|
serve(*protocol, *port)
|
||||||
|
case "erase":
|
||||||
|
erase(SONGS_DIR)
|
||||||
default:
|
default:
|
||||||
fmt.Println("Expected 'find', 'download', or 'serve' subcommands")
|
fmt.Println("Expected 'find', 'download', 'erase', or 'serve' subcommands")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -214,3 +214,12 @@ func (db *DbClient) DeleteSongByID(songID uint32) error {
|
||||||
|
|
||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue