Add resume capability to GetTempFile

This commit is contained in:
LukePulverenti 2013-03-14 15:52:53 -04:00
parent a7f06c84b1
commit fe1834e6be
10 changed files with 219 additions and 119 deletions

View File

@ -1,4 +1,5 @@
using MediaBrowser.Common.Configuration;
using System.Net.Http.Headers;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
@ -203,50 +204,96 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
/// <summary>
/// Downloads the contents of a given url into a temporary location
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="resourcePool">The resource pool.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <param name="userAgent">The user agent.</param>
/// <param name="options">The options.</param>
/// <returns>Task{System.String}.</returns>
/// <exception cref="System.ArgumentNullException">progress</exception>
/// <exception cref="HttpException"></exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
public async Task<string> GetTempFile(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken, IProgress<double> progress, string userAgent = null)
public Task<string> GetTempFile(HttpRequestOptions options)
{
ValidateParams(url, cancellationToken);
var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
if (progress == null)
return GetTempFile(options, tempFile, 0);
}
/// <summary>
/// Gets the temp file.
/// </summary>
/// <param name="options">The options.</param>
/// <param name="tempFile">The temp file.</param>
/// <param name="resumeCount">The resume count.</param>
/// <returns>Task{System.String}.</returns>
/// <exception cref="System.ArgumentNullException">progress</exception>
/// <exception cref="HttpException"></exception>
private async Task<string> GetTempFile(HttpRequestOptions options, string tempFile, int resumeCount)
{
ValidateParams(options.Url, options.CancellationToken);
if (options.Progress == null)
{
throw new ArgumentNullException("progress");
}
cancellationToken.ThrowIfCancellationRequested();
options.CancellationToken.ThrowIfCancellationRequested();
var tempFile = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid() + ".tmp");
var message = new HttpRequestMessage(HttpMethod.Get, options.Url);
var message = new HttpRequestMessage(HttpMethod.Get, url);
if (!string.IsNullOrEmpty(userAgent))
if (!string.IsNullOrEmpty(options.UserAgent))
{
message.Headers.Add("User-Agent", userAgent);
message.Headers.Add("User-Agent", options.UserAgent);
}
if (resourcePool != null)
if (options.ResourcePool != null)
{
await resourcePool.WaitAsync(cancellationToken).ConfigureAwait(false);
await options.ResourcePool.WaitAsync(options.CancellationToken).ConfigureAwait(false);
}
_logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", url, tempFile);
options.Progress.Report(0);
_logger.Info("HttpClientManager.GetTempFile url: {0}, temp file: {1}", options.Url, tempFile);
FileStream tempFileStream;
if (resumeCount > 0 && File.Exists(tempFile))
{
tempFileStream = new FileStream(tempFile, FileMode.Open, FileAccess.Write, FileShare.Read,
StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
var startPosition = tempFileStream.Length;
tempFileStream.Seek(startPosition, SeekOrigin.Current);
message.Headers.Range = new RangeHeaderValue(startPosition, null);
_logger.Info("Resuming from position {1} for {0}", options.Url, startPosition);
}
else
{
tempFileStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read,
StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous);
}
var serverSupportsRangeRequests = false;
Exception downloadException = null;
try
{
cancellationToken.ThrowIfCancellationRequested();
options.CancellationToken.ThrowIfCancellationRequested();
using (var response = await GetHttpClient(GetHostFromUrl(url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false))
using (var response = await GetHttpClient(GetHostFromUrl(options.Url)).SendAsync(message, HttpCompletionOption.ResponseHeadersRead, options.CancellationToken).ConfigureAwait(false))
{
EnsureSuccessStatusCode(response);
cancellationToken.ThrowIfCancellationRequested();
options.CancellationToken.ThrowIfCancellationRequested();
var rangeValue = string.Join(" ", response.Headers.AcceptRanges.ToArray());
serverSupportsRangeRequests = rangeValue.IndexOf("bytes", StringComparison.OrdinalIgnoreCase) != -1 || rangeValue.IndexOf("*", StringComparison.OrdinalIgnoreCase) != -1;
if (!serverSupportsRangeRequests && resumeCount > 0)
{
_logger.Info("Server does not support range requests for {0}", options.Url);
tempFileStream.Position = 0;
}
IEnumerable<string> lengthValues;
@ -256,75 +303,97 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
// We're not able to track progress
using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
}
await stream.CopyToAsync(tempFileStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
}
}
else
{
var length = long.Parse(string.Join(string.Empty, lengthValues.ToArray()));
using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), progress.Report, length))
using (var stream = ProgressStream.CreateReadProgressStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false), options.Progress.Report, length))
{
using (var fs = new FileStream(tempFile, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, FileOptions.Asynchronous))
{
await stream.CopyToAsync(fs, StreamDefaults.DefaultCopyToBufferSize, cancellationToken).ConfigureAwait(false);
}
await stream.CopyToAsync(tempFileStream, StreamDefaults.DefaultCopyToBufferSize, options.CancellationToken).ConfigureAwait(false);
}
}
progress.Report(100);
options.Progress.Report(100);
cancellationToken.ThrowIfCancellationRequested();
options.CancellationToken.ThrowIfCancellationRequested();
}
return tempFile;
}
catch (OperationCanceledException ex)
{
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
throw GetCancellationException(url, cancellationToken, ex);
}
catch (HttpRequestException ex)
{
_logger.ErrorException("Error getting response from " + url, ex);
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
throw new HttpException(ex.Message, ex);
}
catch (Exception ex)
{
_logger.ErrorException("Error getting response from " + url, ex);
downloadException = ex;
}
finally
{
tempFileStream.Dispose();
if (options.ResourcePool != null)
{
options.ResourcePool.Release();
}
}
if (downloadException != null)
{
await HandleTempFileException(downloadException, options, tempFile, serverSupportsRangeRequests, resumeCount).ConfigureAwait(false);
}
return tempFile;
}
/// <summary>
/// Handles the temp file exception.
/// </summary>
/// <param name="ex">The ex.</param>
/// <param name="options">The options.</param>
/// <param name="tempFile">The temp file.</param>
/// <param name="serverSupportsRangeRequests">if set to <c>true</c> [server supports range requests].</param>
/// <param name="resumeCount">The resume count.</param>
/// <returns>Task.</returns>
/// <exception cref="HttpException"></exception>
private Task HandleTempFileException(Exception ex, HttpRequestOptions options, string tempFile, bool serverSupportsRangeRequests, int resumeCount)
{
var operationCanceledException = ex as OperationCanceledException;
if (operationCanceledException != null)
{
// Cleanup
if (File.Exists(tempFile))
{
File.Delete(tempFile);
}
throw;
throw GetCancellationException(options.Url, options.CancellationToken, operationCanceledException);
}
finally
{
if (resourcePool != null)
{
resourcePool.Release();
}
}
}
_logger.ErrorException("Error getting response from " + options.Url, ex);
var httpRequestException = ex as HttpRequestException;
// Cleanup
if (File.Exists(tempFile))
{
// Try to resume
if (httpRequestException != null && serverSupportsRangeRequests && resumeCount < options.MaxResumeCount && new FileInfo(tempFile).Length > 0)
{
_logger.Info("Attempting to resume download from {0}", options.Url);
return GetTempFile(options, tempFile, resumeCount + 1);
}
File.Delete(tempFile);
}
if (httpRequestException != null)
{
throw new HttpException(ex.Message, ex);
}
throw ex;
}
/// <summary>
/// Downloads the contents of a given url into a MemoryStream
/// </summary>
@ -519,19 +588,6 @@ namespace MediaBrowser.Common.Implementations.HttpClientManager
return Post(url, postData, null, cancellationToken);
}
/// <summary>
/// Gets the temp file.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <param name="userAgent">The user agent.</param>
/// <returns>Task{System.String}.</returns>
public Task<string> GetTempFile(string url, CancellationToken cancellationToken, IProgress<double> progress, string userAgent = null)
{
return GetTempFile(url, null, cancellationToken, progress, userAgent);
}
/// <summary>
/// Gets the memory stream.
/// </summary>

