From 77cff18dce6f07aa46d2f0d89fb444cd95203134 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 22 Oct 2013 21:11:56 -0400 Subject: [PATCH] fix images saving with incorrect paths. --- .../Movies/MovieDbImagesProvider.cs | 20 +++++--- .../Movies/MovieDbProvider.cs | 9 ++-- .../Movies/PersonUpdatesPreScanTask.cs | 31 +++++++----- .../Movies/TmdbPersonProvider.cs | 47 +++++++++---------- .../Providers/ImageSaver.cs | 8 ++-- 5 files changed, 62 insertions(+), 53 deletions(-) diff --git a/MediaBrowser.Providers/Movies/MovieDbImagesProvider.cs b/MediaBrowser.Providers/Movies/MovieDbImagesProvider.cs index 3c001b8c2..52e6c214f 100644 --- a/MediaBrowser.Providers/Movies/MovieDbImagesProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbImagesProvider.cs @@ -145,6 +145,11 @@ namespace MediaBrowser.Providers.Movies protected override bool NeedsRefreshBasedOnCompareDate(BaseItem item, BaseProviderInfo providerInfo) { + if (string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb))) + { + return false; + } + var path = MovieDbProvider.Current.GetDataFilePath(item, "default"); if (!string.IsNullOrEmpty(path)) @@ -169,13 +174,18 @@ namespace MediaBrowser.Providers.Movies /// Task{System.Boolean}. public override async Task FetchAsync(BaseItem item, bool force, CancellationToken cancellationToken) { - var images = FetchImages(item, item.GetProviderId(MetadataProviders.Tmdb), cancellationToken); + var id = item.GetProviderId(MetadataProviders.Tmdb); var status = ProviderRefreshStatus.Success; - if (images != null) + if (!string.IsNullOrEmpty(id)) { - status = await ProcessImages(item, images, cancellationToken).ConfigureAwait(false); + var images = FetchImages(item); + + if (images != null) + { + status = await ProcessImages(item, images, cancellationToken).ConfigureAwait(false); + } } SetLastRefreshed(item, DateTime.UtcNow, status); @@ -186,10 +196,8 @@ namespace MediaBrowser.Providers.Movies /// Fetches the images. /// /// The item. - /// The id. - /// The cancellation token. /// Task{MovieImages}. - private MovieDbProvider.Images FetchImages(BaseItem item, string id, CancellationToken cancellationToken) + private MovieDbProvider.Images FetchImages(BaseItem item) { var path = MovieDbProvider.Current.GetDataFilePath(item, "default"); diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index e23b53dfc..92759e013 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -194,9 +194,6 @@ namespace MediaBrowser.Providers.Movies protected override bool NeedsRefreshInternal(BaseItem item, BaseProviderInfo providerInfo) { - if (HasAltMeta(item) && !ConfigurationManager.Configuration.EnableTmdbUpdates) - return false; - // Boxsets require two passes because we need the children to be refreshed if (item is BoxSet && string.IsNullOrEmpty(item.GetProviderId(MetadataProviders.Tmdb))) { @@ -220,6 +217,8 @@ namespace MediaBrowser.Providers.Movies { return fileInfo.LastWriteTimeUtc > providerInfo.LastRefreshed; } + + return true; } return base.NeedsRefreshBasedOnCompareDate(item, providerInfo); @@ -507,8 +506,6 @@ namespace MediaBrowser.Providers.Movies var dataFilePath = GetDataFilePath(item, language); - var hasAltMeta = HasAltMeta(item); - if (string.IsNullOrEmpty(dataFilePath) || !File.Exists(dataFilePath)) { var isBoxSet = item is BoxSet; @@ -535,7 +532,7 @@ namespace MediaBrowser.Providers.Movies JsonSerializer.SerializeToFile(mainResult, dataFilePath); } - if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !hasAltMeta) + if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !HasAltMeta(item)) { dataFilePath = GetDataFilePath(item, language); diff --git a/MediaBrowser.Providers/Movies/PersonUpdatesPreScanTask.cs b/MediaBrowser.Providers/Movies/PersonUpdatesPreScanTask.cs index e9d0b5c9a..d6cc39c86 100644 --- a/MediaBrowser.Providers/Movies/PersonUpdatesPreScanTask.cs +++ b/MediaBrowser.Providers/Movies/PersonUpdatesPreScanTask.cs @@ -2,7 +2,6 @@ using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Library; using MediaBrowser.Model.Logging; -using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; using System; using System.Collections.Generic; @@ -83,7 +82,7 @@ namespace MediaBrowser.Providers.Movies // Find out the last time we queried tvdb for updates var lastUpdateTime = timestampFileInfo.Exists ? File.ReadAllText(timestampFile, Encoding.UTF8) : string.Empty; - var existingDirectories = Directory.EnumerateDirectories(path).Select(Path.GetFileName).ToList(); + var existingDirectories = GetExistingIds(path).ToList(); if (!string.IsNullOrEmpty(lastUpdateTime)) { @@ -105,7 +104,7 @@ namespace MediaBrowser.Providers.Movies var idsToUpdate = updatedIds.Where(i => !string.IsNullOrWhiteSpace(i) && existingDictionary.ContainsKey(i)); - await UpdatePeople(idsToUpdate, path, progress, cancellationToken).ConfigureAwait(false); + await UpdatePeople(idsToUpdate, progress, cancellationToken).ConfigureAwait(false); } } @@ -113,6 +112,18 @@ namespace MediaBrowser.Providers.Movies progress.Report(100); } + /// + /// Gets the existing ids. + /// + /// The path. + /// IEnumerable{System.String}. + private IEnumerable GetExistingIds(string path) + { + return Directory.EnumerateDirectories(path) + .SelectMany(Directory.EnumerateDirectories) + .Select(Path.GetFileNameWithoutExtension); + } + /// /// Gets the ids to update. /// @@ -159,11 +170,10 @@ namespace MediaBrowser.Providers.Movies /// Updates the people. /// /// The ids. - /// The people data path. /// The progress. /// The cancellation token. /// Task. - private async Task UpdatePeople(IEnumerable ids, string peopleDataPath, IProgress progress, CancellationToken cancellationToken) + private async Task UpdatePeople(IEnumerable ids, IProgress progress, CancellationToken cancellationToken) { var list = ids.ToList(); var numComplete = 0; @@ -172,7 +182,7 @@ namespace MediaBrowser.Providers.Movies { try { - await UpdatePerson(id, peopleDataPath, cancellationToken).ConfigureAwait(false); + await UpdatePerson(id, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { @@ -192,18 +202,13 @@ namespace MediaBrowser.Providers.Movies /// Updates the person. /// /// The id. - /// The people data path. /// The cancellation token. /// Task. - private Task UpdatePerson(string id, string peopleDataPath, CancellationToken cancellationToken) + private Task UpdatePerson(string id, CancellationToken cancellationToken) { _logger.Info("Updating person from tmdb " + id); - var personDataPath = Path.Combine(peopleDataPath, id); - - Directory.CreateDirectory(peopleDataPath); - - return TmdbPersonProvider.Current.DownloadPersonInfo(id, personDataPath, cancellationToken); + return TmdbPersonProvider.Current.DownloadPersonInfo(id, cancellationToken); } class Result diff --git a/MediaBrowser.Providers/Movies/TmdbPersonProvider.cs b/MediaBrowser.Providers/Movies/TmdbPersonProvider.cs index 685f4bc6e..f48ef4c26 100644 --- a/MediaBrowser.Providers/Movies/TmdbPersonProvider.cs +++ b/MediaBrowser.Providers/Movies/TmdbPersonProvider.cs @@ -1,4 +1,5 @@ using MediaBrowser.Common.Configuration; +using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller.Configuration; @@ -27,7 +28,9 @@ namespace MediaBrowser.Providers.Movies protected readonly IProviderManager ProviderManager; internal static TmdbPersonProvider Current { get; private set; } - + + const string DataFileName = "info.json"; + public TmdbPersonProvider(IJsonSerializer jsonSerializer, ILogManager logManager, IServerConfigurationManager configurationManager, IProviderManager providerManager) : base(logManager, configurationManager) { @@ -68,7 +71,7 @@ namespace MediaBrowser.Providers.Movies { get { - return "2"; + return "3"; } } @@ -97,23 +100,15 @@ namespace MediaBrowser.Providers.Movies // Process images var path = GetPersonDataPath(ConfigurationManager.ApplicationPaths, provderId); - try - { - var files = new DirectoryInfo(path) - .EnumerateFiles("*.json", SearchOption.TopDirectoryOnly) - .Select(i => i.LastWriteTimeUtc) - .ToList(); + var file = Path.Combine(path, DataFileName); + var fileInfo = new FileInfo(file); - if (files.Count > 0) - { - return files.Max() > providerInfo.LastRefreshed; - } - } - catch (DirectoryNotFoundException) + if (fileInfo.Exists) { - // Don't blow up - return true; + return fileInfo.LastWriteTimeUtc > providerInfo.LastRefreshed; } + + return true; } return base.NeedsRefreshBasedOnCompareDate(item, providerInfo); @@ -121,7 +116,9 @@ namespace MediaBrowser.Providers.Movies internal static string GetPersonDataPath(IApplicationPaths appPaths, string tmdbId) { - var seriesDataPath = Path.Combine(GetPersonsDataPath(appPaths), tmdbId); + var letter = tmdbId.GetMD5().ToString().Substring(0, 1); + + var seriesDataPath = Path.Combine(GetPersonsDataPath(appPaths), letter, tmdbId); return seriesDataPath; } @@ -240,18 +237,16 @@ namespace MediaBrowser.Providers.Movies .Select(Path.GetFileName) .ToList(); - const string dataFileName = "info.json"; - // Only download if not already there // The prescan task will take care of updates so we don't need to re-download here - if (!files.Contains(dataFileName, StringComparer.OrdinalIgnoreCase)) + if (!files.Contains(DataFileName, StringComparer.OrdinalIgnoreCase)) { - await DownloadPersonInfo(id, personDataPath, cancellationToken).ConfigureAwait(false); + await DownloadPersonInfo(id, cancellationToken).ConfigureAwait(false); } if (isForcedRefresh || ConfigurationManager.Configuration.EnableTmdbUpdates || !HasAltMeta(person)) { - var info = JsonSerializer.DeserializeFromFile(Path.Combine(personDataPath, dataFileName)); + var info = JsonSerializer.DeserializeFromFile(Path.Combine(personDataPath, DataFileName)); cancellationToken.ThrowIfCancellationRequested(); @@ -263,8 +258,10 @@ namespace MediaBrowser.Providers.Movies } } - internal async Task DownloadPersonInfo(string id, string personDataPath, CancellationToken cancellationToken) + internal async Task DownloadPersonInfo(string id, CancellationToken cancellationToken) { + var personDataPath = GetPersonDataPath(ConfigurationManager.ApplicationPaths, id); + var url = string.Format(@"http://api.themoviedb.org/3/person/{1}?api_key={0}&append_to_response=credits,images", MovieDbProvider.ApiKey, id); using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions @@ -275,7 +272,9 @@ namespace MediaBrowser.Providers.Movies }).ConfigureAwait(false)) { - using (var fs = new FileStream(Path.Combine(personDataPath, "info.json"), FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true)) + Directory.CreateDirectory(personDataPath); + + using (var fs = new FileStream(Path.Combine(personDataPath, DataFileName), FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true)) { await json.CopyToAsync(fs).ConfigureAwait(false); } diff --git a/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs b/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs index 711b3f77c..a967c535e 100644 --- a/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs +++ b/MediaBrowser.Server.Implementations/Providers/ImageSaver.cs @@ -362,6 +362,8 @@ namespace MediaBrowser.Server.Implementations.Providers extension = "jpg"; } + extension = "." + extension.ToLower(); + string path = null; if (saveLocally) @@ -373,16 +375,14 @@ namespace MediaBrowser.Server.Implementations.Providers if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(item.MetaLocation)) { - path = Path.Combine(item.MetaLocation, filename + extension.ToLower()); + path = Path.Combine(item.MetaLocation, filename + extension); } } - filename += "." + extension.ToLower(); - // None of the save local conditions passed, so store it in our internal folders if (string.IsNullOrEmpty(path)) { - path = _remoteImageCache.GetResourcePath(item.GetType().FullName + item.Id, filename); + path = _remoteImageCache.GetResourcePath(item.GetType().FullName + item.Id, filename + extension); } return path;