disable chunked encoding for images

This commit is contained in:
Luke Pulverenti
2014-07-17 18:21:35 -04:00
parent 80eda34f9f
commit 06118307dd
21 changed files with 313 additions and 241 deletions

View File

@@ -117,27 +117,28 @@ namespace MediaBrowser.Server.Implementations.Drawing
}
public async Task ProcessImage(ImageProcessingOptions options, Stream toStream)
{
var file = await ProcessImage(options).ConfigureAwait(false);
using (var fileStream = _fileSystem.GetFileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, true))
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
}
}
public async Task<string> ProcessImage(ImageProcessingOptions options)
{
if (options == null)
{
throw new ArgumentNullException("options");
}
if (toStream == null)
{
throw new ArgumentNullException("toStream");
}
var originalImagePath = options.Image.Path;
if (options.HasDefaultOptions() && options.Enhancers.Count == 0 && !options.CropWhiteSpace)
{
// Just spit out the original file if all the options are default
using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
return;
}
return originalImagePath;
}
var dateModified = options.Image.DateModified;
@@ -166,30 +167,13 @@ namespace MediaBrowser.Server.Implementations.Drawing
if (options.HasDefaultOptionsWithoutSize() && newSize.Equals(originalImageSize) && options.Enhancers.Count == 0)
{
// Just spit out the original file if the new size equals the old
using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
return;
}
return originalImagePath;
}
var quality = options.Quality ?? 90;
var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor);
try
{
using (var fileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
return;
}
}
catch (IOException)
{
// Cache file doesn't exist or is currently being written to
}
var semaphore = GetLock(cacheFilePath);
await semaphore.WaitAsync().ConfigureAwait(false);
@@ -197,17 +181,12 @@ namespace MediaBrowser.Server.Implementations.Drawing
// Check again in case of lock contention
try
{
using (var fileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
if (File.Exists(cacheFilePath))
{
await fileStream.CopyToAsync(toStream).ConfigureAwait(false);
semaphore.Release();
return;
return cacheFilePath;
}
}
catch (IOException)
{
// Cache file doesn't exist or is currently being written to
}
catch
{
semaphore.Release();
@@ -218,37 +197,6 @@ namespace MediaBrowser.Server.Implementations.Drawing
{
var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed.HasValue;
//if (!hasPostProcessing)
//{
// using (var outputStream = await _mediaEncoder.EncodeImage(new ImageEncodingOptions
// {
// InputPath = originalImagePath,
// MaxHeight = options.MaxHeight,
// MaxWidth = options.MaxWidth,
// Height = options.Height,
// Width = options.Width,
// Quality = options.Quality,
// Format = options.OutputFormat == ImageOutputFormat.Original ? Path.GetExtension(originalImagePath).TrimStart('.') : options.OutputFormat.ToString().ToLower()
// }, CancellationToken.None).ConfigureAwait(false))
// {
// using (var outputMemoryStream = new MemoryStream())
// {
// // Save to the memory stream
// await outputStream.CopyToAsync(outputMemoryStream).ConfigureAwait(false);
// var bytes = outputMemoryStream.ToArray();
// await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
// // kick off a task to cache the result
// await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
// }
// return;
// }
//}
using (var fileStream = _fileSystem.GetFileStream(originalImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, true))
{
// Copy to memory stream to avoid Image locking file
@@ -289,18 +237,16 @@ namespace MediaBrowser.Server.Implementations.Drawing
var outputFormat = GetOutputFormat(originalImage, options.OutputFormat);
using (var outputMemoryStream = new MemoryStream())
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
// Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
{
// Save to the memory stream
thumbnail.Save(outputFormat, outputMemoryStream, quality);
var bytes = outputMemoryStream.ToArray();
await toStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
// kick off a task to cache the result
await CacheResizedImage(cacheFilePath, bytes).ConfigureAwait(false);
thumbnail.Save(outputFormat, cacheFileStream, quality);
}
return cacheFilePath;
}
}
@@ -324,9 +270,7 @@ namespace MediaBrowser.Server.Implementations.Drawing
{
try
{
var parentPath = Path.GetDirectoryName(cacheFilePath);
Directory.CreateDirectory(parentPath);
Directory.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
// Save to the cache location
using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, true))

View File

@@ -306,11 +306,15 @@ namespace MediaBrowser.Server.Implementations.HttpServer
throw new ArgumentNullException("path");
}
return GetStaticFileResult(requestContext, path, MimeTypes.GetMimeType(path), fileShare, responseHeaders, isHeadRequest);
return GetStaticFileResult(requestContext, path, MimeTypes.GetMimeType(path), null, fileShare, responseHeaders, isHeadRequest);
}
public object GetStaticFileResult(IRequest requestContext, string path, string contentType,
FileShare fileShare = FileShare.Read, IDictionary<string, string> responseHeaders = null,
public object GetStaticFileResult(IRequest requestContext,
string path,
string contentType,
TimeSpan? cacheCuration = null,
FileShare fileShare = FileShare.Read,
IDictionary<string, string> responseHeaders = null,
bool isHeadRequest = false)
{
if (string.IsNullOrEmpty(path))
@@ -327,7 +331,7 @@ namespace MediaBrowser.Server.Implementations.HttpServer
var cacheKey = path + dateModified.Ticks;
return GetStaticResult(requestContext, cacheKey.GetMD5(), dateModified, null, contentType, () => Task.FromResult(GetFileStream(path, fileShare)), responseHeaders, isHeadRequest);
return GetStaticResult(requestContext, cacheKey.GetMD5(), dateModified, cacheCuration, contentType, () => Task.FromResult(GetFileStream(path, fileShare)), responseHeaders, isHeadRequest);
}
/// <summary>

View File

@@ -319,5 +319,6 @@
"ButtonAudioTracks": "Audio Tracks",
"ButtonSubtitles": "Subtitles",
"ButtonScenes": "Scenes",
"ButtonQuality": "Quality"
"ButtonQuality": "Quality",
"HeaderNotifications": "Notifications"
}

