You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
148 lines
4.7 KiB
148 lines
4.7 KiB
8 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.IO;
|
||
|
using System.Linq;
|
||
|
using NLog;
|
||
|
using NzbDrone.Common.Disk;
|
||
|
using NzbDrone.Common.Exceptron;
|
||
|
using NzbDrone.Common.Extensions;
|
||
|
using NzbDrone.Common.Instrumentation.Extensions;
|
||
|
using NzbDrone.Core.Configuration;
|
||
|
using NzbDrone.Core.MediaFiles.Events;
|
||
|
using NzbDrone.Core.Messaging.Events;
|
||
|
using NzbDrone.Core.Tv;
|
||
|
|
||
|
namespace NzbDrone.Core.MediaFiles
|
||
|
{
|
||
|
public interface IUpdateMovieFileService
|
||
|
{
|
||
|
void ChangeFileDateForFile(MovieFile movieFile, Movie movie);
|
||
|
}
|
||
|
|
||
|
public class UpdateMovieFileService : IUpdateMovieFileService,
|
||
|
IHandle<SeriesScannedEvent>
|
||
|
{
|
||
|
private readonly IDiskProvider _diskProvider;
|
||
|
private readonly IConfigService _configService;
|
||
|
private readonly IMovieService _movieService;
|
||
|
private readonly Logger _logger;
|
||
|
|
||
|
public UpdateMovieFileService(IDiskProvider diskProvider,
|
||
|
IConfigService configService,
|
||
|
IMovieService movieService,
|
||
|
Logger logger)
|
||
|
{
|
||
|
_diskProvider = diskProvider;
|
||
|
_configService = configService;
|
||
|
_movieService = movieService;
|
||
|
_logger = logger;
|
||
|
}
|
||
|
|
||
|
public void ChangeFileDateForFile(MovieFile movieFile, Movie movie)
|
||
|
{
|
||
|
ChangeFileDate(movieFile, movie);
|
||
|
}
|
||
|
|
||
|
private bool ChangeFileDate(MovieFile movieFile, Movie movie)
|
||
|
{
|
||
|
var movieFilePath = Path.Combine(movie.Path, movieFile.RelativePath);
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public void Handle(SeriesScannedEvent message)
|
||
|
{
|
||
|
if (_configService.FileDate == FileDateType.None)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
/* var movies = _movieService.MoviesWithFiles(message.Series.Id);
|
||
|
|
||
|
var movieFiles = new List<MovieFile>();
|
||
|
var updated = new List<MovieFile>();
|
||
|
|
||
|
foreach (var group in movies.GroupBy(e => e.MovieFileId))
|
||
|
{
|
||
|
var moviesInFile = group.Select(e => e).ToList();
|
||
|
var movieFile = moviesInFile.First().MovieFile;
|
||
|
|
||
|
movieFiles.Add(movieFile);
|
||
|
|
||
|
if (ChangeFileDate(movieFile, message.Series, moviesInFile))
|
||
|
{
|
||
|
updated.Add(movieFile);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (updated.Any())
|
||
|
{
|
||
|
_logger.ProgressDebug("Changed file date for {0} files of {1} in {2}", updated.Count, movieFiles.Count, message.Series.Title);
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
|
_logger.ProgressDebug("No file dates changed for {0}", message.Series.Title);
|
||
|
}*/
|
||
|
}
|
||
|
|
||
|
private bool ChangeFileDateToLocalAirDate(string filePath, string fileDate, string fileTime)
|
||
|
{
|
||
|
DateTime airDate;
|
||
|
|
||
|
if (DateTime.TryParse(fileDate + ' ' + fileTime, out airDate))
|
||
|
{
|
||
|
// avoiding false +ve checks and set date skewing by not using UTC (Windows)
|
||
|
DateTime oldDateTime = _diskProvider.FileGetLastWrite(filePath);
|
||
|
|
||
|
if (!DateTime.Equals(airDate, oldDateTime))
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
_diskProvider.FileSetLastWriteTime(filePath, airDate);
|
||
|
_logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldDateTime, airDate);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.Warn(ex, "Unable to set date of file [" + filePath + "]");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
else
|
||
|
{
|
||
|
_logger.Debug("Could not create valid date to change file [{0}]", filePath);
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
private bool ChangeFileDateToUtcAirDate(string filePath, DateTime airDateUtc)
|
||
|
{
|
||
|
DateTime oldLastWrite = _diskProvider.FileGetLastWrite(filePath);
|
||
|
|
||
|
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)
|
||
|
{
|
||
|
ex.ExceptronIgnoreOnMono();
|
||
|
_logger.Warn(ex, "Unable to set date of file [" + filePath + "]");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|