/// <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); }
private static SongInfo ReadSongInfo(string songPath) { //calcumalate hash var hashBytes = Sha1.ComputeHash(System.IO.File.ReadAllBytes(songPath)); string hash = HashUtils.BytesToHexString(hashBytes); var tagFile = TagLib.File.Create(songPath); var tags = tagFile.Tag; //Console.WriteLine(tagFile.Properties.Duration); double length = tagFile.Properties.Duration.TotalSeconds; //expect NULL or ZERO if the tag does not exists //do we want to scrub names at this point? no, I think we need the full ones string title = string.IsNullOrEmpty(tags.Title) ? Path.GetFileNameWithoutExtension(songPath) : tags.Title; int track = (int)tags.Track; int set = (int)tags.Disc; //WIP handle 1/1 = 0 if (set == 1 && tags.DiscCount == 1) { set = 0; } //handle comma'd and slash'd genre tags string genre = string.IsNullOrEmpty(tags.FirstGenre) ? null : tags.FirstGenre; if (genre != null && genre.Contains(',')) { genre = genre.Substring(0, genre.IndexOf(',')); } if (genre != null && genre.Contains('/')) //not sure if this is exactly what we want since it *is* ambiguous { genre = genre.Substring(0, genre.IndexOf('/')); } //string artist = string.IsNullOrEmpty(tags.FirstPerformer) ? null : tags.FirstPerformer; bool hasArtists = (tags.Performers != null && tags.Performers.Length > 0); var artists = hasArtists ? new List <string>(tags.Performers) : new List <string>() { "Unknown" }; string albumArtist = string.IsNullOrEmpty(tags.FirstAlbumArtist) ? (hasArtists ? artists[0] : null) : tags.FirstAlbumArtist; string album = string.IsNullOrEmpty(tags.Album) ? null : tags.Album; return(new SongInfo() { Hash = hash, Title = title, Length = length, Track = track, Set = set, Genre = genre, Path = songPath, Artists = artists, AlbumName = album, AlbumArtistName = albumArtist }); }