Merge pull request #1980 from MediaBrowser/dev

fix DateModified not refreshing
This commit is contained in:
Luke 2016-07-24 12:48:08 -04:00 committed by GitHub
commit c2e2c1db3b
19 changed files with 137 additions and 206 deletions

View File

@ -347,7 +347,7 @@ namespace MediaBrowser.Api.Playback.Progressive
outputHeaders[item.Key] = item.Value;
}
Func<Stream,Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream);
Func<Stream,Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None);
return ResultFactory.GetAsyncStreamWriter(streamWriter, outputHeaders);
}

View File

@ -3,88 +3,12 @@ using ServiceStack.Web;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommonIO;
namespace MediaBrowser.Api.Playback.Progressive
{
public class ProgressiveStreamWriter : IStreamWriter, IHasOptions
{
private string Path { get; set; }
private ILogger Logger { get; set; }
private readonly IFileSystem _fileSystem;
private readonly TranscodingJob _job;
/// <summary>
/// The _options
/// </summary>
private readonly IDictionary<string, string> _options = new Dictionary<string, string>();
/// <summary>
/// Gets the options.
/// </summary>
/// <value>The options.</value>
public IDictionary<string, string> Options
{
get { return _options; }
}
/// <summary>
/// Initializes a new instance of the <see cref="ProgressiveStreamWriter" /> class.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="logger">The logger.</param>
/// <param name="fileSystem">The file system.</param>
public ProgressiveStreamWriter(string path, ILogger logger, IFileSystem fileSystem, TranscodingJob job)
{
Path = path;
Logger = logger;
_fileSystem = fileSystem;
_job = job;
}
/// <summary>
/// Writes to.
/// </summary>
/// <param name="responseStream">The response stream.</param>
public void WriteTo(Stream responseStream)
{
var task = WriteToAsync(responseStream);
Task.WaitAll(task);
}
/// <summary>
/// Writes to.
/// </summary>
/// <param name="responseStream">The response stream.</param>
public async Task WriteToAsync(Stream responseStream)
{
try
{
await new ProgressiveFileCopier(_fileSystem, _job, Logger).StreamFile(Path, responseStream).ConfigureAwait(false);
}
catch (IOException)
{
// These error are always the same so don't dump the whole stack trace
Logger.Error("Error streaming media. The client has most likely disconnected or transcoding has failed.");
throw;
}
catch (Exception ex)
{
Logger.ErrorException("Error streaming media. The client has most likely disconnected or transcoding has failed.", ex);
throw;
}
finally
{
if (_job != null)
{
ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);
}
}
}
}
public class ProgressiveFileCopier
{
private readonly IFileSystem _fileSystem;
@ -103,22 +27,18 @@ namespace MediaBrowser.Api.Playback.Progressive
_logger = logger;
}
public async Task StreamFile(string path, Stream outputStream)
public async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken)
{
var eofCount = 0;
long position = 0;
using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
{
while (eofCount < 15)
{
await CopyToInternal(fs, outputStream, BufferSize).ConfigureAwait(false);
var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, cancellationToken).ConfigureAwait(false);
var fsPosition = fs.Position;
var bytesRead = fsPosition - position;
//Logger.Debug("Streamed {0} bytes from file {1}", bytesRead, path);
//var position = fs.Position;
//_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);
if (bytesRead == 0)
{
@ -126,57 +46,36 @@ namespace MediaBrowser.Api.Playback.Progressive
{
eofCount++;
}
await Task.Delay(100).ConfigureAwait(false);
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
}
else
{
eofCount = 0;
}
position = fsPosition;
}
}
}
private async Task CopyToInternal(Stream source, Stream destination, int bufferSize)
private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)
{
var array = new byte[bufferSize];
int count;
while ((count = await source.ReadAsync(array, 0, array.Length).ConfigureAwait(false)) != 0)
byte[] buffer = new byte[bufferSize];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
{
//if (_job != null)
//{
// var didPause = false;
// var totalPauseTime = 0;
await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
// if (_job.IsUserPaused)
// {
// _logger.Debug("Pausing writing to network stream while user has paused playback.");
// while (_job.IsUserPaused && totalPauseTime < 30000)
// {
// didPause = true;
// var pauseTime = 500;
// totalPauseTime += pauseTime;
// await Task.Delay(pauseTime).ConfigureAwait(false);
// }
// }
// if (didPause)
// {
// _logger.Debug("Resuming writing to network stream due to user unpausing playback.");
// }
//}
await destination.WriteAsync(array, 0, count).ConfigureAwait(false);
_bytesWritten += count;
_bytesWritten += bytesRead;
totalBytesRead += bytesRead;
if (_job != null)
{
_job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten);
}
}
return totalBytesRead;
}
}
}

