pass along date modified

This commit is contained in:
Luke Pulverenti 2016-07-17 12:59:40 -04:00
parent 537e774f5f
commit b6979fa28a
4 changed files with 36 additions and 26 deletions

View File

@ -163,7 +163,7 @@ namespace Emby.Drawing
return _imageEncoder.SupportedOutputFormats; return _imageEncoder.SupportedOutputFormats;
} }
public async Task<Tuple<string, string>> ProcessImage(ImageProcessingOptions options) public async Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingOptions options)
{ {
if (options == null) if (options == null)
{ {
@ -178,14 +178,13 @@ namespace Emby.Drawing
} }
var originalImagePath = originalImage.Path; var originalImagePath = originalImage.Path;
var dateModified = originalImage.DateModified;
if (!_imageEncoder.SupportsImageEncoding) if (!_imageEncoder.SupportsImageEncoding)
{ {
return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
} }
var dateModified = originalImage.DateModified;
if (options.CropWhiteSpace && _imageEncoder.SupportsImageEncoding) if (options.CropWhiteSpace && _imageEncoder.SupportsImageEncoding)
{ {
var tuple = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false); var tuple = await GetWhitespaceCroppedImage(originalImagePath, dateModified).ConfigureAwait(false);
@ -211,7 +210,7 @@ namespace Emby.Drawing
if (options.HasDefaultOptions(originalImagePath)) if (options.HasDefaultOptions(originalImagePath))
{ {
// Just spit out the original file if all the options are default // Just spit out the original file if all the options are default
return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
} }
ImageSize? originalImageSize; ImageSize? originalImageSize;
@ -221,7 +220,7 @@ namespace Emby.Drawing
if (options.HasDefaultOptions(originalImagePath, originalImageSize.Value)) if (options.HasDefaultOptions(originalImagePath, originalImageSize.Value))
{ {
// Just spit out the original file if all the options are default // Just spit out the original file if all the options are default
return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
} }
} }
catch catch
@ -235,10 +234,6 @@ namespace Emby.Drawing
var outputFormat = GetOutputFormat(options.SupportedOutputFormats[0]); var outputFormat = GetOutputFormat(options.SupportedOutputFormats[0]);
var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor, options.ForegroundLayer); var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, outputFormat, options.AddPlayedIndicator, options.PercentPlayed, options.UnplayedCount, options.BackgroundColor, options.ForegroundLayer);
var semaphore = GetLock(cacheFilePath);
await semaphore.WaitAsync().ConfigureAwait(false);
var imageProcessingLockTaken = false; var imageProcessingLockTaken = false;
try try
@ -251,15 +246,20 @@ namespace Emby.Drawing
var newHeight = Convert.ToInt32(newSize.Height); var newHeight = Convert.ToInt32(newSize.Height);
_fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath)); _fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath));
var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N"));
_fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
imageProcessingLockTaken = true; imageProcessingLockTaken = true;
_imageEncoder.EncodeImage(originalImagePath, cacheFilePath, AutoOrient(options.Item), newWidth, newHeight, quality, options, outputFormat); _imageEncoder.EncodeImage(originalImagePath, tmpPath, AutoOrient(options.Item), newWidth, newHeight, quality, options, outputFormat);
CopyFile(tmpPath, cacheFilePath);
return new Tuple<string, string, DateTime>(tmpPath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(tmpPath));
} }
return new Tuple<string, string>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath)); return new Tuple<string, string, DateTime>(cacheFilePath, GetMimeType(outputFormat, cacheFilePath), _fileSystem.GetLastWriteTimeUtc(cacheFilePath));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -267,7 +267,7 @@ namespace Emby.Drawing
_logger.ErrorException("Error encoding image", ex); _logger.ErrorException("Error encoding image", ex);
// Just spit out the original file if all the options are default // Just spit out the original file if all the options are default
return new Tuple<string, string>(originalImagePath, MimeTypes.GetMimeType(originalImagePath)); return new Tuple<string, string, DateTime>(originalImagePath, MimeTypes.GetMimeType(originalImagePath), dateModified);
} }
finally finally
{ {
@ -275,8 +275,18 @@ namespace Emby.Drawing
{ {
_imageProcessingSemaphore.Release(); _imageProcessingSemaphore.Release();
} }
}
}
private void CopyFile(string src, string destination)
{
try
{
File.Copy(src, destination, true);
}
catch
{
semaphore.Release();
} }
} }
@ -412,14 +422,9 @@ namespace Emby.Drawing
var croppedImagePath = GetCachePath(CroppedWhitespaceImageCachePath, name, Path.GetExtension(originalImagePath)); var croppedImagePath = GetCachePath(CroppedWhitespaceImageCachePath, name, Path.GetExtension(originalImagePath));
var semaphore = GetLock(croppedImagePath);
await semaphore.WaitAsync().ConfigureAwait(false);
// Check again in case of contention // Check again in case of contention
if (_fileSystem.FileExists(croppedImagePath)) if (_fileSystem.FileExists(croppedImagePath))
{ {
semaphore.Release();
return GetResult(croppedImagePath); return GetResult(croppedImagePath);
} }
@ -428,11 +433,15 @@ namespace Emby.Drawing
try try
{ {
_fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath)); _fileSystem.CreateDirectory(Path.GetDirectoryName(croppedImagePath));
var tmpPath = Path.Combine(_appPaths.TempDirectory, Guid.NewGuid().ToString("N"));
_fileSystem.CreateDirectory(Path.GetDirectoryName(tmpPath));
await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false); await _imageProcessingSemaphore.WaitAsync().ConfigureAwait(false);
imageProcessingLockTaken = true; imageProcessingLockTaken = true;
_imageEncoder.CropWhiteSpace(originalImagePath, croppedImagePath); _imageEncoder.CropWhiteSpace(originalImagePath, tmpPath);
CopyFile(tmpPath, croppedImagePath);
return GetResult(tmpPath);
} }
catch (NotImplementedException) catch (NotImplementedException)
{ {
@ -452,11 +461,7 @@ namespace Emby.Drawing
{ {
_imageProcessingSemaphore.Release(); _imageProcessingSemaphore.Release();
} }
semaphore.Release();
} }
return GetResult(croppedImagePath);
} }
private Tuple<string, DateTime> GetResult(string path) private Tuple<string, DateTime> GetResult(string path)

