diff --git a/client/src/App.js b/client/src/App.js index 510cf55..41b27b8 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -11,10 +11,9 @@ import "react-toastify/dist/ReactToastify.css"; import { MediaRecorder, register } from "extendable-media-recorder"; import { connect } from "extendable-media-recorder-wav-encoder"; -const backendServer = - process.env.REACT_APP_BACKEND_URL || "http://localhost:5000"; +const server = process.env.REACT_APP_BACKEND_URL || "http://localhost:5000"; -var socket = io(backendServer); +var socket = io(server); function App() { const [stream, setStream] = useState(); @@ -299,7 +298,6 @@ function App() { closeOnClick rtl={false} pauseOnFocusLoss - draggable pauseOnHover theme="light" transition={Slide} diff --git a/client/src/components/CarouselSliders.js b/client/src/components/CarouselSliders.js index 2f8686e..9312473 100644 --- a/client/src/components/CarouselSliders.js +++ b/client/src/components/CarouselSliders.js @@ -6,10 +6,14 @@ const CarouselSliders = (props) => { const [activeVideoID, setActiveVideoID] = useState(null); const players = useRef({}); - const activeVid = props.matches.length ? props.matches[0].YouTubeID : ""; - useEffect(() => { - setActiveVideoID(activeVid); + if (props.matches.length > 0) { + const firstVideoID = props.matches[0].YouTubeID; + document + .getElementById(`slide-${firstVideoID}`) + .scrollIntoView({ behavior: "smooth" }); + setActiveVideoID(firstVideoID); + } }, [props.matches]); const onReady = (event, videoId) => { @@ -72,7 +76,11 @@ const CarouselSliders = (props) => { : `${styles.Link} ${styles.ActiveLink}` } href={`#slide-${match.YouTubeID}`} - onClick={() => { + onClick={(e) => { + e.preventDefault(); + document + .getElementById(`slide-${match.YouTubeID}`) + .scrollIntoView({ behavior: "smooth" }); setActiveVideoID(match.YouTubeID); }} > 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/socketHandlers.go b/socketHandlers.go index 0145acf..ff3668c 100644 --- a/socketHandlers.go +++ b/socketHandlers.go @@ -213,7 +213,7 @@ func handleNewRecording(socket socketio.Conn, recordData string) { logger.ErrorContext(ctx, "failed to convert decodedData to samples.", slog.Any("error", err)) } - /** this operation alters the audio, adding some level of bass to it. + /** this operation alters the audio, adds some level of bass to it. if sampleRate != 44100 { samples, err = shazam.Downsample(samples, sampleRate, 44100) if err != nil { diff --git a/spotify/downloader.go b/spotify/downloader.go index aa7fb8a..8bac30c 100644 --- a/spotify/downloader.go +++ b/spotify/downloader.go @@ -146,8 +146,9 @@ func dlTrack(tracks []Track, path string) (int, error) { return } + DeleteFile(filepath.Join(path, fileName+".m4a")) + if DELETE_SONG_FILE { - DeleteFile(filepath.Join(path, fileName+".m4a")) DeleteFile(filepath.Join(path, fileName+".wav")) } 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 +}