diff --git a/src/NzbDrone.Core/Datastore/Migration/121_update_filedate_config.cs b/src/NzbDrone.Core/Datastore/Migration/121_update_filedate_config.cs new file mode 100644 index 000000000..a45b6530d --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/121_update_filedate_config.cs @@ -0,0 +1,67 @@ +using System.Data; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; +using System.Text; +using System.Text.RegularExpressions; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(121)] + public class update_filedate_config : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(SetTitleSlug); + } + + private void SetTitleSlug(IDbConnection conn, IDbTransaction tran) + { + using (IDbCommand getSeriesCmd = conn.CreateCommand()) + { + getSeriesCmd.Transaction = tran; + getSeriesCmd.CommandText = @"SELECT Id, Value FROM Config WHERE Key = 'filedate'"; + using (IDataReader seriesReader = getSeriesCmd.ExecuteReader()) + { + while (seriesReader.Read()) + { + var id = seriesReader.GetInt32(0); + var value = seriesReader.GetString(1); + + using (IDbCommand updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = "UPDATE Config SET Value = 'Release' WHERE Id = ?"; + updateCmd.AddParameter(id); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + + public static string ToUrlSlug(string value) + { + //First to lower case + value = value.ToLowerInvariant(); + + //Remove all accents + var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(value); + value = Encoding.ASCII.GetString(bytes); + + //Replace spaces + value = Regex.Replace(value, @"\s", "-", RegexOptions.Compiled); + + //Remove invalid chars + value = Regex.Replace(value, @"[^a-z0-9\s-_]", "", RegexOptions.Compiled); + + //Trim dashes from end + value = value.Trim('-', '_'); + + //Replace double occurences of - or _ + value = Regex.Replace(value, @"([-_]){2,}", "$1", RegexOptions.Compiled); + + return value; + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core/MediaFiles/FileDateType.cs b/src/NzbDrone.Core/MediaFiles/FileDateType.cs index 6d78be960..45a8711b9 100644 --- a/src/NzbDrone.Core/MediaFiles/FileDateType.cs +++ b/src/NzbDrone.Core/MediaFiles/FileDateType.cs @@ -3,7 +3,7 @@ public enum FileDateType { None = 0, - LocalAirDate = 1, - UtcAirDate = 2 + Cinemas = 1, + Release = 2 } } diff --git a/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs b/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs index 67b415c20..f6589f554 100644 --- a/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpdateEpisodeFileService.cs @@ -49,7 +49,7 @@ namespace NzbDrone.Core.MediaFiles switch (_configService.FileDate) { - case FileDateType.LocalAirDate: + case FileDateType.Release: { var airDate = episodes.First().AirDate; var airTime = series.AirTime; @@ -62,7 +62,7 @@ namespace NzbDrone.Core.MediaFiles return ChangeFileDateToLocalAirDate(episodeFilePath, airDate, airTime); } - case FileDateType.UtcAirDate: + case FileDateType.Cinemas: { var airDateUtc = episodes.First().AirDateUtc; diff --git a/src/NzbDrone.Core/MediaFiles/UpdateMovieFileService.cs b/src/NzbDrone.Core/MediaFiles/UpdateMovieFileService.cs index af45d8831..6d6fdbbd8 100644 --- a/src/NzbDrone.Core/MediaFiles/UpdateMovieFileService.cs +++ b/src/NzbDrone.Core/MediaFiles/UpdateMovieFileService.cs @@ -20,7 +20,7 @@ namespace NzbDrone.Core.MediaFiles } public class UpdateMovieFileService : IUpdateMovieFileService, - IHandle + IHandle { private readonly IDiskProvider _diskProvider; private readonly IConfigService _configService; @@ -47,10 +47,60 @@ namespace NzbDrone.Core.MediaFiles { var movieFilePath = Path.Combine(movie.Path, movieFile.RelativePath); + switch (_configService.FileDate) + { + case FileDateType.Release: + { + var airDate = movie.PhysicalRelease; + + if (airDate == null) + { + return false; + } + + return ChangeFileDate(movieFilePath, airDate.Value); + } + + case FileDateType.Cinemas: + { + var airDate = movie.InCinemas; + + if (airDate == null) + { + return false; + } + + return ChangeFileDate(movieFilePath, airDate.Value); + } + } + + return false; + } + + private bool ChangeFileDate(string filePath, DateTime date) + { + DateTime oldDateTime = _diskProvider.FileGetLastWrite(filePath); + + if (!DateTime.Equals(date, oldDateTime)) + { + try + { + _diskProvider.FileSetLastWriteTime(filePath, date); + _logger.Debug("Date of file [{0}] changed from '{1}' to '{2}'", filePath, oldDateTime, date); + + return true; + } + + catch (Exception ex) + { + _logger.Warn(ex, "Unable to set date of file [" + filePath + "]"); + } + } + return false; } - public void Handle(SeriesScannedEvent message) + public void Handle(MovieScannedEvent message) { if (_configService.FileDate == FileDateType.None) { diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index e15d5d659..1c5d20daf 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -183,6 +183,7 @@ + diff --git a/src/UI/Settings/MediaManagement/FileManagement/FileManagementViewTemplate.hbs b/src/UI/Settings/MediaManagement/FileManagement/FileManagementViewTemplate.hbs index bee701a07..51dbb165b 100644 --- a/src/UI/Settings/MediaManagement/FileManagement/FileManagementViewTemplate.hbs +++ b/src/UI/Settings/MediaManagement/FileManagement/FileManagementViewTemplate.hbs @@ -1,98 +1,98 @@
- File Management - -
- - -
-
-
-
- -
- - -
-
-
-
- -
- - -
-
-
-
- -
- - -
- -
- -
- -
-
- -
- - -
- -
- -
- -
- -
+ File Management + +
+ + +
+
+
+
+ +
+ + +
+
+
+
+ +
+ + +
+
+
+
+ +
+ + +
+ +
+ +
+ +
+
+ +
+ + +
+ +
+ +
+ +
+ +