2014-03-08 19:01:51 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2014-07-23 23:43:54 +00:00
|
|
|
using System.IO;
|
2014-03-08 19:01:51 +00:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Disk;
|
2014-10-03 17:50:43 +00:00
|
|
|
using NzbDrone.Common.Exceptron;
|
2014-12-02 06:26:25 +00:00
|
|
|
using NzbDrone.Common.Extensions;
|
2014-07-23 23:43:54 +00:00
|
|
|
using NzbDrone.Common.Instrumentation.Extensions;
|
2014-03-08 19:01:51 +00:00
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
using NzbDrone.Core.MediaFiles.Events;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Tv;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.MediaFiles
|
|
|
|
{
|
|
|
|
public interface IUpdateEpisodeFileService
|
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes);
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class UpdateEpisodeFileService : IUpdateEpisodeFileService,
|
|
|
|
IHandle<SeriesScannedEvent>
|
|
|
|
{
|
|
|
|
private readonly IDiskProvider _diskProvider;
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly IEpisodeService _episodeService;
|
|
|
|
private readonly Logger _logger;
|
|
|
|
|
|
|
|
public UpdateEpisodeFileService(IDiskProvider diskProvider,
|
|
|
|
IConfigService configService,
|
|
|
|
IEpisodeService episodeService,
|
|
|
|
Logger logger)
|
|
|
|
{
|
|
|
|
_diskProvider = diskProvider;
|
|
|
|
_configService = configService;
|
|
|
|
_episodeService = episodeService;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
public void ChangeFileDateForFile(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
ChangeFileDate(episodeFile, series, episodes);
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
private bool ChangeFileDate(EpisodeFile episodeFile, Series series, List<Episode> episodes)
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-07-23 23:43:54 +00:00
|
|
|
var episodeFilePath = Path.Combine(series.Path, episodeFile.RelativePath);
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
switch (_configService.FileDate)
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
case FileDateType.LocalAirDate:
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-10-03 17:50:43 +00:00
|
|
|
var airDate = episodes.First().AirDate;
|
|
|
|
var airTime = series.AirTime;
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-10-03 17:50:43 +00:00
|
|
|
if (airDate.IsNullOrWhiteSpace() || airTime.IsNullOrWhiteSpace())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-10-03 17:50:43 +00:00
|
|
|
return ChangeFileDateToLocalAirDate(episodeFilePath, airDate, airTime);
|
|
|
|
}
|
2014-03-13 05:27:36 +00:00
|
|
|
|
2014-10-03 17:50:43 +00:00
|
|
|
case FileDateType.UtcAirDate:
|
2014-03-13 05:27:36 +00:00
|
|
|
{
|
2014-10-03 17:50:43 +00:00
|
|
|
var airDateUtc = episodes.First().AirDateUtc;
|
2014-03-13 05:27:36 +00:00
|
|
|
|
2014-10-03 17:50:43 +00:00
|
|
|
if (!airDateUtc.HasValue)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ChangeFileDateToUtcAirDate(episodeFilePath, airDateUtc.Value);
|
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
2014-03-13 05:27:36 +00:00
|
|
|
|
|
|
|
return false;
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
public void Handle(SeriesScannedEvent message)
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
if (_configService.FileDate == FileDateType.None)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var episodes = _episodeService.EpisodesWithFiles(message.Series.Id);
|
|
|
|
|
|
|
|
var episodeFiles = new List<EpisodeFile>();
|
|
|
|
var updated = new List<EpisodeFile>();
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
foreach (var group in episodes.GroupBy(e => e.EpisodeFileId))
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
var episodesInFile = group.Select(e => e).ToList();
|
|
|
|
var episodeFile = episodesInFile.First().EpisodeFile;
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
episodeFiles.Add(episodeFile);
|
|
|
|
|
|
|
|
if (ChangeFileDate(episodeFile, message.Series, episodesInFile))
|
|
|
|
{
|
|
|
|
updated.Add(episodeFile);
|
2014-10-03 17:50:43 +00:00
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
if (updated.Any())
|
|
|
|
{
|
|
|
|
_logger.ProgressDebug("Changed file date for {0} files of {1} in {2}", updated.Count, episodeFiles.Count, message.Series.Title);
|
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
_logger.ProgressDebug("No file dates changed for {0}", message.Series.Title);
|
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
private bool ChangeFileDateToLocalAirDate(string filePath, string fileDate, string fileTime)
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
DateTime airDate;
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
if (DateTime.TryParse(fileDate + ' ' + fileTime, out airDate))
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
|
|
|
// avoiding false +ve checks and set date skewing by not using UTC (Windows)
|
2014-10-14 21:31:35 +00:00
|
|
|
DateTime oldDateTime = _diskProvider.FileGetLastWrite(filePath);
|
2014-03-08 19:01:51 +00:00
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
if (!DateTime.Equals(airDate, oldDateTime))
|
2014-03-08 19:01:51 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, airDate);
|
|
|
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldDateTime, airDate);
|
|
|
|
|
|
|
|
return true;
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.WarnException("Unable to set date of file [" + filePath + "]", ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2014-03-13 05:27:36 +00:00
|
|
|
_logger.Debug("Could not create valid date to change file [{0}]", filePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool ChangeFileDateToUtcAirDate(string filePath, DateTime airDateUtc)
|
|
|
|
{
|
2014-10-14 21:31:35 +00:00
|
|
|
DateTime oldLastWrite = _diskProvider.FileGetLastWrite(filePath);
|
2014-03-13 05:27:36 +00:00
|
|
|
|
|
|
|
if (!DateTime.Equals(airDateUtc, oldLastWrite))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
_diskProvider.FileSetLastWriteTime(filePath, airDateUtc);
|
|
|
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldLastWrite, airDateUtc);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2014-10-03 17:50:43 +00:00
|
|
|
ex.ExceptronIgnoreOnMono();
|
2014-03-13 05:27:36 +00:00
|
|
|
_logger.WarnException("Unable to set date of file [" + filePath + "]", ex);
|
|
|
|
}
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 05:27:36 +00:00
|
|
|
return false;
|
2014-03-08 19:01:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|