Refactor directory writable check to FileUtil

Moved the method `IsDirectoryWritable` from `WhisperAudioToText` to `FileUtil` for better code reuse and maintainability. Adjusted references in `WhisperAudioToText` and `VoskAudioToText` accordingly.

Signed-off-by: Ivandro Jao <Ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-10-10 19:17:40 +01:00
parent fdb4d34de6
commit a46a33bf2f
3 changed files with 21 additions and 16 deletions

View File

@ -935,5 +935,23 @@ namespace Nikse.SubtitleEdit.Core.Common
return string.Empty;
}
/// <summary>
/// Checks if a directory is writable by attempting to create and delete a temporary file within it.
/// </summary>
/// <param name="dirPath">The directory path to check for write access.</param>
/// <returns>True if the directory is writable, false otherwise.</returns>
public static bool IsDirectoryWritable(string dirPath)
{
try
{
using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose)) { }
return true;
}
catch
{
return false;
}
}
}
}

View File

@ -267,7 +267,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
catch
{
var dir = Path.GetDirectoryName(fileName);
if (!WhisperAudioToText.IsDirectoryWritable(dir))
if (!FileUtil.IsDirectoryWritable(dir))
{
MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error);
}

View File

@ -843,7 +843,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
catch
{
var dir = Path.GetDirectoryName(fileName);
if (!IsDirectoryWritable(dir))
if (!FileUtil.IsDirectoryWritable(dir))
{
MessageBox.Show($"SE does not have write access to the folder '{dir}'", MessageBoxIcon.Error);
}
@ -852,20 +852,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText
}
}
public static bool IsDirectoryWritable(string dirPath)
{
try
{
using (FileStream fs = File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose))
{
}
return true;
}
catch
{
return false;
}
}
internal static string GetLanguage(string name)
{