Exemple #1
0
        private Result RemoveFromLikedPlaylist(RemoveFromPlaylistRequest request)
        {
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                if (request.Item.IsTrack)
                {
                    var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);
                    if (track == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Track not found"
                               }
                    }
                    ;
                    track.LikeStatus = LikeStatus.None;
                    mgr.Set(track);
                    lastFmManager.LoveTrackOnLastFM(DateTime.MinValue, track, false);
                    return(new Result()
                    {
                        Success = true
                    });
                }
                else
                {
                    var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                    if (album == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Album not found"
                               }
                    }
                    ;

                    var tracks = mgr.FindTracks(null, Domain.Objects.Playlist.LIKED_ID, album.Id, 0, int.MaxValue);
                    foreach (var trackItem in tracks)
                    {
                        var track = mgr.Get <Domain.Objects.Track>(trackItem.Id);

                        if (track == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track not found"
                                   }
                        }
                        ;

                        track.LikeStatus = LikeStatus.None;
                        mgr.Set(track);
                        lastFmManager.LoveTrackOnLastFM(DateTime.MinValue, track, true);
                    }
                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
        }
        public Result UpdatePlayerPlayingStatus([FromBody] UpdatePlayerPlayingStatusRequest request)
        {
            try
            {
                if (Player.Instance.CurrentPlaylist.CurrentTrack != null &&
                    Player.Instance.CurrentPlaylist.CurrentTrack.Id == int.Parse(request.TrackId))
                {
                    Domain.Objects.Track track;
                    using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                        track = mgr.Get <Domain.Objects.Track>(int.Parse(request.TrackId));

                    if (track == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Track not found "
                               }
                    }
                    ;

                    if (request.IsPlaying)
                    {
                        // started playing
                        if (!Player.Instance.CurrentPlaylist.CurrentTrackStartedPlaying)
                        {
                            Player.Instance.CurrentPlaylist.CurrentTrackStartedPlaying   = true;
                            Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn = DateTime.UtcNow;
                        }

                        lastFmManager.UpdateNowPlayingToLastFM(track);
                    }
                    else
                    {
                        // paused/stopped playing
                    }

                    return(new Result()
                    {
                        Success = true
                    });
                }
                else
                {
                    return(new Result()
                    {
                        Success = false,
                        Message = "Current track does not match specified track id"
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }
        private void UpdateCurrentTrackPlayed(string currentTrackId, bool playedToEnd)
        {
            // todo update current track details before moving on
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                var track = mgr.Get <Domain.Objects.Track>(int.Parse(currentTrackId));

                if (track == null)
                {// silently skip
                    Console.Error.WriteLine("Unable to update current track nr played because id '" + currentTrackId + "' was not found");
                    return;
                }

                if (track.Id == Player.Instance.CurrentPlaylist.CurrentTrack.Id && Player.Instance.CurrentPlaylist.CurrentTrackStartedPlaying)
                {
                    bool wasPlayedToEnd = playedToEnd;
                    if (playedToEnd)
                    {
                        // but is it really?, it's possible that it was just seeking to the end
                        var elapsedDuration = (DateTime.UtcNow - Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn).TotalSeconds;
                        if (track.Duration != 0 && elapsedDuration / track.Duration < 0.5)
                        {
                            // less than 50% of the track duration was passed, assume it was seeked to the end and don't update nr of played to the end
                            wasPlayedToEnd = false;
                        }
                    }

                    // only if the track actually started playing
                    track.NrPlayed++;

                    if (wasPlayedToEnd)
                    {
                        track.NrPlayedToEnd++;
                    }

                    // save a scrobble
                    Domain.Objects.Scrobble scrobble = new Domain.Objects.Scrobble()
                    {
                        TrackId     = track.Id,
                        On          = DateTime.UtcNow,
                        PlayedToEnd = wasPlayedToEnd
                    };
                    mgr.Set(scrobble);
                    // and notify last fm if played to end
                    if (wasPlayedToEnd)
                    {
                        lastFmManager.ScrobbleToLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track);
                    }

                    mgr.Set(track);
                }
            }
        }
        public GetDetailsResult GetDetails(GetDetailsRequest request)
        {
            try
            {
                Domain.Objects.Track track;

                List <Domain.Objects.Scrobble> scrobbles;
                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    track     = mgr.Get <Domain.Objects.Track>(int.Parse(request.TrackId));
                    scrobbles = mgr.GetScrobblesOfTrack(track.Id);
                }

                if (track == null)
                {
                    return new GetDetailsResult()
                           {
                               Success = false, Message = "Track not found"
                           }
                }
                ;

                return(new GetDetailsResult()
                {
                    Success = true,
                    Details = new TrackDetails()
                    {
                        Id = track.Id + "",
                        Title = track.Title,
                        Album = track.Album,
                        Artists = track.Artists?.Split(','),
                        Genres = System.Text.RegularExpressions.Regex.Split(track.Genres + "", "[,/]"),
                        TrackNr = track.TrackNr,
                        AddedOn = track.AddedOn,
                        LastPlayed = track.LastPlayed == DateTime.MinValue ? null : (DateTime?)track.LastPlayed,
                        Liked = track.LikeStatus,
                        NrPlayed = track.NrPlayed,
                        NrPlayedToEnd = track.NrPlayedToEnd,
                        LastScrobbles = scrobbles.OrderByDescending(s => s.On).Select(s => new TrackScrobble()
                        {
                            On = s.On,
                            PlayedToEnd = s.PlayedToEnd
                        }).ToArray()
                    }
                });
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <GetDetailsResult>(ex));
            }
        }
        public IActionResult GetArt(int trackId)
        {
            if (trackId == 0)
            {
                return(NotFound());
            }

            Domain.Objects.Track track;
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                track = mgr.Get <Domain.Objects.Track>(trackId);

            if (track == null)
            {
                return(NotFound());
            }

            string path = System.IO.Path.Combine(settings.Value.RootDirectory, track.Filename);

            using (var tlfile = TagLib.File.Create(path))
            {
                TagLib.Tag tag123 = tlfile.Tag;

                if (tag123.Pictures != null && tag123.Pictures.Length > 0)
                {
                    return(new FileContentResult(tag123.Pictures[0].Data.ToArray(), tag123.Pictures[0].MimeType));
                }
                else
                {
                    // check if cover file exists
                    string dir       = System.IO.Path.GetDirectoryName(path);
                    string coverpath = System.IO.Path.Combine(dir, "cover.jpg");
                    string mimetype  = "image/jpg";
                    if (!System.IO.File.Exists(coverpath))
                    {
                        coverpath = System.IO.Path.Combine(dir, "cover.png");

                        mimetype = "image/png";
                    }

                    if (System.IO.File.Exists(coverpath))
                    {
                        return(new FileContentResult(System.IO.File.ReadAllBytes(coverpath), mimetype));
                    }
                    else
                    {
                        return(NotFound());
                    }
                }
            }
        }
        public SetLikeStatusRequestResult SetLikeStatus([FromBody] SetLikeStatusRequest request)
        {
            try
            {
                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var track = mgr.Get <Domain.Objects.Track>(int.Parse(request.TrackId));

                    if (track == null)
                    {
                        return new SetLikeStatusRequestResult()
                               {
                                   Success = false, Message = "Track not found "
                               }
                    }
                    ;


                    var oldLikeStatus = track.LikeStatus;
                    var newLikeStatus = request.LikeStatus;
                    track.LikeStatus = request.LikeStatus;
                    mgr.Set(track);

                    // update last fm
                    try
                    {
                        if (oldLikeStatus != LikeStatus.Liked && newLikeStatus == LikeStatus.Liked)
                        {
                            lastFmManager.LoveTrackOnLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track, true);
                        }
                        else if (oldLikeStatus == LikeStatus.Liked && newLikeStatus != LikeStatus.Liked)
                        {
                            lastFmManager.LoveTrackOnLastFM(Player.Instance.CurrentPlaylist.CurrentTrackStartedPlayingOn, track, false);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.Error.WriteLine("Unable to update love status on Last FM: " + ex.Message);
                    }
                }

                return(GetPlayerStateResultFromPlayer <SetLikeStatusRequestResult>());
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <SetLikeStatusRequestResult>(ex));
            }
        }
