Example #1
0
File: MediaDB.cs Project: XCVG/XSMP
        /// <summary>
        /// Gets a list of songs in a folder, non-recursive
        /// </summary>
        public IReadOnlyList <PublicModels.Song> GetSongsInFolder(string folderPath)
        {
            ThrowIfNotReady();

            SHA1 sha1 = SHA1.Create();

            string firstPathPart = MediaUtils.GetFirstPathElement(folderPath);
            string realPath      = MediaUtils.ReplaceFirstPathElement(folderPath, MediaFolderUniquePaths[firstPathPart]);

            var files = Directory.EnumerateFiles(realPath);

            List <PublicModels.Song> songs = new List <PublicModels.Song>();

            foreach (var file in files)
            {
                var    hashBytes = sha1.ComputeHash(File.ReadAllBytes(file));
                string hash      = HashUtils.BytesToHexString(hashBytes);

                var rawSong = DBContext.Song.Find(hash);
                if (rawSong != null)
                {
                    var song = PublicModels.Song.FromDBObject(rawSong, DBContext);
                    songs.Add(song);
                }
            }

            return(songs);
        }
Example #2
0
File: MediaDB.cs Project: XCVG/XSMP
        /// <summary>
        /// Gets a list of subfolders within a media folder
        /// </summary>
        public IReadOnlyList <string> GetFoldersInFolder(string folderPath)
        {
            ThrowIfNotReady();

            string firstPathPart = MediaUtils.GetFirstPathElement(folderPath);
            string realPath      = MediaUtils.ReplaceFirstPathElement(folderPath, MediaFolderUniquePaths[firstPathPart]);

            return(Directory.EnumerateDirectories(realPath).Select(d => new DirectoryInfo(d).Name).ToImmutableArray());
        }
Example #3
0
File: MediaDB.cs Project: XCVG/XSMP
        public IReadOnlyList <PublicModels.Song> GetAlbumSongs(string cname)
        {
            ThrowIfNotReady();

            var(artistCName, albumCName) = MediaUtils.SplitAlbumCName(cname);
            var rawSongs = from song in DBContext.Song
                           where song.AlbumArtistName == artistCName && song.AlbumName == albumCName
                           orderby song.Track ascending
                           select song;

            var songs = rawSongs.ToArray().Select(s => PublicModels.Song.FromDBObject(s, DBContext));

            return(songs.ToList());
        }
Example #4
0
File: MediaDB.cs Project: XCVG/XSMP
        public PublicModels.Album?GetAlbum(string cname)
        {
            ThrowIfNotReady();

            var(artistCName, albumCName) = MediaUtils.SplitAlbumCName(cname);
            var rawAlbums = from album in DBContext.Album
                            where album.ArtistName == artistCName && album.Name == albumCName
                            orderby album.Name ascending
                            select album;

            var rawAlbumArray = rawAlbums.ToArray();

            if (rawAlbumArray.Length > 0)
            {
                return(PublicModels.Album.FromDBObject(rawAlbumArray[0], DBContext));
            }

            return(null);
        }
Example #5
0
File: MediaDB.cs Project: XCVG/XSMP
        /// <summary>
        /// Gets if a media folder exists and is valid
        /// </summary>
        public bool GetFolderExists(string folderPath)
        {
            ThrowIfNotReady();

            //so the idea is:
            //check if the first segment matches one of our root paths. Fail if it doesn't. Replace with real path if it does
            string firstPathPart = MediaUtils.GetFirstPathElement(folderPath);

            if (!MediaFolderUniquePaths.ContainsKey(firstPathPart))
            {
                return(false);
            }
            //check if the real path exists on disk. Success if it does
            string realPath = MediaUtils.ReplaceFirstPathElement(folderPath, MediaFolderUniquePaths[firstPathPart]);

            if (Directory.Exists(realPath))
            {
                return(true);
            }

            return(false);
        }
