mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-11-22 03:02:35 +01:00
Refactor subtitle file location code in FileUtil
This commit simplifies the subtitle file location code in FileUtil by removing unnecessary nested functions. It changes the way the search is done in known subtitle directories and handles the case if a video file is sent with a full path. This results in cleaner and more manageable code. Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
parent
82ac6dec92
commit
f256b59691
@ -711,34 +711,30 @@ namespace Nikse.SubtitleEdit.Core.Common
|
||||
|
||||
public static string TryLocateSubtitleFile(string path, string videoFileName)
|
||||
{
|
||||
// search in these directories
|
||||
// search in these subdirectories: \Subs;\Sub;\Subtitles;
|
||||
var knownSubtitleDirectories = new[]
|
||||
{
|
||||
path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles")
|
||||
};
|
||||
foreach (var knownSubtitleDirectory in knownSubtitleDirectories)
|
||||
|
||||
// handles if video file was sent with full path
|
||||
if (Path.IsPathRooted(videoFileName))
|
||||
{
|
||||
var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileName);
|
||||
if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory))
|
||||
{
|
||||
return subtitleFileFromKnownDirectory;
|
||||
}
|
||||
videoFileName = Path.GetFileName(videoFileName);
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
|
||||
string LocateSubtitle(string localPath, in string localVideoFileName)
|
||||
foreach (var knownSubtitleDirectory in knownSubtitleDirectories)
|
||||
{
|
||||
if (!Directory.Exists(localPath))
|
||||
if (!Directory.Exists(knownSubtitleDirectory))
|
||||
{
|
||||
return string.Empty;
|
||||
continue;
|
||||
}
|
||||
|
||||
// try to locate subtitle file that has the same name as the video file
|
||||
var defaultSubtitles = new[]
|
||||
{
|
||||
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".ass")),
|
||||
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".srt"))
|
||||
Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".ass")),
|
||||
Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".srt"))
|
||||
};
|
||||
foreach (var defaultSubtitle in defaultSubtitles)
|
||||
{
|
||||
@ -748,17 +744,17 @@ namespace Nikse.SubtitleEdit.Core.Common
|
||||
}
|
||||
}
|
||||
|
||||
var assEnumerable = Directory.EnumerateFiles(localPath, "*.ass", SearchOption.TopDirectoryOnly);
|
||||
var subripEnumerable = Directory.EnumerateFiles(localPath, "*.srt", SearchOption.TopDirectoryOnly);
|
||||
var subtitleFile = assEnumerable.Concat(subripEnumerable).FirstOrDefault();
|
||||
// subtitle file found in top directory
|
||||
// get first subtitle in path with extension .ass or .srt
|
||||
var assEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.ass", SearchOption.TopDirectoryOnly);
|
||||
var subRipEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.srt", SearchOption.TopDirectoryOnly);
|
||||
var subtitleFile = assEnumerable.Concat(subRipEnumerable).FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(subtitleFile))
|
||||
{
|
||||
return subtitleFile;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user