Exemple #7
0
        public IActionResult GetCover(int id)
        {
            Domain.Objects.Cover cover;
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.CoverDatabasePath))
                cover = mgr.Get <Domain.Objects.Cover>(id);

            if (cover == null)
            {
                return(NotFound());
            }

            if (cover.Data == null)
            {
                return(NoContent()); // TODO?
            }
            return(new FileContentResult(cover.Data, "image/png"));
        }
        public IActionResult GetAudio(int trackId)
        {
            if (trackId == 0)
            {
                return(NotFound());
            }

            Domain.Objects.Track track;
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                track = mgr.Get <Domain.Objects.Track>(trackId);

            if (track == null)
            {
                return(NotFound());
            }

            string filePath = System.IO.Path.Combine(settings.Value.RootDirectory, track.Filename);

            FileInfo fi = new FileInfo(filePath);

            if (fi.LastWriteTime <= GetIfModifiedSince())
            {
                Response.StatusCode  = 304;
                Response.ContentType = "audio/mpeg";
                return(Content(String.Empty));
            }

            FileStream stream = System.IO.File.OpenRead(filePath);

            //var response =  HttpResponse.GetOKResponse(req, "audio/mpeg", stream);

            //HttpResponse response;
            if ((Request.Headers["User-Agent"] + "").Contains("Chrome"))
            {
                //if (Request.Headers.ContainsKey("Range"))
                //    response = PartialHttpResponse.GetPartialResponse(req, "audio/mpeg", stream);
                // TODO ?
                Response.Headers["Accept-Ranges"] = "bytes";
            }

            Response.Headers["X-Content-Duration"] = track.Duration.ToString(System.Globalization.CultureInfo.InvariantCulture);

            return(new FileStreamResult(stream, "audio/mpeg"));
        }
        public GetPlayerStateResult PlayPlaylistNow([FromBody] PlayPlaylistRequest request)
        {
            if (request.PlaylistId == Player.Instance.CurrentPlaylist.Id + "")
            {
                return new GetPlayerStateResult()
                       {
                           Success = false, Message = "Already playing this playlist"
                       }
            }
            ;

            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                if (request.PlaylistId == (Domain.Objects.Playlist.ALL_ID + "") || request.PlaylistId == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    Player.Instance.LoadPlaylist(new Player.Playlist()
                    {
                        Id     = int.Parse(request.PlaylistId), // all or liked so no fetching of playlist object
                        Tracks = mgr.GetTrackItems(int.Parse(request.PlaylistId)),
                    });
                }
                else
                {
                    var playlist = mgr.Get <Domain.Objects.Playlist>(int.Parse(request.PlaylistId));

                    if (playlist == null)
                    {
                        return new GetPlayerStateResult()
                               {
                                   Success = false, Message = "Playlist not found"
                               }
                    }
                    ;

                    Player.Instance.LoadPlaylist(new Player.Playlist()
                    {
                        Id     = playlist.Id,
                        Tracks = mgr.GetTrackItems(playlist.Id),
                    });
                }
            }
            return(GetPlayerStateResultFromPlayer <GetPlayerStateResult>());
        }
