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)
|
public static string TryLocateSubtitleFile(string path, string videoFileName)
|
||||||
{
|
{
|
||||||
// search in these directories
|
// search in these subdirectories: \Subs;\Sub;\Subtitles;
|
||||||
var knownSubtitleDirectories = new[]
|
var knownSubtitleDirectories = new[]
|
||||||
{
|
{
|
||||||
path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles")
|
path, Path.Combine(path, "Subs"), Path.Combine(path, "Sub"), Path.Combine(path, "Subtitles")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// handles if video file was sent with full path
|
||||||
|
if (Path.IsPathRooted(videoFileName))
|
||||||
|
{
|
||||||
|
videoFileName = Path.GetFileName(videoFileName);
|
||||||
|
}
|
||||||
|
|
||||||
foreach (var knownSubtitleDirectory in knownSubtitleDirectories)
|
foreach (var knownSubtitleDirectory in knownSubtitleDirectories)
|
||||||
{
|
{
|
||||||
var subtitleFileFromKnownDirectory = LocateSubtitle(knownSubtitleDirectory, videoFileName);
|
if (!Directory.Exists(knownSubtitleDirectory))
|
||||||
if (!string.IsNullOrEmpty(subtitleFileFromKnownDirectory))
|
|
||||||
{
|
{
|
||||||
return subtitleFileFromKnownDirectory;
|
continue;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
|
|
||||||
string LocateSubtitle(string localPath, in string localVideoFileName)
|
|
||||||
{
|
|
||||||
if (!Directory.Exists(localPath))
|
|
||||||
{
|
|
||||||
return string.Empty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to locate subtitle file that has the same name as the video file
|
// try to locate subtitle file that has the same name as the video file
|
||||||
var defaultSubtitles = new[]
|
var defaultSubtitles = new[]
|
||||||
{
|
{
|
||||||
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".ass")),
|
Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".ass")),
|
||||||
Path.Combine(localPath, Path.ChangeExtension(localVideoFileName, ".srt"))
|
Path.Combine(knownSubtitleDirectory, Path.ChangeExtension(videoFileName, ".srt"))
|
||||||
};
|
};
|
||||||
foreach (var defaultSubtitle in defaultSubtitles)
|
foreach (var defaultSubtitle in defaultSubtitles)
|
||||||
{
|
{
|
||||||
@ -748,17 +744,17 @@ namespace Nikse.SubtitleEdit.Core.Common
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var assEnumerable = Directory.EnumerateFiles(localPath, "*.ass", SearchOption.TopDirectoryOnly);
|
// get first subtitle in path with extension .ass or .srt
|
||||||
var subripEnumerable = Directory.EnumerateFiles(localPath, "*.srt", SearchOption.TopDirectoryOnly);
|
var assEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.ass", SearchOption.TopDirectoryOnly);
|
||||||
var subtitleFile = assEnumerable.Concat(subripEnumerable).FirstOrDefault();
|
var subRipEnumerable = Directory.EnumerateFiles(knownSubtitleDirectory, "*.srt", SearchOption.TopDirectoryOnly);
|
||||||
// subtitle file found in top directory
|
var subtitleFile = assEnumerable.Concat(subRipEnumerable).FirstOrDefault();
|
||||||
if (!string.IsNullOrEmpty(subtitleFile))
|
if (!string.IsNullOrEmpty(subtitleFile))
|
||||||
{
|
{
|
||||||
return subtitleFile;
|
return subtitleFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
return string.Empty;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user