// used for getting art from a file. private Art CreateArt(string filePath) { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read); // compute the hash of the file stream Art art = new Art(); art.Md5Hash = fs.MD5(); art.FileSize = fs.Length; art.LastModified = System.IO.File.GetLastWriteTime(fs.Name).ToUnixTime(); art.ArtId = Injection.Kernel.Get<IArtRepository>().ArtIdForMd5(art.Md5Hash); art.FilePath = filePath; if ((object)art.ArtId == null) { art.ArtId = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.Art); Injection.Kernel.Get<IArtRepository>().InsertArt(art); } // Dispose file stream fs.Close(); return art; }
// used for getting art from a tag. // We don't set the FilePath here, because that is only used for actual art files on disk private Art CreateArt(TagLib.File file) { Art art = new Art(); if (file.Tag.Pictures.Length > 0) { byte[] data = file.Tag.Pictures[0].Data.Data; art.Md5Hash = data.MD5(); art.FileSize = data.Length; art.LastModified = System.IO.File.GetLastWriteTime(file.Name).ToUnixTime(); art.ArtId = Injection.Kernel.Get<IArtRepository>().ArtIdForMd5(art.Md5Hash); if (art.ArtId == null) { // This art isn't in the database yet, so add it art.ArtId = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.Art); Injection.Kernel.Get<IArtRepository>().InsertArt(art); } } // Close file handle file.Dispose(); return art; }
private Stream CreateStream(Art art) { if ((object)art.ArtId == null) { return null; } int? itemId = Injection.Kernel.Get<IArtRepository>().ItemIdForArtId((int)art.ArtId); if ((object)itemId == null) { return null; } ItemType type = Injection.Kernel.Get<IItemRepository>().ItemTypeForItemId((int)itemId); Stream stream = null; if (type == ItemType.Song) { stream = StreamForSong((int)itemId); } else if (type == ItemType.Folder) { stream = StreamForFolder((int)itemId); } return stream; }