Skip to content

Commit

Permalink
Auto-Organize: Fix PathTooLongException due to long EpisodeTitle #2
Browse files Browse the repository at this point in the history
  • Loading branch information
softworkz committed Oct 2, 2015
1 parent dd2cf20 commit 1722d7f
Showing 1 changed file with 42 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,21 +486,29 @@ private async Task<string> GetNewPath(string sourcePath, Series series, int seas

var newPath = GetSeasonFolderPath(series, seasonNumber, options);

var episodeFileName = GetEpisodeFileName(newPath.Length, sourcePath, series.Name, seasonNumber, episodeNumber, endingEpisodeNumber, episode.Name, options);

newPath = Path.Combine(newPath, episodeFileName);
// MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256
// Usually newPath would include the drive component, but use 256 to be sure
var maxFilenameLength = 256 - newPath.Length;

// Try to account for windows limitations by removing the episode title
if (newPath.Length > 255)
if (!newPath.EndsWith(@"\"))
{
var extension = Path.GetExtension(episodeFileName);
var fileName = Path.GetFileNameWithoutExtension(episodeFileName);
fileName = fileName.Replace(episode.Name, string.Empty, StringComparison.OrdinalIgnoreCase);
episodeFileName = Path.ChangeExtension(fileName, extension);
// Remove 1 for missing backslash combining path and filename
maxFilenameLength--;
}

// Remove additional 4 chars to prevent PathTooLongException for downloaded subtitles (eg. filename.ext.eng.srt)
maxFilenameLength -= 4;

newPath = Path.Combine(newPath, episodeFileName);
var episodeFileName = GetEpisodeFileName(sourcePath, series.Name, seasonNumber, episodeNumber, endingEpisodeNumber, episode.Name, options, maxFilenameLength);

if (string.IsNullOrEmpty(episodeFileName))
{
// cause failure
return string.Empty;
}

newPath = Path.Combine(newPath, episodeFileName);

return newPath;
}

Expand Down Expand Up @@ -543,7 +551,7 @@ private string GetSeasonFolderPath(Series series, int seasonNumber, TvFileOrgani
return Path.Combine(path, _fileSystem.GetValidFilename(seasonFolderName));
}

private string GetEpisodeFileName(int destPathLength, string sourcePath, string seriesName, int seasonNumber, int episodeNumber, int? endingEpisodeNumber, string episodeTitle, TvFileOrganizationOptions options)
private string GetEpisodeFileName(string sourcePath, string seriesName, int seasonNumber, int episodeNumber, int? endingEpisodeNumber, string episodeTitle, TvFileOrganizationOptions options, int? maxLength)
{
seriesName = _fileSystem.GetValidFilename(seriesName).Trim();
episodeTitle = _fileSystem.GetValidFilename(episodeTitle).Trim();
Expand Down Expand Up @@ -574,25 +582,33 @@ private string GetEpisodeFileName(int destPathLength, string sourcePath, string
.Replace("%0e", episodeNumber.ToString("00", _usCulture))
.Replace("%00e", episodeNumber.ToString("000", _usCulture));

// Add +1 because it is unsure if destpathlength includes a trailing backslash
int currentTotalLength = destPathLength + 1 + result.Length;

// MAX_PATH - trailing <NULL> charachter - drive component: 260 - 1 - 3 = 256
// Usually maxRemainingTitleLength would include the drive component, but use 256 to be sure
// Substract 3 for the temp token length (%#1, %#2 or %#3)
int maxRemainingTitleLength = 256 - currentTotalLength + 3;
string shortenedEpisodeTitle = string.Empty;

if (maxRemainingTitleLength > 5)
if (maxLength.HasValue && result.Contains("%#"))
{
// A title with fewer than 5 letters wouldn't be of much value
shortenedEpisodeTitle = episodeTitle.Substring(0, Math.Min(maxRemainingTitleLength, episodeTitle.Length));
// Substract 3 for the temp token length (%#1, %#2 or %#3)
int maxRemainingTitleLength = maxLength.Value - result.Length + 3;
string shortenedEpisodeTitle = string.Empty;

if (maxRemainingTitleLength > 5)
{
// A title with fewer than 5 letters wouldn't be of much value
shortenedEpisodeTitle = episodeTitle.Substring(0, Math.Min(maxRemainingTitleLength, episodeTitle.Length));
}

result = result.Replace("%#1", shortenedEpisodeTitle)
.Replace("%#2", shortenedEpisodeTitle.Replace(" ", "."))
.Replace("%#3", shortenedEpisodeTitle.Replace(" ", "_"));
}

return result.Replace("%#1", shortenedEpisodeTitle)
.Replace("%#2", shortenedEpisodeTitle.Replace(" ", "."))
.Replace("%#3", shortenedEpisodeTitle.Replace(" ", "_"));
if (maxLength.HasValue && result.Length > maxLength.Value)
{
// There may be cases where reducing the title length may still not be sufficient to
// stay below maxLength
var msg = string.Format("Unable to generate an episode file name shorter than {0} characters to constrain to the max path limit", maxLength);
_logger.Warn(msg);
return string.Empty;
}

return result;
}

private bool IsSameEpisode(string sourcePath, string newPath)
Expand Down

0 comments on commit 1722d7f

Please sign in to comment.