handle more duplicate episode paths when deleting

pull/702/head
Luke Pulverenti 11 years ago
parent 145285784b
commit 36210922e5

@ -74,10 +74,16 @@ namespace MediaBrowser.Controller.LiveTv
public string ChannelId { get; set; } public string ChannelId { get; set; }
/// <summary> /// <summary>
/// Gets or sets the timer identifier. /// Gets or sets the recording identifier.
/// </summary> /// </summary>
/// <value>The timer identifier.</value> /// <value>The recording identifier.</value>
public string TimerId { get; set; } public string RecordingId { get; set; }
/// <summary>
/// Gets or sets the name of the program.
/// </summary>
/// <value>The name of the program.</value>
public string ProgramName { get; set; }
/// <summary> /// <summary>
/// Gets or sets the clients. /// Gets or sets the clients.

@ -1,20 +1,21 @@
using System; using MediaBrowser.Common.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities.TV; using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.FileOrganization; using MediaBrowser.Controller.FileOrganization;
using MediaBrowser.Controller.IO; using MediaBrowser.Controller.IO;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Providers;
using MediaBrowser.Controller.Resolvers;
using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.FileOrganization; using MediaBrowser.Model.FileOrganization;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.FileOrganization namespace MediaBrowser.Server.Implementations.FileOrganization
@ -154,9 +155,10 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
_logger.Info("Sorting file {0} to new path {1}", sourcePath, newPath); _logger.Info("Sorting file {0} to new path {1}", sourcePath, newPath);
result.TargetPath = newPath; result.TargetPath = newPath;
var existing = GetDuplicatePaths(result.TargetPath, series, seasonNumber, episodeNumber); var fileExists = File.Exists(result.TargetPath);
var otherDuplicatePaths = GetOtherDuplicatePaths(result.TargetPath, series, seasonNumber, episodeNumber, endingEpiosdeNumber);
if (!overwriteExisting && existing.Count > 0) if (!overwriteExisting && (fileExists || otherDuplicatePaths.Count > 0))
{ {
result.Status = FileSortingStatus.SkippedExisting; result.Status = FileSortingStatus.SkippedExisting;
result.StatusMessage = string.Empty; result.StatusMessage = string.Empty;
@ -164,18 +166,67 @@ namespace MediaBrowser.Server.Implementations.FileOrganization
} }
PerformFileSorting(options, result); PerformFileSorting(options, result);
if (overwriteExisting)
{
foreach (var path in otherDuplicatePaths)
{
_logger.Debug("Removing duplicate episode {0}", path);
try
{
File.Delete(path);
}
catch (IOException ex)
{
_logger.ErrorException("Error removing duplicate episode", ex, path);
} }
}
}
}
private List<string> GetOtherDuplicatePaths(string targetPath, Series series, int seasonNumber, int episodeNumber, int? endingEpisodeNumber)
{
var episodePaths = series.RecursiveChildren
.OfType<Episode>()
.Where(i =>
{
var locationType = i.LocationType;
private List<string> GetDuplicatePaths(string targetPath, Series series, int seasonNumber, int episodeNumber) // Must be file system based and match exactly
if (locationType != LocationType.Remote &&
locationType != LocationType.Virtual &&
i.ParentIndexNumber.HasValue &&
i.ParentIndexNumber.Value == seasonNumber &&
i.IndexNumber.HasValue &&
i.IndexNumber.Value == episodeNumber)
{ {
var list = new List<string>();
if (File.Exists(targetPath)) if (endingEpisodeNumber.HasValue || i.IndexNumberEnd.HasValue)
{ {
list.Add(targetPath); return endingEpisodeNumber.HasValue && i.IndexNumberEnd.HasValue &&
endingEpisodeNumber.Value == i.IndexNumberEnd.Value;
} }
return list; return true;
}
return false;
})
.Select(i => i.Path)
.ToList();
var folder = Path.GetDirectoryName(targetPath);
var targetFileNameWithoutExtension = Path.GetFileNameWithoutExtension(targetPath);
var filesOfOtherExtensions = Directory.EnumerateFiles(folder, "*", SearchOption.TopDirectoryOnly)
.Where(i => EntityResolutionHelper.IsVideoFile(i) && string.Equals(Path.GetFileNameWithoutExtension(i), targetFileNameWithoutExtension, StringComparison.OrdinalIgnoreCase));
episodePaths.AddRange(filesOfOtherExtensions);
return episodePaths.Where(i => !string.Equals(i, targetPath, StringComparison.OrdinalIgnoreCase))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
} }
private void PerformFileSorting(TvFileOrganizationOptions options, FileOrganizationResult result) private void PerformFileSorting(TvFileOrganizationOptions options, FileOrganizationResult result)

Loading…
Cancel
Save