View File

@ -638,6 +638,7 @@ namespace MediaBrowser.Api.Images
CacheDuration = cacheDuration, CacheDuration = cacheDuration,
ResponseHeaders = headers, ResponseHeaders = headers,
ContentType = imageResult.Item2, ContentType = imageResult.Item2,
DateLastModified = imageResult.Item3,
IsHeadRequest = isHeadRequest, IsHeadRequest = isHeadRequest,
Path = imageResult.Item1, Path = imageResult.Item1,

View File

@ -84,7 +84,7 @@ namespace MediaBrowser.Controller.Drawing
/// </summary> /// </summary>
/// <param name="options">The options.</param> /// <param name="options">The options.</param>
/// <returns>Task.</returns> /// <returns>Task.</returns>
Task<Tuple<string, string>> ProcessImage(ImageProcessingOptions options); Task<Tuple<string, string, DateTime>> ProcessImage(ImageProcessingOptions options);
/// <summary> /// <summary>
/// Gets the enhanced image. /// Gets the enhanced image.

View File

@ -331,7 +331,11 @@ namespace MediaBrowser.Server.Implementations.HttpServer
options.ContentType = MimeTypes.GetMimeType(path); options.ContentType = MimeTypes.GetMimeType(path);
} }
if (!options.DateLastModified.HasValue)
{
options.DateLastModified = _fileSystem.GetLastWriteTimeUtc(path); options.DateLastModified = _fileSystem.GetLastWriteTimeUtc(path);
}
var cacheKey = path + options.DateLastModified.Value.Ticks; var cacheKey = path + options.DateLastModified.Value.Ticks;
options.CacheKey = cacheKey.GetMD5(); options.CacheKey = cacheKey.GetMD5();