View File

@ -46,6 +46,12 @@ namespace MediaBrowser.Controller.Entities.Audio
}
}
[IgnoreDataMember]
public override bool EnableForceSaveOnDateModifiedChange
{
get { return true; }
}
public Audio()
{
Artists = new List<string>();

View File

@ -455,7 +455,10 @@ namespace MediaBrowser.Controller.Entities
public DateTime DateLastRefreshed { get; set; }
[IgnoreDataMember]
public DateTime? DateModifiedDuringLastRefresh { get; set; }
public virtual bool EnableForceSaveOnDateModifiedChange
{
get { return false; }
}
/// <summary>
/// The logger

View File

@ -34,6 +34,12 @@ namespace MediaBrowser.Controller.Entities
return SeriesName;
}
[IgnoreDataMember]
public override bool EnableForceSaveOnDateModifiedChange
{
get { return true; }
}
public Guid? FindSeriesId()
{
return SeriesId;

View File

@ -373,13 +373,6 @@ namespace MediaBrowser.Controller.Entities
if (currentChildren.TryGetValue(child.Id, out currentChild) && IsValidFromResolver(currentChild, child))
{
var currentChildLocationType = currentChild.LocationType;
if (currentChildLocationType != LocationType.Remote &&
currentChildLocationType != LocationType.Virtual)
{
currentChild.DateModified = child.DateModified;
}
await UpdateIsOffline(currentChild, false).ConfigureAwait(false);
validChildren.Add(currentChild);

View File

@ -4,6 +4,7 @@ using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
namespace MediaBrowser.Controller.Entities
{
@ -32,6 +33,12 @@ namespace MediaBrowser.Controller.Entities
locationType != LocationType.Virtual;
}
[IgnoreDataMember]
public override bool EnableForceSaveOnDateModifiedChange
{
get { return true; }
}
/// <summary>
/// Gets or sets the remote trailers.
/// </summary>
@ -42,6 +49,7 @@ namespace MediaBrowser.Controller.Entities
/// Gets the type of the media.
/// </summary>
/// <value>The type of the media.</value>
[IgnoreDataMember]
public override string MediaType
{
get { return Model.Entities.MediaType.Game; }

View File

@ -207,8 +207,6 @@ namespace MediaBrowser.Controller.Entities
/// <param name="image">The image.</param>
/// <param name="index">The index.</param>
void SetImage(ItemImageInfo image, int index);
DateTime? DateModifiedDuringLastRefresh { get; set; }
}
public static class HasImagesExtensions

View File

@ -17,7 +17,7 @@ namespace MediaBrowser.Controller.Entities
/// Gets the date modified.
/// </summary>
/// <value>The date modified.</value>
DateTime DateModified { get; }
DateTime DateModified { get; set; }
/// <summary>
/// Gets or sets the date last saved.
@ -51,5 +51,7 @@ namespace MediaBrowser.Controller.Entities
bool SupportsPeople { get; }
bool RequiresRefresh();
bool EnableForceSaveOnDateModifiedChange { get; }
}
}

View File

@ -51,6 +51,12 @@ namespace MediaBrowser.Controller.Entities
}
}
[IgnoreDataMember]
public override bool EnableForceSaveOnDateModifiedChange
{
get { return true; }
}
public override bool CanDownload()
{
return true;

View File

@ -58,6 +58,12 @@ namespace MediaBrowser.Controller.Entities
}
}
[IgnoreDataMember]
public override bool EnableForceSaveOnDateModifiedChange
{
get { return true; }
}
public int? TotalBitrate { get; set; }
public ExtraType? ExtraType { get; set; }

View File

