Automatically create songs directory.

This commit is contained in:
Chigozirim Igweamaka 2024-05-18 08:01:45 +01:00
parent b8996a914c
commit 69dd7b8e44
2 changed files with 19 additions and 4 deletions

15
main.go
View file

@ -21,7 +21,7 @@ import (
)
const (
tmpSongDir = "/home/chigozirim/Documents/my-docs/song-recognition/songs/"
SONGS_DIR = "songs"
)
func GinMiddleware(allowOrigin string) gin.HandlerFunc {
@ -77,6 +77,13 @@ func main() {
return nil
})
err := spotify.CreateFolder(SONGS_DIR)
if err != nil {
err := xerrors.New(err)
logMsg := fmt.Sprintf("failed to create directory %v", SONGS_DIR)
logger.ErrorContext(ctx, logMsg, slog.Any("error", err))
}
server.OnEvent("/", "totalSongs", func(socket socketio.Conn) {
db, err := utils.NewDbClient()
if err != nil {
@ -132,7 +139,7 @@ func main() {
statusMsg := fmt.Sprintf("%v songs found in album.", len(tracksInAlbum))
socket.Emit("downloadStatus", downloadStatus("info", statusMsg))
totalTracksDownloaded, err := spotify.DlAlbum(spotifyURL, tmpSongDir)
totalTracksDownloaded, err := spotify.DlAlbum(spotifyURL, SONGS_DIR)
if err != nil {
socket.Emit("downloadStatus", downloadStatus("error", "Couldn't to download album."))
@ -163,7 +170,7 @@ func main() {
statusMsg := fmt.Sprintf("%v songs found in playlist.", len(tracksInPL))
socket.Emit("downloadStatus", downloadStatus("info", statusMsg))
totalTracksDownloaded, err := spotify.DlPlaylist(spotifyURL, tmpSongDir)
totalTracksDownloaded, err := spotify.DlPlaylist(spotifyURL, SONGS_DIR)
if err != nil {
socket.Emit("downloadStatus", downloadStatus("error", "Couldn't download playlist."))
@ -213,7 +220,7 @@ func main() {
logger.ErrorContext(ctx, "failed to get song by key.", slog.Any("error", err))
}
totalDownloads, err := spotify.DlSingleTrack(spotifyURL, tmpSongDir)
totalDownloads, err := spotify.DlSingleTrack(spotifyURL, SONGS_DIR)
if err != nil {
if len(err.Error()) <= 25 {
socket.Emit("downloadStatus", downloadStatus("error", err.Error()))

View file

@ -47,6 +47,14 @@ func DeleteFile(filePath string) {
}
}
func CreateFolder(folderPath string) error {
err := os.MkdirAll(folderPath, 0755)
if err != nil {
return err
}
return nil
}
func SongKeyExists(key string) (bool, error) {
db, err := utils.NewDbClient()
if err != nil {