From a46a33bf2fe57b49482ddf66b42adcc7567e69e2 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Thu, 10 Oct 2024 19:17:40 +0100 Subject: [PATCH] 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 --- src/libse/Common/FileUtil.cs | 18 ++++++++++++++++++ src/ui/Forms/AudioToText/VoskAudioToText.cs | 2 +- src/ui/Forms/AudioToText/WhisperAudioToText.cs | 17 ++--------------- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/libse/Common/FileUtil.cs b/src/libse/Common/FileUtil.cs index 81296a813..2ac9056e1 100644 --- a/src/libse/Common/FileUtil.cs +++ b/src/libse/Common/FileUtil.cs @@ -935,5 +935,23 @@ namespace Nikse.SubtitleEdit.Core.Common return string.Empty; } + + /// + /// Checks if a directory is writable by attempting to create and delete a temporary file within it. + /// + /// The directory path to check for write access. + /// True if the directory is writable, false otherwise. + public static bool IsDirectoryWritable(string dirPath) + { + try + { + using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose)) { } + return true; + } + catch + { + return false; + } + } } } diff --git a/src/ui/Forms/AudioToText/VoskAudioToText.cs b/src/ui/Forms/AudioToText/VoskAudioToText.cs index 57bd6dc0c..f4f6c8cee 100644 --- a/src/ui/Forms/AudioToText/VoskAudioToText.cs +++ b/src/ui/Forms/AudioToText/VoskAudioToText.cs @@ -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); } diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.cs index b82426c02..38eea6877 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.cs @@ -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) {