From 198cb1bc9ccda757031963d01ef1f082da4a874f Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 9 May 2017 14:51:26 -0400 Subject: update file responses --- MediaBrowser.Model/IO/IFileSystem.cs | 44 ++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'MediaBrowser.Model/IO/IFileSystem.cs') diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index 26de9332e..92112f4ae 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -108,6 +108,8 @@ namespace MediaBrowser.Model.IO /// FileStream. Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, bool isAsync = false); + Stream GetFileStream(string path, FileOpenMode mode, FileAccessMode access, FileShareMode share, FileOpenOptions fileOpenOptions); + /// /// Opens the read. /// @@ -402,4 +404,46 @@ namespace MediaBrowser.Model.IO ReadWrite = 3 } + // + // Summary: + // Represents advanced options for creating a System.IO.FileStream object. + [Flags] + public enum FileOpenOptions + { + // + // Summary: + // Indicates that the system should write through any intermediate cache and go + // directly to disk. + WriteThrough = int.MinValue, + // + // Summary: + // Indicates that no additional options should be used when creating a System.IO.FileStream + // object. + None = 0, + // + // Summary: + // Indicates that a file is encrypted and can be decrypted only by using the same + // user account used for encryption. + Encrypted = 16384, + // + // Summary: + // Indicates that a file is automatically deleted when it is no longer in use. + DeleteOnClose = 67108864, + // + // Summary: + // Indicates that the file is to be accessed sequentially from beginning to end. + // The system can use this as a hint to optimize file caching. If an application + // moves the file pointer for random access, optimum caching may not occur; however, + // correct operation is still guaranteed. + SequentialScan = 134217728, + // + // Summary: + // Indicates that the file is accessed randomly. The system can use this as a hint + // to optimize file caching. + RandomAccess = 268435456, + // + // Summary: + // Indicates that a file can be used for asynchronous reading and writing. + Asynchronous = 1073741824 + } } -- cgit v1.2.3 From 1991da85af51b8500d84d60c1b4c5b42ac0eb923 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Fri, 12 May 2017 00:54:19 -0400 Subject: update setting of file attributes --- .../IO/ManagedFileSystem.cs | 58 ++++++++++++++++------ MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs | 6 +-- MediaBrowser.Model/IO/IFileSystem.cs | 3 +- MediaBrowser.Providers/Manager/ImageSaver.cs | 27 +++------- MediaBrowser.Providers/Omdb/OmdbItemProvider.cs | 19 +++---- MediaBrowser.Providers/Omdb/OmdbProvider.cs | 17 ++++--- MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs | 7 +-- 7 files changed, 75 insertions(+), 62 deletions(-) (limited to 'MediaBrowser.Model/IO/IFileSystem.cs') diff --git a/Emby.Common.Implementations/IO/ManagedFileSystem.cs b/Emby.Common.Implementations/IO/ManagedFileSystem.cs index 6cbf39758..7d14e521f 100644 --- a/Emby.Common.Implementations/IO/ManagedFileSystem.cs +++ b/Emby.Common.Implementations/IO/ManagedFileSystem.cs @@ -518,6 +518,49 @@ namespace Emby.Common.Implementations.IO } } + public void SetAttributes(string path, bool isHidden, bool isReadOnly) + { + if (_sharpCifsFileSystem.IsEnabledForPath(path)) + { + _sharpCifsFileSystem.SetAttributes(path, isHidden, isReadOnly); + return; + } + + var info = GetFileInfo(path); + + if (!info.Exists) + { + return; + } + + if (info.IsReadOnly == isReadOnly && info.IsHidden == isHidden) + { + return; + } + + var attributes = File.GetAttributes(path); + + if (isReadOnly) + { + attributes = attributes | FileAttributes.ReadOnly; + } + else + { + attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly); + } + + if (isHidden) + { + attributes = attributes | FileAttributes.Hidden; + } + else + { + attributes = RemoveAttribute(attributes, FileAttributes.Hidden); + } + + File.SetAttributes(path, attributes); + } + private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) { return attributes & ~attributesToRemove; @@ -690,20 +733,7 @@ namespace Emby.Common.Implementations.IO return; } - var fileInfo = GetFileInfo(path); - - if (fileInfo.Exists) - { - if (fileInfo.IsHidden) - { - SetHidden(path, false); - } - if (fileInfo.IsReadOnly) - { - SetReadOnly(path, false); - } - } - + SetAttributes(path, false, false); File.Delete(path); } diff --git a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs index a3a55176c..57e2ec450 100644 --- a/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs +++ b/MediaBrowser.LocalMetadata/Savers/BaseXmlSaver.cs @@ -218,13 +218,9 @@ namespace MediaBrowser.LocalMetadata.Savers { if (file.IsHidden) { - FileSystem.SetHidden(path, false); wasHidden = true; } - if (file.IsReadOnly) - { - FileSystem.SetReadOnly(path, false); - } + FileSystem.SetAttributes(path, false, false); } using (var filestream = FileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) diff --git a/MediaBrowser.Model/IO/IFileSystem.cs b/MediaBrowser.Model/IO/IFileSystem.cs index 92112f4ae..ea6b04824 100644 --- a/MediaBrowser.Model/IO/IFileSystem.cs +++ b/MediaBrowser.Model/IO/IFileSystem.cs @@ -313,7 +313,8 @@ namespace MediaBrowser.Model.IO IEnumerable GetFileSystemEntryPaths(string path, bool recursive = false); void SetHidden(string path, bool isHidden); - void SetReadOnly(string path, bool isHidden); + void SetReadOnly(string path, bool readOnly); + void SetAttributes(string path, bool isHidden, bool readOnly); char DirectorySeparatorChar { get; } diff --git a/MediaBrowser.Providers/Manager/ImageSaver.cs b/MediaBrowser.Providers/Manager/ImageSaver.cs index 1d8275c26..4bd504409 100644 --- a/MediaBrowser.Providers/Manager/ImageSaver.cs +++ b/MediaBrowser.Providers/Manager/ImageSaver.cs @@ -166,7 +166,7 @@ namespace MediaBrowser.Providers.Manager { var currentPath = currentImagePath; - _logger.Debug("Deleting previous image {0}", currentPath); + _logger.Info("Deleting previous image {0}", currentPath); _libraryMonitor.ReportFileSystemChangeBeginning(currentPath); @@ -236,7 +236,7 @@ namespace MediaBrowser.Providers.Manager /// Task. private async Task SaveImageToLocation(Stream source, string path, CancellationToken cancellationToken) { - _logger.Debug("Saving image to {0}", path); + _logger.Info("Saving image to {0}", path); var parentFolder = _fileSystem.GetDirectoryName(path); @@ -249,31 +249,16 @@ namespace MediaBrowser.Providers.Manager _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(path)); - // If the file is currently hidden we'll have to remove that or the save will fail - var file = _fileSystem.GetFileInfo(path); + _fileSystem.SetAttributes(path, false, false); - // This will fail if the file is hidden - if (file.Exists) + using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, FileOpenOptions.Asynchronous)) { - if (file.IsHidden) - { - _fileSystem.SetHidden(file.FullName, false); - } - if (file.IsReadOnly) - { - _fileSystem.SetReadOnly(path, false); - } - } - - using (var fs = _fileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, true)) - { - await source.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken) - .ConfigureAwait(false); + await source.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false); } if (_config.Configuration.SaveMetadataHidden) { - _fileSystem.SetHidden(file.FullName, true); + _fileSystem.SetHidden(path, true); } } finally diff --git a/MediaBrowser.Providers/Omdb/OmdbItemProvider.cs b/MediaBrowser.Providers/Omdb/OmdbItemProvider.cs index 477543d5e..f9d19b6be 100644 --- a/MediaBrowser.Providers/Omdb/OmdbItemProvider.cs +++ b/MediaBrowser.Providers/Omdb/OmdbItemProvider.cs @@ -72,8 +72,7 @@ namespace MediaBrowser.Providers.Omdb var imdbId = searchInfo.GetProviderId(MetadataProviders.Imdb); - var baseUrl = await OmdbProvider.GetOmdbBaseUrl(cancellationToken).ConfigureAwait(false); - var url = baseUrl + "/?plot=full&r=json"; + var urlQuery = "plot=full&r=json"; if (type == "episode" && episodeSearchInfo != null) { episodeSearchInfo.SeriesProviderIds.TryGetValue(MetadataProviders.Imdb.ToString(), out imdbId); @@ -94,23 +93,23 @@ namespace MediaBrowser.Providers.Omdb { if (year.HasValue) { - url += "&y=" + year.Value.ToString(CultureInfo.InvariantCulture); + urlQuery += "&y=" + year.Value.ToString(CultureInfo.InvariantCulture); } // &s means search and returns a list of results as opposed to t if (isSearch) { - url += "&s=" + WebUtility.UrlEncode(name); + urlQuery += "&s=" + WebUtility.UrlEncode(name); } else { - url += "&t=" + WebUtility.UrlEncode(name); + urlQuery += "&t=" + WebUtility.UrlEncode(name); } - url += "&type=" + type; + urlQuery += "&type=" + type; } else { - url += "&i=" + imdbId; + urlQuery += "&i=" + imdbId; isSearch = false; } @@ -118,14 +117,16 @@ namespace MediaBrowser.Providers.Omdb { if (searchInfo.IndexNumber.HasValue) { - url += string.Format(CultureInfo.InvariantCulture, "&Episode={0}", searchInfo.IndexNumber); + urlQuery += string.Format(CultureInfo.InvariantCulture, "&Episode={0}", searchInfo.IndexNumber); } if (searchInfo.ParentIndexNumber.HasValue) { - url += string.Format(CultureInfo.InvariantCulture, "&Season={0}", searchInfo.ParentIndexNumber); + urlQuery += string.Format(CultureInfo.InvariantCulture, "&Season={0}", searchInfo.ParentIndexNumber); } } + var url = await OmdbProvider.GetOmdbUrl(urlQuery, cancellationToken).ConfigureAwait(false); + using (var stream = await OmdbProvider.GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false)) { var resultList = new List(); diff --git a/MediaBrowser.Providers/Omdb/OmdbProvider.cs b/MediaBrowser.Providers/Omdb/OmdbProvider.cs index d1c3b2214..b89105376 100644 --- a/MediaBrowser.Providers/Omdb/OmdbProvider.cs +++ b/MediaBrowser.Providers/Omdb/OmdbProvider.cs @@ -265,9 +265,16 @@ namespace MediaBrowser.Providers.Omdb return false; } - public static async Task GetOmdbBaseUrl(CancellationToken cancellationToken) + public static async Task GetOmdbUrl(string query, CancellationToken cancellationToken) { - return "https://www.omdbapi.com"; + var url = "https://www.omdbapi.com?apikey=fe53f97e"; + + if (!string.IsNullOrWhiteSpace(query)) + { + url += "&" + query; + } + + return url; } private async Task EnsureItemInfo(string imdbId, CancellationToken cancellationToken) @@ -292,8 +299,7 @@ namespace MediaBrowser.Providers.Omdb } } - var baseUrl = await GetOmdbBaseUrl(cancellationToken).ConfigureAwait(false); - var url = string.Format(baseUrl + "/?i={0}&plot=full&tomatoes=true&r=json", imdbParam); + var url = await GetOmdbUrl(string.Format("i={0}&plot=full&tomatoes=true&r=json", imdbParam), cancellationToken).ConfigureAwait(false); using (var stream = await GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false)) { @@ -327,8 +333,7 @@ namespace MediaBrowser.Providers.Omdb } } - var baseUrl = await GetOmdbBaseUrl(cancellationToken).ConfigureAwait(false); - var url = string.Format(baseUrl + "/?i={0}&season={1}&detail=full", imdbParam, seasonId); + var url = await GetOmdbUrl(string.Format("i={0}&season={1}&detail=full", imdbParam, seasonId), cancellationToken).ConfigureAwait(false); using (var stream = await GetOmdbResponse(_httpClient, url, cancellationToken).ConfigureAwait(false)) { diff --git a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs index 623b109f7..ae2492802 100644 --- a/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs +++ b/MediaBrowser.XbmcMetadata/Savers/BaseNfoSaver.cs @@ -220,14 +220,9 @@ namespace MediaBrowser.XbmcMetadata.Savers { if (file.IsHidden) { - FileSystem.SetHidden(path, false); - wasHidden = true; } - if (file.IsReadOnly) - { - FileSystem.SetReadOnly(path, false); - } + FileSystem.SetAttributes(path, false, false); } using (var filestream = FileSystem.GetFileStream(path, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read)) -- cgit v1.2.3