Exemple #10
0
        public Result RemovePlaylist([FromBody] RemovePlaylistRequest request)
        {
            try
            {
                if (request.Playlist.Id == (Domain.Objects.Playlist.ALL_ID + "") || request.Playlist.Id == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    return new Result()
                           {
                               Success = false, Message = "Can't delete the [All] or [Liked] playlist"
                           }
                }
                ;

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var playlist = mgr.Get <Domain.Objects.Playlist>(int.Parse(request.Playlist.Id));

                    if (playlist == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Playlist not found"
                               }
                    }
                    ;

                    mgr.Delete <Domain.Objects.Playlist>(playlist);
                    mgr.DeleteAllTracksFromPlaylist(playlist.Id);

                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }
Exemple #11
0
        public Result AddPlaylist([FromBody] AddPlaylistRequest request)
        {
            try
            {
                if (request.Playlist.Id == (Domain.Objects.Playlist.ALL_ID + "") || request.Playlist.Id == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    return new Result()
                           {
                               Success = false, Message = "Can't change the [All] or [Liked] playlist"
                           }
                }
                ;

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var playlist = request.Playlist.Id == "" ? null : mgr.Get <Domain.Objects.Playlist>(int.Parse(request.Playlist.Id));

                    if (playlist == null)
                    {
                        playlist = new Domain.Objects.Playlist();
                    }

                    playlist.Name = request.Playlist.Name;

                    mgr.Set(playlist);

                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }
        private T GetPlayerStateResultFromPlayer <T>()
            where T : GetPlayerStateResult, new()
        {
            Domain.Objects.Track track;
            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                track = Player.Instance.CurrentPlaylist.CurrentTrack == null ? null : mgr.Get <Domain.Objects.Track>(Player.Instance.CurrentPlaylist.CurrentTrack.Id);

            return(new T()
            {
                Player = new PlayerState()
                {
                    CurrentTrack = track == null ? null : new Track()
                    {
                        Id = track.Id + "",
                        Title = track.Title,
                        Artists = track.Artists?.Split(","),
                        Album = track.Album,
                        TrackNr = track.TrackNr,
                        Liked = track.LikeStatus,

                        Url = Url.Action("GetAudio", new { trackId = track.Id }),
                        ArtImage = Url.Action("GetArt", new { trackId = track.Id })
                    },

                    Shuffle = Player.Instance.CurrentPlaylist.IsShuffled
                },

                Success = true
            });
        }
        public PlayNowResult PlayAlbumOrTrackNow([FromBody] PlayAlbumOrTrackRequest request)
        {
            // TODO
            int playlistId;

            if (string.IsNullOrEmpty(request.PlaylistId))
            {
                playlistId = Domain.Objects.Playlist.ALL_ID; // all
            }
            else
            {
                playlistId = int.Parse(request.PlaylistId);
            }

            using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
            {
                if (playlistId != Player.Instance.CurrentPlaylist.Id)
                {
                    PlayPlaylistNow(new PlayPlaylistRequest()
                    {
                        PlaylistId = playlistId + ""
                    });
                }

                // find in the current playlist and change track

                if (request.Item.IsTrack)
                {
                    var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);

                    if (track == null)
                    {
                        return new PlayNowResult()
                               {
                                   Success = false, Message = "Track not found "
                               }
                    }
                    ;

                    Player.Instance.PlayTracks(new HashSet <int>()
                    {
                        request.Item.Id
                    });
                }
                else
                {
                    var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                    if (album == null)
                    {
                        return new PlayNowResult()
                               {
                                   Success = false, Message = "Album not found"
                               }
                    }
                    ;

                    var tracks = mgr.FindTracks(null, playlistId, album.Id, 0, int.MaxValue);

                    Player.Instance.PlayTracks(new HashSet <int>(tracks.Select(t => t.Id)));
                }
            }

            return(GetPlayerStateResultFromPlayer <PlayNowResult>());
        }