View File

@ -71,7 +71,14 @@ namespace MediaBrowser.Common.Implementations.Updates
var target = Path.Combine(isArchive ? _appPaths.TempUpdatePath : _appPaths.PluginsPath, package.targetFilename);
// Download to temporary file so that, if interrupted, it won't destroy the existing installation
var tempFile = await _httpClient.GetTempFile(package.sourceUrl, cancellationToken, progress).ConfigureAwait(false);
var tempFile = await _httpClient.GetTempFile(new HttpRequestOptions
{
Url = package.sourceUrl,
CancellationToken = cancellationToken,
Progress = progress,
MaxResumeCount = 3
}).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();

View File

@ -61,6 +61,7 @@
<Compile Include="IO\StreamDefaults.cs" />
<Compile Include="Net\BasePeriodicWebSocketListener.cs" />
<Compile Include="Configuration\IApplicationPaths.cs" />
<Compile Include="Net\HttpRequestOptions.cs" />
<Compile Include="Net\IHttpResultFactory.cs" />
<Compile Include="Net\IServerManager.cs" />
<Compile Include="Net\IWebSocketListener.cs" />

View File

@ -0,0 +1,47 @@
using System;
using System.Threading;
namespace MediaBrowser.Common.Net
{
/// <summary>
/// Class HttpRequestOptions
/// </summary>
public class HttpRequestOptions
{
/// <summary>
/// Gets or sets the URL.
/// </summary>
/// <value>The URL.</value>
public string Url { get; set; }
/// <summary>
/// Gets or sets the cancellation token.
/// </summary>
/// <value>The cancellation token.</value>
public CancellationToken CancellationToken { get; set; }
/// <summary>
/// Gets or sets the resource pool.
/// </summary>
/// <value>The resource pool.</value>
public SemaphoreSlim ResourcePool { get; set; }
/// <summary>
/// Gets or sets the user agent.
/// </summary>
/// <value>The user agent.</value>
public string UserAgent { get; set; }
/// <summary>
/// Gets or sets the max resume count.
/// </summary>
/// <value>The max resume count.</value>
public int MaxResumeCount { get; set; }
/// <summary>
/// Gets or sets the progress.
/// </summary>
/// <value>The progress.</value>
public IProgress<double> Progress { get; set; }
}
}