View File

@@ -882,5 +882,12 @@
"LabelAppName": "App name",
"LabelAppNameExample": "Example: Sickbeard, NzbDrone",
"HeaderNewApiKeyHelp": "Grant an application permission to communicate with Media Browser.",
"ButtonEnterSupporterKey": "Enter supporter key"
"ButtonEnterSupporterKey": "Enter supporter key",
"HeaderHttpHeaders": "Http Headers",
"HeaderIdentificationHeader": "Identification Header",
"LabelValue": "Value:",
"LabelMatchType": "Match type:",
"OptionEquals": "Equals",
"OptionRegex": "Regex",
"OptionSubstring": "Substring"
}

View File

@@ -261,6 +261,7 @@
<Compile Include="Persistence\SqliteUserRepository.cs" />
<Compile Include="Sorting\StudioComparer.cs" />
<Compile Include="Sorting\VideoBitRateComparer.cs" />
<Compile Include="Sync\SyncManager.cs" />
<Compile Include="Themes\AppThemeManager.cs" />
<Compile Include="Udp\UdpMessageReceivedEventArgs.cs" />
<Compile Include="Udp\UdpServer.cs" />

View File

@@ -0,0 +1,52 @@
using MediaBrowser.Controller.Sync;
using MediaBrowser.Model.Querying;
using MediaBrowser.Model.Sync;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Sync
{
public class SyncManager : ISyncManager
{
public Task<List<SyncJob>> CreateJob(SyncJobRequest request)
{
throw new NotImplementedException();
}
public Task<SyncSchedule> CreateSchedule(SyncScheduleRequest request)
{
throw new NotImplementedException();
}
public QueryResult<SyncJob> GetJobs(SyncJobQuery query)
{
throw new NotImplementedException();
}
public QueryResult<SyncSchedule> GetSchedules(SyncScheduleQuery query)
{
throw new NotImplementedException();
}
public Task CancelJob(string id)
{
throw new NotImplementedException();
}
public Task CancelSchedule(string id)
{
throw new NotImplementedException();
}
public SyncJob GetJob(string id)
{
throw new NotImplementedException();
}
public SyncSchedule GetSchedule(string id)
{
throw new NotImplementedException();
}
}
}