private Video CreateVideo(string filePath, int? folderId, TagLib.File file) { int? itemId = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.Video); if (itemId == null) { return new Video(); } Video video = new Video(); video.ItemId = itemId; video.FolderId = folderId; video.FileType = video.FileType.FileTypeForTagLibMimeType(file.MimeType); if (video.FileType == FileType.Unknown) { logger.IfInfo("\"" + filePath + "\" Unknown file type: " + file.Properties.Description); } video.Width = file.Properties.VideoWidth; video.Height = file.Properties.VideoHeight; video.Duration = Convert.ToInt32(file.Properties.Duration.TotalSeconds); video.Bitrate = file.Properties.AudioBitrate; // Get filesystem information about file FileInfo fsFile = new FileInfo(filePath); video.FileSize = fsFile.Length; video.LastModified = fsFile.LastWriteTime.ToUnixTime(); video.FileName = fsFile.Name; // Generate an art id from the embedded art, if it exists int? artId = CreateArt(file).ArtId; // Close file handles fsFile = null; file.Dispose(); // If there was no embedded art, use the folder's art artId = (object)artId == null ? Injection.Kernel.Get<IArtRepository>().ArtIdForItemId(video.FolderId) : artId; // Create the art/item relationship Injection.Kernel.Get<IArtRepository>().UpdateArtItemRelationship(artId, video.ItemId, true); return video; }
public Song CreateSong(string filePath, int? folderId, TagLib.File file) { int? itemId = Injection.Kernel.Get<IItemRepository>().GenerateItemId(ItemType.Song); if (itemId == null) { return new Song(); } Song song = new Song(); song.ItemId = itemId; song.FolderId = folderId; // Parse taglib tags TagLib.Tag tag = file.Tag; try { string firstPerformer = tag.FirstPerformer; if (firstPerformer != null) { Artist artist = Injection.Kernel.Get<IArtistRepository>().ArtistForNameOrCreate(firstPerformer.Trim()); song.ArtistId = artist.ArtistId; song.ArtistName = artist.ArtistName; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating artist info for song: ", e); song.ArtistId = null; song.ArtistName = null; } try { string firstAlbumArtist = tag.FirstAlbumArtist; if (firstAlbumArtist != null) { AlbumArtist albumArtist = Injection.Kernel.Get<IAlbumArtistRepository>().AlbumArtistForNameOrCreate(firstAlbumArtist.Trim()); song.AlbumArtistId = albumArtist.AlbumArtistId; song.AlbumArtistName = albumArtist.AlbumArtistName; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating album artist info for song: ", e); song.AlbumArtistId = null; song.AlbumArtistName = null; } // If we have an artist, but not an albumArtist, then use the artist info for albumArtist if (song.AlbumArtistId == null && song.ArtistName != null) { AlbumArtist albumArtist = Injection.Kernel.Get<IAlbumArtistRepository>().AlbumArtistForNameOrCreate(song.ArtistName); song.AlbumArtistId = albumArtist.AlbumArtistId; song.AlbumArtistName = albumArtist.AlbumArtistName; } try { string albumName = tag.Album; if (albumName != null) { Album album = Injection.Kernel.Get<IAlbumRepository>().AlbumForName(albumName.Trim(), song.AlbumArtistId, Convert.ToInt32(tag.Year)); song.AlbumId = album.AlbumId; song.AlbumName = album.AlbumName; song.ReleaseYear = album.ReleaseYear; } } catch (Exception e) { if (logger.IsErrorEnabled) logger.Error("Error creating album info for song: ", e); song.AlbumId = null; song.AlbumName = null; song.ReleaseYear = null; } song.FileType = song.FileType.FileTypeForTagLibMimeType(file.MimeType); if (song.FileType == FileType.Unknown) { logger.IfInfo("\"" + filePath + "\" Unknown file type: " + file.Properties.Description); } try { string title = tag.Title; if (title != null) { song.SongName = title.Trim(); } } catch { song.SongName = null; } try { song.TrackNumber = Convert.ToInt32(tag.Track); } catch { song.TrackNumber = null; } try { song.DiscNumber = Convert.ToInt32(tag.Disc); } catch { song.DiscNumber = null; } try { string firstGenre = tag.FirstGenre; if (firstGenre != null) { song.GenreName = firstGenre.Trim(); } } catch { song.GenreName = null; } try { song.BeatsPerMinute = tag.BeatsPerMinute; } catch { song.BeatsPerMinute = null; } try { song.Lyrics = tag.Lyrics; } catch { song.Lyrics = null; } try { song.Comment = tag.Comment; } catch { song.Comment = null; } // Dispose tag tag = null; if ((object)song.GenreName != null) { // Retreive the genre id song.GenreId = Injection.Kernel.Get<IGenreRepository>().GenreForName(song.GenreName).GenreId; } song.Duration = Convert.ToInt32(file.Properties.Duration.TotalSeconds); song.Bitrate = file.Properties.AudioBitrate; // Get necessary filesystem information about file FileInfo fsFile = new FileInfo(filePath); song.FileSize = fsFile.Length; song.LastModified = fsFile.LastWriteTime.ToUnixTime(); song.FileName = fsFile.Name; // If there is no song name, use the file name if (song.SongName == null || song.SongName == "") { song.SongName = song.FileName; } // Generate an art id from the embedded art, if it exists int? artId = CreateArt(file).ArtId; // Dispose file handles fsFile = null; file.Dispose(); // If there was no embedded art, use the folder's art artId = (object)artId == null ? Injection.Kernel.Get<IArtRepository>().ArtIdForItemId(song.FolderId) : artId; // Create the art/item relationship Injection.Kernel.Get<IArtRepository>().UpdateArtItemRelationship(artId, song.ItemId, true); return song; }
// 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; }