Exemple #14
0
        public Result RemoveFromPlaylist([FromBody] RemoveFromPlaylistRequest request)
        {
            try
            {
                if (request.PlaylistId == (Domain.Objects.Playlist.ALL_ID + ""))
                {
                    return new Result()
                           {
                               Success = false, Message = "Can't remove tracks from the [All] or [Liked] playlist"
                           }
                }
                ;
                else if (request.PlaylistId == (Domain.Objects.Playlist.LIKED_ID + ""))
                {
                    return(RemoveFromLikedPlaylist(request));
                }

                using (DAL.DALManager mgr = new DAL.DALManager(settings.Value.DatabasePath))
                {
                    var playlist = mgr.Get <Domain.Objects.Playlist>(int.Parse(request.PlaylistId));

                    if (playlist == null)
                    {
                        return new Result()
                               {
                                   Success = false, Message = "Playlist not found"
                               }
                    }
                    ;

                    int nrOfItemsRemoved = 0;
                    if (request.Item.IsTrack)
                    {
                        var track = mgr.Get <Domain.Objects.Track>(request.Item.Id);

                        if (track == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track not found"
                                   }
                        }
                        ;

                        var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);
                        if (playlistTrack == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Track is was not in the playlist"
                                   }
                        }
                        ;

                        mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                        nrOfItemsRemoved++;
                    }
                    else
                    {
                        var album = mgr.Get <Domain.Objects.Album>(request.Item.Id);
                        if (album == null)
                        {
                            return new Result()
                                   {
                                       Success = false, Message = "Album not found"
                                   }
                        }
                        ;

                        var tracks = mgr.FindTracks(null, Domain.Objects.Playlist.ALL_ID, album.Id, 0, int.MaxValue);
                        foreach (var track in tracks)
                        {
                            var playlistTrack = mgr.GetPlaylistTrack(playlist.Id, track.Id);

                            if (playlistTrack != null)
                            {
                                mgr.RemoveFromPlaylist(playlist.Id, track.Id);
                                nrOfItemsRemoved++;
                            }
                        }
                    }
                    playlist.NrOfTracks -= nrOfItemsRemoved;
                    mgr.Set <Domain.Objects.Playlist>(playlist);

                    return(new Result()
                    {
                        Success = true
                    });
                }
            }
            catch (Exception ex)
            {
                return(GetErrorResultFromException <Result>(ex));
            }
        }