View File

@ -49,29 +49,15 @@ namespace MediaBrowser.Common.Net
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{Stream}.</returns>
Task<Stream> Post(string url, Dictionary<string, string> postData, CancellationToken cancellationToken);
/// <summary>
/// Downloads the contents of a given url into a temporary location
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="resourcePool">The resource pool.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <param name="userAgent">The user agent.</param>
/// <param name="options">The options.</param>
/// <returns>Task{System.String}.</returns>
/// <exception cref="System.ArgumentNullException">progress</exception>
/// <exception cref="MediaBrowser.Model.Net.HttpException"></exception>
Task<string> GetTempFile(string url, SemaphoreSlim resourcePool, CancellationToken cancellationToken, IProgress<double> progress, string userAgent = null);
/// <summary>
/// Gets the temp file.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="progress">The progress.</param>
/// <param name="userAgent">The user agent.</param>
/// <returns>Task{System.String}.</returns>
Task<string> GetTempFile(string url, CancellationToken cancellationToken, IProgress<double> progress, string userAgent = null);
Task<string> GetTempFile(HttpRequestOptions options);
/// <summary>
/// Downloads the contents of a given url into a MemoryStream

View File

@ -100,7 +100,7 @@ namespace MediaBrowser.Controller.Providers
{
var path = item.Images[image];
return IsInSameDirectory(item, path) && !item.ResolveArgs.GetMetaFileByPath(path).HasValue;
return IsInMetaLocation(item, path) && !item.ResolveArgs.GetMetaFileByPath(path).HasValue;
}).ToList();
// Now remove them from the dictionary
@ -122,7 +122,7 @@ namespace MediaBrowser.Controller.Providers
}
// Only validate paths from the same directory - need to copy to a list because we are going to potentially modify the collection below
var deletedImages = item.BackdropImagePaths.Where(path => IsInSameDirectory(item, path) && !item.ResolveArgs.GetMetaFileByPath(path).HasValue).ToList();
var deletedImages = item.BackdropImagePaths.Where(path => IsInMetaLocation(item, path) && !item.ResolveArgs.GetMetaFileByPath(path).HasValue).ToList();
// Now remove them from the dictionary
foreach (var path in deletedImages)
@ -137,7 +137,7 @@ namespace MediaBrowser.Controller.Providers
/// <param name="item">The item.</param>
/// <param name="path">The path.</param>
/// <returns><c>true</c> if [is in same directory] [the specified item]; otherwise, <c>false</c>.</returns>
private bool IsInSameDirectory(BaseItem item, string path)
private bool IsInMetaLocation(BaseItem item, string path)
{
return string.Equals(Path.GetDirectoryName(path), item.MetaLocation, StringComparison.OrdinalIgnoreCase);
}

