Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor directory creation logic #8832

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/libse/Common/FileUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -935,5 +935,27 @@ public static string TryLocateSubtitleFile(string path, string videoFileName)

return string.Empty;
}

/// <summary>
/// Tries to create a directory at the specified path if it does not already exist.
/// </summary>
/// <param name="path">The path where the directory is to be created.</param>
/// <returns>True if the directory was successfully created or already exists; otherwise, false.</returns>
public static bool TryCreateDirectory(string path)
{
if (!Directory.Exists(path))
{
try
{
return Directory.CreateDirectory(path).Exists;
}
catch
{
return false;
}
}

return true;
}
}
}
8 changes: 3 additions & 5 deletions src/libse/Common/ZipExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,11 +216,9 @@ public bool ExtractFile(ZipFileEntry zfe, string filename)
{
// Make sure the parent directory exist
string path = Path.GetDirectoryName(filename);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}


FileUtil.TryCreateDirectory(path);

// Check it is directory. If so, do nothing
if (Directory.Exists(filename))
{
Expand Down
15 changes: 1 addition & 14 deletions src/ui/Forms/GenerateTransparentVideoWithSubtitles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1339,20 +1339,7 @@ private void ShowLabelOutput()

private void linkLabelSourceFolder_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
var path = linkLabelSourceFolder.Text;

if (!Directory.Exists(path))
{
try
{
Directory.CreateDirectory(path);
}
catch
{
return;
}
}

FileUtil.TryCreateDirectory(linkLabelSourceFolder.Text);
UiUtil.OpenFolder(linkLabelSourceFolder.Text);
}

Expand Down
11 changes: 2 additions & 9 deletions src/ui/Forms/RestoreAutoBackup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,9 @@ public static bool SaveAutoBackupSettings(Settings settings)
}

var path = Path.Combine(Configuration.AutoBackupDirectory, "Settings");
if (!Directory.Exists(path))
if (!FileUtil.TryCreateDirectory(path))
{
try
{
Directory.CreateDirectory(path);
}
catch
{
return false;
}
return false;
}

var fileName = $"{DateTime.Now.Year:0000}-{DateTime.Now.Month:00}-{DateTime.Now.Day:00}_{DateTime.Now.Hour:00}-{DateTime.Now.Minute:00}-{DateTime.Now.Second:00}Settings.xml";
Expand Down
7 changes: 2 additions & 5 deletions src/ui/Logic/CommandLineConvert/CommandLineConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2092,11 +2092,8 @@ internal static bool BatchConvertSave(string targetFormat, TimeSpan offset, Text
if (binaryParagraphs.Count > 0)
{
var path = Path.Combine(outputFolder, Path.GetFileNameWithoutExtension(fileName));
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

FileUtil.TryCreateDirectory(path);

targetFormatFound = true;
for (var i = 0; i < binaryParagraphs.Count; i++)
{
Expand Down