private void Scan(int parentId, ref int numFilesAdded, DirectoryInfo info) { MediaLibraryEntry directoryEntry = GetOrCreateDirectoryEntry(parentId, info); IEnumerable <FileSystemInfo> fileSystemEntries = info.EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly); HashSet <string> albumNames = new HashSet <string>(); foreach (FileSystemInfo info2 in fileSystemEntries) { if (info2 is DirectoryInfo directoryInfo) { if (info2.Name != "__MACOSX") { Scan(directoryEntry.Id, ref numFilesAdded, directoryInfo); } } else if (info2.Name.EndsWith(".mp3")) { bool added = AddOrUpdateFileEntry(directoryEntry.Id, (FileInfo)info2, albumNames); if (added) { numFilesAdded++; } } } if (albumNames.Count == 1) { directoryEntry.Name = albumNames.Single(); Library.UpdateEntry(directoryEntry); } }
public void UpdateLastPlayed(int id) { DateTime now = DateTime.UtcNow; using (var context = new SqliteDbContext(settings)) { MediaLibraryEntry entry = context.LibraryEntries .Where(e => e.Id == id) .SingleOrDefault(); entry.LastPlayedUtc = now; // If this is a file, also update the parent folder if (!entry.IsFolder) { entry = context.LibraryEntries .Where(e => e.Id == entry.ParentId) .SingleOrDefault(); entry.LastPlayedUtc = now; } context.SaveChanges(); } }
public void UpdateEntry(MediaLibraryEntry entry) { using (var context = new SqliteDbContext(settings)) { context.Update(entry); context.SaveChanges(); } }
public FileStreamResult StreamFile(int id) { MediaLibraryEntry entry = Index.GetFile(id); Index.UpdateLastPlayed(id); return(new FileStreamResult(entry.OpenReadStream(), "audio/mp3")); }
public void RemoveEntry(MediaLibraryEntry entry) { Entries.Remove(entry); entriesById.Remove(entry); entriesByPath.Remove(entry); entriesByParentId.Remove(entry); entriesByIsFolder.Remove(entry); }
public int AddEntry(MediaLibraryEntry entry) { using (var context = new SqliteDbContext(settings)) { context.Add(entry); context.SaveChanges(); return(entry.Id); } }
public ActionResult GetCoverArt(int id) { MediaLibraryEntry entry = Index.GetEntry(id); bool originalIsFolder = entry.IsFolder; if (!originalIsFolder) { using (var id3File = TagLib.File.Create(entry.Path)) { TagLib.IPicture image = id3File.Tag.Pictures .OrderByDescending(i => i.Type == TagLib.PictureType.FrontCover) .FirstOrDefault(); if (image != null) { return(new FileContentResult(image.Data.Data, image.MimeType)); } } // Nothing in the MP3 file, so let's try to get an image from the parent folder entry = Index.GetFolder(entry.ParentId); } Debug.Assert(entry.IsFolder); FileInfo file = new DirectoryInfo(entry.Path) .EnumerateFiles("*.jpg") .FirstOrDefault(); if (file == null) { // If the request was for a folder and there is no image file, // try to find an image in the MP3 files if (originalIsFolder) { entry = Index.GetChildFiles(entry.Id) .FirstOrDefault(); using (var id3File = TagLib.File.Create(entry.Path)) { TagLib.IPicture image = id3File.Tag.Pictures .OrderByDescending(i => i.Type == TagLib.PictureType.FrontCover) .FirstOrDefault(); if (image != null) { return(new FileContentResult(image.Data.Data, image.MimeType)); } } // Nothing in the MP3 file either } return(new FileContentResult(Array.Empty <byte>(), "image/jpeg")); } return(new FileStreamResult(file.OpenRead(), "image/jpeg")); }
public MediaLibraryEntry GetRootFolderFor(MediaLibraryEntry dir) { MediaLibraryEntry rootDir = dir; while (rootDir.ParentId >= 0) { rootDir = entriesById.Get(rootDir.ParentId); } return(rootDir); }
public int AddEntry(MediaLibraryEntry entry) { entry.Id = Interlocked.Increment(ref identity); Entries.Add(entry); entriesById.Add(entry); entriesByPath.Add(entry); entriesByIsFolder.Add(entry); entriesByParentId.Add(entry); return(entry.Id); }
public void UpdateLastPlayed(int id) { DateTime now = DateTime.UtcNow; MediaLibraryEntry entry = GetEntry(id); entry.LastPlayedUtc = now; // If this is a file, also update the parent folder if (!entry.IsFolder) { entry = GetEntry(entry.ParentId); entry.LastPlayedUtc = now; } }
public MediaLibraryEntry GetRootFolderFor(MediaLibraryEntry dir) { if (dir == null) { return(null); } using (var context = new SqliteDbContext(settings)) { MediaLibraryEntry rootDir = dir; while (rootDir.ParentId >= 0) { rootDir = context.LibraryEntries .Where(e => e.Id == rootDir.ParentId) .SingleOrDefault(); } return(rootDir); } }
private MediaLibraryEntry GetOrCreateDirectoryEntry(int parentId, DirectoryInfo info) { MediaLibraryEntry entry = Library.GetEntryByPath(info.FullName); if (entry == null) { entry = new MediaLibraryEntry { ParentId = parentId, Path = info.FullName, Name = info.Name, IsFolder = true, AddedUtc = DateTime.UtcNow, }; Logger.LogInformation("Scan: Adding directory: {0}", info.FullName); Library.AddEntry(entry); } return(entry); }
public ActionResult <Response> GetMusicDirectory(int id) { MediaLibraryEntry dir = Index.GetFolder(id); MediaLibraryEntry rootDir = Index.GetRootFolderFor(id); return(new Response { Item = new Directory { id = dir.Id.ToString(CultureInfo.InvariantCulture), name = dir.Name, parent = dir.ParentId.ToString(CultureInfo.InvariantCulture), child = Index.GetChildEntries(id) .Select(i => new Child { id = i.Id.ToString(CultureInfo.InvariantCulture), parent = i.ParentId.ToString(CultureInfo.InvariantCulture), artist = i.Artist, title = i.Name, album = dir.Name, isDir = i.IsFolder, track = i.TrackNumber ?? 0, trackSpecified = i.TrackNumber.HasValue, coverArt = (i.IsFolder ? i.Id : i.ParentId).ToString(CultureInfo.InvariantCulture), duration = i.Duration.HasValue ? (int)System.Math.Ceiling(i.Duration.Value.TotalSeconds) : 0, durationSpecified = i.Duration.HasValue, path = System.IO.Path.GetRelativePath(rootDir.Path, i.Path), }) .OrderBy(i => i.trackSpecified) .ThenBy(i => i.album) .ThenBy(i => i.track) .ThenBy(i => i.title) .ToArray(), }, ItemElementName = ItemChoiceType.directory, version = "1.14.0", }); }
public void UpdateEntry(MediaLibraryEntry entry) { RemoveEntry(entry); AddEntry(entry); }
public MediaLibraryEntry GetFolder(int id) { MediaLibraryEntry item = entriesById.Get(id); return(item.IsFolder ? item : null); }
private bool AddOrUpdateFileEntry(int parentId, FileInfo info, HashSet <string> albumNames) { string artist = null; string title = null; int? trackNumber = null; TimeSpan?duration = null; using (var file = TagLib.File.Create(info.FullName)) { if (file.Tag != null) { title = file.Tag.Title ?? Path.GetFileNameWithoutExtension(info.Name); trackNumber = file.Tag.Track > 0 ? (int)file.Tag.Track : (int?)null; string album = file.Tag.Album?.Trim(); if (!string.IsNullOrWhiteSpace(album)) { albumNames.Add(album); } artist = !string.IsNullOrWhiteSpace(file.Tag.JoinedAlbumArtists) ? file.Tag.JoinedAlbumArtists?.Trim() : !string.IsNullOrWhiteSpace(file.Tag.FirstAlbumArtist) ? file.Tag.FirstAlbumArtist?.Trim() : !string.IsNullOrWhiteSpace(file.Tag.FirstPerformer) ? file.Tag.FirstPerformer?.Trim() : #pragma warning disable CS0618 // Type or member is obsolete file.Tag.Artists?.Length > 0 ? string.Join('/', file.Tag.Artists) : null; #pragma warning restore CS0618 // Type or member is obsolete duration = file.Properties.Duration; } } bool added; MediaLibraryEntry existingEntry = Library.GetEntryByPath(info.FullName); MediaLibraryEntry newEntry; if (existingEntry == null) { Logger.LogInformation("Scan: Adding file: {0}", info.FullName); added = true; newEntry = new MediaLibraryEntry { IsFolder = false, ParentId = parentId, Path = info.FullName, AddedUtc = DateTime.UtcNow, }; } else { newEntry = existingEntry.Copy(); added = false; } newEntry.Name = title; newEntry.TrackNumber = trackNumber; newEntry.Artist = artist; newEntry.Duration = duration; if (existingEntry != null) { if (existingEntry.Equals(newEntry)) { return(false); } Library.RemoveEntry(existingEntry); } Library.AddEntry(newEntry); return(added); }
public MediaLibraryEntry GetFile(int id) { MediaLibraryEntry item = entriesById.Get(id); return(item.IsFolder ? null : item); }