View File

@ -397,24 +397,27 @@ var Dashboard = {
if (!info.HasPendingRestart) {
Dashboard.reloadPage();
} else {
Dashboard.reloadPageWhenServerAvailable(retryCount);
Dashboard.retryReload(retryCount);
}
}).fail(function () {
setTimeout(function () {
retryCount = retryCount || 0;
retryCount++;
if (retryCount < 10) {
Dashboard.reloadPageWhenServerAvailable(retryCount);
} else {
Dashboard.suppressAjaxErrors = false;
}
}, 500);
}).fail(function() {
Dashboard.retryReload(retryCount);
});
},
retryReload: function (retryCount) {
setTimeout(function () {
retryCount = retryCount || 0;
retryCount++;
if (retryCount < 10) {
Dashboard.reloadPageWhenServerAvailable(retryCount);
} else {
Dashboard.suppressAjaxErrors = false;
}
}, 500);
},
getPosterViewHtml: function (options) {

View File

@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2011/08/nuspec.xsd">
<metadata>
<id>MediaBrowser.Common.Internal</id>
<version>3.0.45</version>
<version>3.0.47</version>
<title>MediaBrowser.Common.Internal</title>
<authors>Luke</authors>
<owners>ebr,Luke,scottisafool</owners>
@ -12,7 +12,7 @@
<description>Contains common components shared by Media Browser Theatre and Media Browser Server. Not intended for plugin developer consumption.</description>
<copyright>Copyright © Media Browser 2013</copyright>
<dependencies>
<dependency id="MediaBrowser.Common" version="3.0.45" />
<dependency id="MediaBrowser.Common" version="3.0.47" />
<dependency id="NLog" version="2.0.0.2000" />
<dependency id="ServiceStack.Text" version="3.9.38" />
<dependency id="protobuf-net" version="2.0.0.621" />

View File

@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MediaBrowser.Common</id>
<version>3.0.45</version>
<version>3.0.47</version>
<title>MediaBrowser.Common</title>
<authors>Media Browser Team</authors>
<owners>ebr,Luke,scottisafool</owners>

View File

@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>MediaBrowser.Server.Core</id>
<version>3.0.45</version>
<version>3.0.47</version>
<title>Media Browser.Server.Core</title>
<authors>Media Browser Team</authors>
<owners>ebr,Luke,scottisafool</owners>
@ -12,7 +12,7 @@
<description>Contains core components required to build plugins for Media Browser Server.</description>
<copyright>Copyright © Media Browser 2013</copyright>
<dependencies>
<dependency id="MediaBrowser.Common" version="3.0.45" />
<dependency id="MediaBrowser.Common" version="3.0.47" />
</dependencies>
</metadata>
<files>