From 69b83082c856355de416b17c1dc73b3ad7505db1 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 12 Oct 2014 11:18:26 -0400 Subject: [PATCH] grab image sizes at discovery time --- MediaBrowser.Api/Images/ImageService.cs | 36 +---------- MediaBrowser.Controller/Dto/IDtoService.cs | 3 +- MediaBrowser.Controller/Entities/BaseItem.cs | 50 +++++++++++----- .../Entities/ItemImageInfo.cs | 4 ++ MediaBrowser.Dlna/Didl/DidlBuilder.cs | 21 ++++--- MediaBrowser.Model/Entities/ChapterInfo.cs | 12 ++++ .../Photos/PhotoProvider.cs | 14 ++--- .../TV/MissingEpisodeProvider.cs | 10 +--- .../Dto/DtoService.cs | 60 +++++++++++-------- .../Localization/Server/server.json | 4 +- .../Session/SessionManager.cs | 3 +- 11 files changed, 112 insertions(+), 105 deletions(-) diff --git a/MediaBrowser.Api/Images/ImageService.cs b/MediaBrowser.Api/Images/ImageService.cs index b05e371a6..d455290be 100644 --- a/MediaBrowser.Api/Images/ImageService.cs +++ b/MediaBrowser.Api/Images/ImageService.cs @@ -312,36 +312,6 @@ namespace MediaBrowser.Api.Images } } - var video = item as Video; - - if (video != null) - { - var index = 0; - - foreach (var chapter in _itemRepo.GetChapters(video.Id)) - { - if (!string.IsNullOrEmpty(chapter.ImagePath)) - { - var image = chapter.ImagePath; - - var info = GetImageInfo(item, new ItemImageInfo - { - Path = image, - Type = ImageType.Chapter, - DateModified = _fileSystem.GetLastWriteTimeUtc(image) - - }, index); - - if (info != null) - { - list.Add(info); - } - } - - index++; - } - } - return list; } @@ -351,8 +321,6 @@ namespace MediaBrowser.Api.Images { var fileInfo = new FileInfo(info.Path); - var size = _imageProcessor.GetImageSize(info.Path); - return new ImageInfo { Path = info.Path, @@ -360,8 +328,8 @@ namespace MediaBrowser.Api.Images ImageType = info.Type, ImageTag = _imageProcessor.GetImageCacheTag(item, info), Size = fileInfo.Length, - Width = Convert.ToInt32(size.Width), - Height = Convert.ToInt32(size.Height) + Width = info.Width ?? 0, + Height = info.Height ?? 0 }; } catch (Exception ex) diff --git a/MediaBrowser.Controller/Dto/IDtoService.cs b/MediaBrowser.Controller/Dto/IDtoService.cs index fc15eedf0..b3917d70b 100644 --- a/MediaBrowser.Controller/Dto/IDtoService.cs +++ b/MediaBrowser.Controller/Dto/IDtoService.cs @@ -47,10 +47,9 @@ namespace MediaBrowser.Controller.Dto /// /// Gets the chapter information dto. /// - /// The chapter information. /// The item. /// ChapterInfoDto. - ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item); + List GetChapterInfoDtos(BaseItem item); /// /// Gets the user item data dto. diff --git a/MediaBrowser.Controller/Entities/BaseItem.cs b/MediaBrowser.Controller/Entities/BaseItem.cs index 93b71ae36..97564f563 100644 --- a/MediaBrowser.Controller/Entities/BaseItem.cs +++ b/MediaBrowser.Controller/Entities/BaseItem.cs @@ -3,6 +3,7 @@ using MediaBrowser.Common.IO; using MediaBrowser.Controller.Channels; using MediaBrowser.Controller.Collections; using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.Localization; @@ -253,6 +254,7 @@ namespace MediaBrowser.Controller.Entities public static ILiveTvManager LiveTvManager { get; set; } public static IChannelManager ChannelManager { get; set; } public static ICollectionManager CollectionManager { get; set; } + public static IImageProcessor ImageProcessor { get; set; } /// /// Returns a that represents this instance. @@ -1455,17 +1457,16 @@ namespace MediaBrowser.Controller.Entities if (image == null) { - ImageInfos.Add(new ItemImageInfo - { - Path = file.FullName, - Type = type, - DateModified = FileSystem.GetLastWriteTimeUtc(file) - }); + ImageInfos.Add(GetImageInfo(file, type)); } else { + var imageInfo = GetImageInfo(file, type); + image.Path = file.FullName; - image.DateModified = FileSystem.GetLastWriteTimeUtc(file); + image.DateModified = imageInfo.DateModified; + image.Width = imageInfo.Width; + image.Height = imageInfo.Height; } } @@ -1574,7 +1575,9 @@ namespace MediaBrowser.Controller.Entities { Path = path, DateModified = FileSystem.GetLastWriteTimeUtc(path), - Type = imageType + Type = imageType, + Width = chapter.ImageWidth, + Height = chapter.ImageHeight }; } @@ -1631,16 +1634,35 @@ namespace MediaBrowser.Controller.Entities } } - ImageInfos.AddRange(newImageList.Select(i => new ItemImageInfo - { - Path = i.FullName, - Type = imageType, - DateModified = FileSystem.GetLastWriteTimeUtc(i) - })); + ImageInfos.AddRange(newImageList.Select(i => GetImageInfo(i, imageType))); return newImageList.Count > 0; } + private ItemImageInfo GetImageInfo(FileSystemInfo file, ImageType type) + { + var info = new ItemImageInfo + { + Path = file.FullName, + Type = type, + DateModified = FileSystem.GetLastWriteTimeUtc(file) + }; + + try + { + var size = ImageProcessor.GetImageSize(info.Path); + + info.Width = Convert.ToInt32(size.Width); + info.Height = Convert.ToInt32(size.Height); + } + catch + { + + } + + return info; + } + /// /// Gets the file system path to delete when the item is to be deleted /// diff --git a/MediaBrowser.Controller/Entities/ItemImageInfo.cs b/MediaBrowser.Controller/Entities/ItemImageInfo.cs index 80aec6482..fe1f0598a 100644 --- a/MediaBrowser.Controller/Entities/ItemImageInfo.cs +++ b/MediaBrowser.Controller/Entities/ItemImageInfo.cs @@ -10,5 +10,9 @@ namespace MediaBrowser.Controller.Entities public ImageType Type { get; set; } public DateTime DateModified { get; set; } + + public int? Width { get; set; } + + public int? Height { get; set; } } } diff --git a/MediaBrowser.Dlna/Didl/DidlBuilder.cs b/MediaBrowser.Dlna/Didl/DidlBuilder.cs index 28aebae79..b6732456b 100644 --- a/MediaBrowser.Dlna/Didl/DidlBuilder.cs +++ b/MediaBrowser.Dlna/Didl/DidlBuilder.cs @@ -869,19 +869,22 @@ namespace MediaBrowser.Dlna.Didl } - int? width = null; - int? height = null; + int? width = imageInfo.Width; + int? height = imageInfo.Height; - try + if (!height.HasValue && !width.HasValue) { - var size = _imageProcessor.GetImageSize(imageInfo.Path, imageInfo.DateModified); + try + { + var size = _imageProcessor.GetImageSize(imageInfo.Path, imageInfo.DateModified); - width = Convert.ToInt32(size.Width); - height = Convert.ToInt32(size.Height); - } - catch - { + width = Convert.ToInt32(size.Width); + height = Convert.ToInt32(size.Height); + } + catch + { + } } return new ImageDownloadInfo diff --git a/MediaBrowser.Model/Entities/ChapterInfo.cs b/MediaBrowser.Model/Entities/ChapterInfo.cs index 9da7a9caa..9d0bbc0d9 100644 --- a/MediaBrowser.Model/Entities/ChapterInfo.cs +++ b/MediaBrowser.Model/Entities/ChapterInfo.cs @@ -23,5 +23,17 @@ namespace MediaBrowser.Model.Entities /// /// The image path. public string ImagePath { get; set; } + + /// + /// Gets or sets the height of the image. + /// + /// The height of the image. + public int? ImageHeight { get; set; } + + /// + /// Gets or sets the width of the image. + /// + /// The width of the image. + public int? ImageWidth { get; set; } } } diff --git a/MediaBrowser.Providers/Photos/PhotoProvider.cs b/MediaBrowser.Providers/Photos/PhotoProvider.cs index 74c26fdd8..fe6289766 100644 --- a/MediaBrowser.Providers/Photos/PhotoProvider.cs +++ b/MediaBrowser.Providers/Photos/PhotoProvider.cs @@ -1,5 +1,4 @@ -using MediaBrowser.Controller.Drawing; -using MediaBrowser.Controller.Entities; +using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; @@ -18,12 +17,10 @@ namespace MediaBrowser.Providers.Photos public class PhotoProvider : ICustomMetadataProvider, IHasItemChangeMonitor { private readonly ILogger _logger; - private readonly IImageProcessor _imageProcessor; - public PhotoProvider(ILogger logger, IImageProcessor imageProcessor) + public PhotoProvider(ILogger logger) { _logger = logger; - _imageProcessor = imageProcessor; } public Task FetchAsync(Photo item, MetadataRefreshOptions options, CancellationToken cancellationToken) @@ -142,9 +139,10 @@ namespace MediaBrowser.Providers.Photos _logger.ErrorException("Image Provider - Error reading image tag for {0}", e, item.Path); } - var size = _imageProcessor.GetImageSize(item.Path); - item.Height = Convert.ToInt32(size.Height); - item.Width = Convert.ToInt32(size.Width); + var imageInfo = item.GetImageInfo(ImageType.Primary, 0); + + item.Height = imageInfo.Height; + item.Width = imageInfo.Width; const ItemUpdateType result = ItemUpdateType.ImageUpdate | ItemUpdateType.MetadataImport; return Task.FromResult(result); diff --git a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs index 49712eea3..87e2f6c9c 100644 --- a/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs +++ b/MediaBrowser.Providers/TV/MissingEpisodeProvider.cs @@ -93,10 +93,10 @@ namespace MediaBrowser.Providers.TV var hasBadData = HasInvalidContent(group); - var anySeasonsRemoved = await RemoveObsoleteOrMissingSeasons(group, episodeLookup, hasBadData) + var anySeasonsRemoved = await RemoveObsoleteOrMissingSeasons(group, episodeLookup, false) .ConfigureAwait(false); - var anyEpisodesRemoved = await RemoveObsoleteOrMissingEpisodes(group, episodeLookup, hasBadData) + var anyEpisodesRemoved = await RemoveObsoleteOrMissingEpisodes(group, episodeLookup, false) .ConfigureAwait(false); var hasNewEpisodes = false; @@ -204,12 +204,6 @@ namespace MediaBrowser.Providers.TV IEnumerable> episodeLookup, CancellationToken cancellationToken) { - // Be conservative here to avoid creating missing episodes for ones they already have - if (!seriesHasBadData) - { - return false; - } - var existingEpisodes = (from s in series let seasonOffset = TvdbSeriesProvider.GetSeriesOffset(s.ProviderIds) ?? ((s.AnimeSeriesIndex ?? 1) - 1) from c in s.RecursiveChildren.OfType() diff --git a/MediaBrowser.Server.Implementations/Dto/DtoService.cs b/MediaBrowser.Server.Implementations/Dto/DtoService.cs index 0c06d1b18..c2ac386eb 100644 --- a/MediaBrowser.Server.Implementations/Dto/DtoService.cs +++ b/MediaBrowser.Server.Implementations/Dto/DtoService.cs @@ -372,7 +372,7 @@ namespace MediaBrowser.Server.Implementations.Dto dto.Longitude = item.Longitude; dto.Altitude = item.Altitude; dto.IsoSpeedRating = item.IsoSpeedRating; - + var album = item.Album; if (album != null) @@ -659,7 +659,7 @@ namespace MediaBrowser.Server.Implementations.Dto /// The chapter info. /// The item. /// ChapterInfoDto. - public ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item) + private ChapterInfoDto GetChapterInfoDto(ChapterInfo chapterInfo, BaseItem item) { var dto = new ChapterInfoDto { @@ -680,6 +680,13 @@ namespace MediaBrowser.Server.Implementations.Dto return dto; } + public List GetChapterInfoDtos(BaseItem item) + { + return _itemRepo.GetChapters(item.Id) + .Select(c => GetChapterInfoDto(c, item)) + .ToList(); + } + /// /// Sets simple property values on a DTOBaseItem /// @@ -1055,20 +1062,7 @@ namespace MediaBrowser.Server.Implementations.Dto if (fields.Contains(ItemFields.Chapters)) { - List chapters; - - if (dto.MediaSources != null && dto.MediaSources.Count > 0) - { - chapters = _itemRepo.GetChapters(item.Id).Select(c => GetChapterInfoDto(c, item)).ToList(); - } - else - { - chapters = _itemRepo.GetChapters(video.Id) - .Select(c => GetChapterInfoDto(c, item)) - .ToList(); - } - - dto.Chapters = chapters; + dto.Chapters = GetChapterInfoDtos(item); } } @@ -1435,21 +1429,35 @@ namespace MediaBrowser.Server.Implementations.Dto // See if we can avoid a file system lookup by looking for the file in ResolveArgs var dateModified = imageInfo.DateModified; + double? width = imageInfo.Width; + double? height = imageInfo.Height; + ImageSize size; - try + if (!width.HasValue || !height.HasValue) { - size = _imageProcessor.GetImageSize(path, dateModified); + try + { + size = _imageProcessor.GetImageSize(path, dateModified); + } + catch (FileNotFoundException) + { + _logger.Error("Image file does not exist: {0}", path); + return; + } + catch (Exception ex) + { + _logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path); + return; + } } - catch (FileNotFoundException) + else { - _logger.Error("Image file does not exist: {0}", path); - return; - } - catch (Exception ex) - { - _logger.ErrorException("Failed to determine primary image aspect ratio for {0}", ex, path); - return; + size = new ImageSize + { + Height = height.Value, + Width = width.Value + }; } dto.OriginalPrimaryImageAspectRatio = size.Width / size.Height; diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index efe50f45a..d7a99638e 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -1224,6 +1224,6 @@ "MessageNoDevicesSupportCameraUpload": "You currently don't have any devices that support camera upload.", "LabelCameraUploadPath": "Camera upload path:", "LabelCameraUploadPathHelp": "Select a custom upload path, if desired. If unspecified a default folder will be used.", - "LabelCreateCameraUploadSubfolder": "Create a sub-folder for each device", - "LabelCreateCameraUploadSubfolderHelp": "Specific folders can be assigned to devices by clicking on the device on the Devices page." + "LabelCreateCameraUploadSubfolder": "Create a subfolder for each device", + "LabelCreateCameraUploadSubfolderHelp": "Specific folders can be assigned to a device by clicking on it from the Devices page." } diff --git a/MediaBrowser.Server.Implementations/Session/SessionManager.cs b/MediaBrowser.Server.Implementations/Session/SessionManager.cs index 8d640bf1e..c16044bd3 100644 --- a/MediaBrowser.Server.Implementations/Session/SessionManager.cs +++ b/MediaBrowser.Server.Implementations/Session/SessionManager.cs @@ -26,7 +26,6 @@ using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; -using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -1594,7 +1593,7 @@ namespace MediaBrowser.Server.Implementations.Session { info.ChapterImagesItemId = chapterOwner.Id.ToString("N"); - info.Chapters = _itemRepo.GetChapters(chapterOwner.Id).Select(i => _dtoService.GetChapterInfoDto(i, chapterOwner)).ToList(); + info.Chapters = _dtoService.GetChapterInfoDtos(chapterOwner).ToList(); } if (!string.IsNullOrWhiteSpace(mediaSourceId))