@ -102,6 +102,12 @@ namespace MediaBrowser.Controller.Providers
{
var directory = Path.GetDirectoryName(path);
if (string.IsNullOrWhiteSpace(directory))
{
_logger.Debug("Parent path is null for {0}", path);
return null;
}
var dict = GetFileSystemDictionary(directory, false);
FileSystemMetadata entry;

View File

@ -143,6 +143,22 @@ namespace MediaBrowser.Providers.Manager
var beforeSaveResult = await BeforeSave(itemOfType, isFirstRefresh || refreshOptions.ReplaceAllMetadata || refreshOptions.MetadataRefreshMode == MetadataRefreshMode.FullRefresh || requiresRefresh, updateType).ConfigureAwait(false);
updateType = updateType | beforeSaveResult;
if (item.LocationType == LocationType.FileSystem)
{
var file = refreshOptions.DirectoryService.GetFile(item.Path);
if (file != null)
{
var fileLastWriteTime = file.LastWriteTimeUtc;
if (item.EnableForceSaveOnDateModifiedChange && fileLastWriteTime != item.DateModified)
{
Logger.Debug("Date modified for {0}. Old date {1} new date {2} Id {3}", item.Path, item.DateModified, fileLastWriteTime, item.Id);
requiresRefresh = true;
}
item.DateModified = fileLastWriteTime;
}
}
// Save if changes were made, or it's never been saved before
if (refreshOptions.ForceSave || updateType > ItemUpdateType.None || isFirstRefresh || refreshOptions.ReplaceAllMetadata || requiresRefresh)
{
@ -155,12 +171,10 @@ namespace MediaBrowser.Providers.Manager
if (hasRefreshedMetadata && hasRefreshedImages)
{
item.DateLastRefreshed = DateTime.UtcNow;
item.DateModifiedDuringLastRefresh = item.DateModified;
}
else
{
item.DateLastRefreshed = default(DateTime);
item.DateModifiedDuringLastRefresh = null;
}
// Save to database

View File

@ -167,9 +167,10 @@ namespace MediaBrowser.Providers.MediaInfo
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
{
if (item.DateModifiedDuringLastRefresh.HasValue)
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return item.DateModifiedDuringLastRefresh.Value != item.DateModified;
return true;
}
return false;

View File

@ -171,13 +171,11 @@ namespace MediaBrowser.Providers.MediaInfo
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
{
if (item.DateModifiedDuringLastRefresh.HasValue)
{
if (item.DateModifiedDuringLastRefresh.Value != item.DateModified)
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return true;
}
}
if (item.SupportsLocalMetadata)
{

View File

@ -195,13 +195,11 @@ namespace MediaBrowser.Providers.MediaInfo
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
{
if (item.DateModifiedDuringLastRefresh.HasValue)
{
if (item.DateModifiedDuringLastRefresh.Value != item.DateModified)
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return true;
}
}
return false;
}

View File

@ -154,9 +154,10 @@ namespace MediaBrowser.Providers.Photos
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService)
{
if (item.DateModifiedDuringLastRefresh.HasValue)
var file = directoryService.GetFile(item.Path);
if (file != null && file.LastWriteTimeUtc != item.DateModified)
{
return item.DateModifiedDuringLastRefresh.Value != item.DateModified;
return true;
}
return false;

View File

@ -44,7 +44,6 @@ namespace MediaBrowser.Server.Implementations.Library
// Make sure DateCreated and DateModified have values
var fileInfo = directoryService.GetFile(item.Path);
item.DateModified = fileSystem.GetLastWriteTimeUtc(fileInfo);
SetDateCreated(item, fileSystem, fileInfo);
EnsureName(item, fileInfo);
@ -80,7 +79,7 @@ namespace MediaBrowser.Server.Implementations.Library
item.GetParents().Any(i => i.IsLocked);
// Make sure DateCreated and DateModified have values
EnsureDates(fileSystem, item, args, true);
EnsureDates(fileSystem, item, args);
}
/// <summary>
@ -125,8 +124,7 @@ namespace MediaBrowser.Server.Implementations.Library
/// <param name="fileSystem">The file system.</param>
/// <param name="item">The item.</param>
/// <param name="args">The args.</param>
/// <param name="includeCreationTime">if set to <c>true</c> [include creation time].</param>
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args, bool includeCreationTime)
private static void EnsureDates(IFileSystem fileSystem, BaseItem item, ItemResolveArgs args)
{
if (fileSystem == null)
{
@ -147,36 +145,23 @@ namespace MediaBrowser.Server.Implementations.Library
var childData = args.IsDirectory ? args.GetFileSystemEntryByPath(item.Path) : null;
if (childData != null)
{
if (includeCreationTime)
{
SetDateCreated(item, fileSystem, childData);
}
item.DateModified = fileSystem.GetLastWriteTimeUtc(childData);
}
else
{
var fileData = fileSystem.GetFileSystemInfo(item.Path);
if (fileData.Exists)
{
if (includeCreationTime)
{
SetDateCreated(item, fileSystem, fileData);
}
item.DateModified = fileSystem.GetLastWriteTimeUtc(fileData);
}
}
}
else
{
if (includeCreationTime)
{
SetDateCreated(item, fileSystem, args.FileInfo);
}
item.DateModified = fileSystem.GetLastWriteTimeUtc(args.FileInfo);
}
}
private static void SetDateCreated(BaseItem item, IFileSystem fileSystem, FileSystemMetadata info)

View File

