浏览代码

retry image saving to internal location

Luke Pulverenti 9 年之前
父节点
当前提交
49d222a6d5

+ 38 - 17
MediaBrowser.Providers/Manager/ImageSaver.cs

@@ -123,35 +123,32 @@ namespace MediaBrowser.Providers.Manager
                 new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
                 new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
                 GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
                 GetSavePaths(item, type, imageIndex, mimeType, saveLocally);
 
 
+            var retryPaths = !string.IsNullOrEmpty(internalCacheKey) ?
+                new[] { GetCacheKeyPath(item, type, mimeType, internalCacheKey) } :
+                GetSavePaths(item, type, imageIndex, mimeType, false);
+
             // If there are more than one output paths, the stream will need to be seekable
             // If there are more than one output paths, the stream will need to be seekable
-            if (paths.Length > 1 && !source.CanSeek)
+            var memoryStream = new MemoryStream();
+            using (source)
             {
             {
-                var memoryStream = new MemoryStream();
-                using (source)
-                {
-                    await source.CopyToAsync(memoryStream).ConfigureAwait(false);
-                }
-                memoryStream.Position = 0;
-                source = memoryStream;
+                await source.CopyToAsync(memoryStream).ConfigureAwait(false);
             }
             }
 
 
+            source = memoryStream;
+
             var currentPath = GetCurrentImagePath(item, type, index);
             var currentPath = GetCurrentImagePath(item, type, index);
 
 
             using (source)
             using (source)
             {
             {
-                var isFirst = true;
+                var currentPathIndex = 0;
 
 
                 foreach (var path in paths)
                 foreach (var path in paths)
                 {
                 {
-                    // Seek back to the beginning
-                    if (!isFirst)
-                    {
-                        source.Position = 0;
-                    }
+                    source.Position = 0;
 
 
-                    await SaveImageToLocation(source, path, cancellationToken).ConfigureAwait(false);
+                    await SaveImageToLocation(source, path, retryPaths[currentPathIndex], cancellationToken).ConfigureAwait(false);
 
 
-                    isFirst = false;
+                    currentPathIndex++;
                 }
                 }
             }
             }
 
 
@@ -185,6 +182,30 @@ namespace MediaBrowser.Providers.Manager
             }
             }
         }
         }
 
 
+        private async Task SaveImageToLocation(Stream source, string path, string retryPath, CancellationToken cancellationToken)
+        {
+            try
+            {
+                await SaveImageToLocation(source, path, cancellationToken).ConfigureAwait(false);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                var retry = !string.Equals(path, retryPath, StringComparison.OrdinalIgnoreCase);
+
+                if (retry)
+                {
+                    _logger.Error("UnauthorizedAccessException - Access to path {0} is denied. Will retry saving to {1}", path, retryPath);
+                }
+                else
+                {
+                    throw;
+                }
+            }
+
+            source.Position = 0;
+            await SaveImageToLocation(source, retryPath, cancellationToken).ConfigureAwait(false);
+        }
+
         private string GetCacheKeyPath(IHasImages item, ImageType type, string mimeType, string key)
         private string GetCacheKeyPath(IHasImages item, ImageType type, string mimeType, string key)
         {
         {
             var extension = MimeTypes.ToExtension(mimeType);
             var extension = MimeTypes.ToExtension(mimeType);
@@ -209,7 +230,7 @@ namespace MediaBrowser.Providers.Manager
 
 
             try
             try
             {
             {
-				_fileSystem.CreateDirectory(Path.GetDirectoryName(path));
+                _fileSystem.CreateDirectory(Path.GetDirectoryName(path));
 
 
                 // If the file is currently hidden we'll have to remove that or the save will fail
                 // If the file is currently hidden we'll have to remove that or the save will fail
                 var file = new FileInfo(path);
                 var file = new FileInfo(path);

+ 1 - 0
MediaBrowser.Server.Implementations/Channels/ChannelPostScanTask.cs

@@ -87,6 +87,7 @@ namespace MediaBrowser.Server.Implementations.Channels
 
 
                 const int currentRefreshLevel = 1;
                 const int currentRefreshLevel = 1;
                 var maxRefreshLevel = features.AutoRefreshLevels ?? 0;
                 var maxRefreshLevel = features.AutoRefreshLevels ?? 0;
+                maxRefreshLevel = Math.Max(maxRefreshLevel, 2);
 
 
                 if (maxRefreshLevel > 0)
                 if (maxRefreshLevel > 0)
                 {
                 {

+ 14 - 2
MediaBrowser.Server.Implementations/Configuration/ServerConfigurationManager.cs

@@ -203,10 +203,12 @@ namespace MediaBrowser.Server.Implementations.Configuration
                 && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
                 && !string.Equals(Configuration.ItemsByNamePath ?? string.Empty, newPath))
             {
             {
                 // Validate
                 // Validate
-				if (!_fileSystem.DirectoryExists(newPath))
+                if (!_fileSystem.DirectoryExists(newPath))
                 {
                 {
                     throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
                     throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
                 }
                 }
+
+                EnsureWriteAccess(newPath);
             }
             }
         }
         }
 
 
@@ -223,13 +225,23 @@ namespace MediaBrowser.Server.Implementations.Configuration
                 && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
                 && !string.Equals(Configuration.MetadataPath ?? string.Empty, newPath))
             {
             {
                 // Validate
                 // Validate
-				if (!_fileSystem.DirectoryExists(newPath))
+                if (!_fileSystem.DirectoryExists(newPath))
                 {
                 {
                     throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
                     throw new DirectoryNotFoundException(string.Format("{0} does not exist.", newPath));
                 }
                 }
+
+                EnsureWriteAccess(newPath);
             }
             }
         }
         }
 
 
+        private void EnsureWriteAccess(string path)
+        {
+            var file = Path.Combine(path, Guid.NewGuid().ToString());
+
+            _fileSystem.WriteAllText(file, string.Empty);
+            _fileSystem.DeleteFile(file);
+        }
+
         public void DisableMetadataService(string service)
         public void DisableMetadataService(string service)
         {
         {
             DisableMetadataService(typeof(Movie), Configuration, service);
             DisableMetadataService(typeof(Movie), Configuration, service);