Example #6
0
File: MediaDB.cs Project: XCVG/XSMP
        /// <summary>
        /// Gets a unique cname for a (new) playlist
        /// </summary>
        public string GetPlaylistUniqueName(string name)
        {
            ThrowIfNotReady();

            var cname = MediaUtils.GetCanonicalName(name);

            //easy case: cname does not exist in playlist list
            if (!Playlists.Keys.Contains(cname))
            {
                return(cname);
            }
            else
            {
                //contains at least one partial match
                int highestNumber = 0;
                foreach (var key in Playlists.Keys)
                {
                    if (key.StartsWith(cname))
                    {
                        var ks = key.Split('_');
                        if (ks.Length > 1)
                        {
                            int keyNumber = int.Parse(ks[1]);
                            if (keyNumber > highestNumber)
                            {
                                highestNumber = keyNumber;
                            }
                        }
                    }
                }

                var newNumber = highestNumber + 1;

                return($"{cname}_{newNumber}");
            }
        }
Example #7
0
        private static void AddSong(SongInfo song, mediadbContext dbContext)
        {
            //check to see if artists exit and add them if they do not
            //var artistsCNames = song.Artists.Select(a => new KeyValuePair<string, string>(MediaUtils.GetCanonicalName(a), a)).Distinct().ToDictionary(kvp => kvp.Key,;
            var artistsCNames = new Dictionary <string, string>();

            foreach (var artist in song.Artists)
            {
                string artistCName = MediaUtils.GetCanonicalName(artist);
                if (artistsCNames.ContainsKey(artistCName))
                {
                    continue;
                }
                artistsCNames.Add(artistCName, artist);
            }

            foreach (var kvp in artistsCNames)
            {
                string artistCName = kvp.Key;
                string artistName  = kvp.Value;

                if (string.IsNullOrEmpty(artistCName))
                {
                    continue;
                }

                if (dbContext.Artist.Where(a => a.Name == artistCName).Count() == 0)
                {
                    var artist = new Artist()
                    {
                        Name = artistCName, NiceName = artistName
                    };
                    dbContext.Artist.Add(artist);
                }
            }

            //check if album artist exists and add it if it does not
            string albumArtistCName = MediaUtils.GetCanonicalName(song.AlbumArtistName);

            if (!string.IsNullOrEmpty(albumArtistCName) && !artistsCNames.ContainsKey(albumArtistCName))
            {
                if (dbContext.Artist.Where(a => a.Name == albumArtistCName).Count() == 0)
                {
                    var artist = new Artist()
                    {
                        Name = albumArtistCName, NiceName = song.AlbumArtistName
                    };
                    dbContext.Artist.Add(artist);
                }
            }

            //check if album exists and add it if it does not
            string albumCName = MediaUtils.GetCanonicalName(song.AlbumName);

            if (!string.IsNullOrEmpty(albumCName) && !string.IsNullOrEmpty(albumArtistCName))
            {
                if (dbContext.Album.Where(a => a.Name == albumCName && a.ArtistName == albumArtistCName).Count() == 0)
                {
                    var album = new Album()
                    {
                        Name = albumCName, ArtistName = albumArtistCName, Title = song.AlbumName
                    };
                    dbContext.Album.Add(album);
                }
            }

            //genre should be a cname
            string genreCName = MediaUtils.GetCanonicalName(song.Genre);

            //create song object and add!
            var songObject = new Song()
            {
                Hash            = song.Hash,
                Title           = song.Title,
                Genre           = genreCName,
                Length          = song.Length,
                Set             = song.Set,
                Track           = song.Track,
                Path            = song.Path,
                AlbumName       = albumCName,
                AlbumArtistName = albumArtistCName
            };

            dbContext.Song.Add(songObject);

            //create artist-song link objects
            foreach (var artistCName in artistsCNames.Keys)
            {
                var artistsong = new ArtistSong()
                {
                    ArtistName = artistCName, SongHash = song.Hash
                };
                dbContext.ArtistSong.Add(artistsong);
            }
        }