@ -257,7 +257,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
_connection.AddColumn(Logger, "TypedBaseItems", "TrailerTypes", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "CriticRating", "Float");
_connection.AddColumn(Logger, "TypedBaseItems", "CriticRatingSummary", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "DateModifiedDuringLastRefresh", "DATETIME");
_connection.AddColumn(Logger, "TypedBaseItems", "InheritedTags", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "CleanName", "Text");
_connection.AddColumn(Logger, "TypedBaseItems", "PresentationUniqueKey", "Text");
@ -402,7 +401,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
"Tags",
"SourceType",
"TrailerTypes",
"DateModifiedDuringLastRefresh",
"OriginalTitle",
"PrimaryVersionId",
"DateLastMediaAdded",
@ -523,7 +521,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
"TrailerTypes",
"CriticRating",
"CriticRatingSummary",
"DateModifiedDuringLastRefresh",
"InheritedTags",
"CleanName",
"PresentationUniqueKey",
@ -902,15 +899,6 @@ namespace MediaBrowser.Server.Implementations.Persistence
_saveItemCommand.GetParameter(index++).Value = item.CriticRating;
_saveItemCommand.GetParameter(index++).Value = item.CriticRatingSummary;
if (!item.DateModifiedDuringLastRefresh.HasValue || item.DateModifiedDuringLastRefresh.Value == default(DateTime))
{
_saveItemCommand.GetParameter(index++).Value = null;
}
else
{
_saveItemCommand.GetParameter(index++).Value = item.DateModifiedDuringLastRefresh.Value;
}
var inheritedTags = item.GetInheritedTags();
if (inheritedTags.Count > 0)
{
@ -1370,88 +1358,101 @@ namespace MediaBrowser.Server.Implementations.Persistence
}
}
if (!reader.IsDBNull(51))
{
item.DateModifiedDuringLastRefresh = reader.GetDateTime(51).ToUniversalTime();
}
var index = 51;
if (!reader.IsDBNull(52))
if (!reader.IsDBNull(index))
{
item.OriginalTitle = reader.GetString(52);
item.OriginalTitle = reader.GetString(index);
}
index++;
var video = item as Video;
if (video != null)
{
if (!reader.IsDBNull(53))
if (!reader.IsDBNull(index))
{
video.PrimaryVersionId = reader.GetString(53);
video.PrimaryVersionId = reader.GetString(index);
}
}
index++;
var folder = item as Folder;
if (folder != null && !reader.IsDBNull(54))
if (folder != null && !reader.IsDBNull(index))
{
folder.DateLastMediaAdded = reader.GetDateTime(54).ToUniversalTime();
folder.DateLastMediaAdded = reader.GetDateTime(index).ToUniversalTime();
}
index++;
if (!reader.IsDBNull(55))
if (!reader.IsDBNull(index))
{
item.Album = reader.GetString(55);
item.Album = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(56))
if (!reader.IsDBNull(index))
{
item.CriticRating = reader.GetFloat(56);
item.CriticRating = reader.GetFloat(index);
}
index++;
if (!reader.IsDBNull(57))
if (!reader.IsDBNull(index))
{
item.CriticRatingSummary = reader.GetString(57);
item.CriticRatingSummary = reader.GetString(index);
}
index++;
if (!reader.IsDBNull(58))
if (!reader.IsDBNull(index))
{
item.IsVirtualItem = reader.GetBoolean(58);
item.IsVirtualItem = reader.GetBoolean(index);
}
index++;
var hasSeries = item as IHasSeries;
if (hasSeries != null)
{
if (!reader.IsDBNull(59))
if (!reader.IsDBNull(index))
{
hasSeries.SeriesName = reader.GetString(59);
hasSeries.SeriesName = reader.GetString(index);
}
}
index++;
var episode = item as Episode;
if (episode != null)
{
if (!reader.IsDBNull(60))
if (!reader.IsDBNull(index))
{
episode.SeasonName = reader.GetString(60);
episode.SeasonName = reader.GetString(index);
}
if (!reader.IsDBNull(61))
index++;
if (!reader.IsDBNull(index))
{
episode.SeasonId = reader.GetGuid(61);
episode.SeasonId = reader.GetGuid(index);
}
}
else
{
index++;
}
index++;
if (hasSeries != null)
{
if (!reader.IsDBNull(62))
if (!reader.IsDBNull(index))
{
hasSeries.SeriesId = reader.GetGuid(62);
hasSeries.SeriesId = reader.GetGuid(index);
}
}
index++;
if (hasSeries != null)
{
if (!reader.IsDBNull(63))
if (!reader.IsDBNull(index))
{
hasSeries.SeriesSortName = reader.GetString(63);
hasSeries.SeriesSortName = reader.GetString(index);
}
}
index++;
return item;
}