Refactoring

This commit is contained in:
Koisu 2025-06-24 11:19:45 -07:00
parent 402d398989
commit b556dac2c1
5 changed files with 64 additions and 70 deletions

View file

@ -229,20 +229,6 @@ namespace Flow.Launcher.Plugin
MatchResult FuzzySearch(string query, string stringToCompare);
/// <summary>
/// Returns if the given name is valid file name even if it doesn't exist
/// </summary>
/// <param name="name">the name to check</param>
public bool IsValidFileName(string name);
/// <summary>
/// Returns if the given name is valid directory name even if it doesn't exist
/// </summary>
/// <param name="name">the name to check</param>
public bool IsValidDirectoryName(string name);
/// <summary>
/// Open A dialog to rename the given file or folder
/// </summary>
/// <param name="info">The directory or file info for the thing to rename. You must check if it actually exists or this will throw an exception</param>
/// <summary>

View file

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
@ -364,5 +365,38 @@ namespace Flow.Launcher.Plugin.SharedCommands
}
}
}
/// <summary>
/// Return true is the given name is a valid file name
/// </summary>
public static bool IsValidFileName(string name)
{
if (IsReservedName(name)) return false;
if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || string.IsNullOrWhiteSpace(name))
{
return false;
}
return true;
}
public static bool IsValidDirectoryName(string name)
{
if (IsReservedName(name)) return false;
char[] invalidChars = Path.GetInvalidPathChars().Append('/').ToArray().Append('\\').ToArray();
if (name.IndexOfAny(invalidChars) >= 0)
{
return false;
}
return true;
}
private static bool IsReservedName(string name)
{
string[] reservedNames = new[] { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
string nameWithoutExtension = Path.GetFileNameWithoutExtension(name).ToUpperInvariant();
if (reservedNames.Contains(nameWithoutExtension))
{
return true;
}
return false;
}
}
}

View file

@ -222,39 +222,7 @@ namespace Flow.Launcher
}
}
}
public bool IsValidFileName(string name)
{
if (IsReservedName(name)) return false;
if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 || string.IsNullOrWhiteSpace(name))
{
return false;
}
return true;
}
public bool IsValidDirectoryName(string name)
{
List<char> invalidChars = Path.GetInvalidPathChars().ToList();
invalidChars.Add('/');
invalidChars.Add('\\');
if (name.IndexOfAny(invalidChars.ToArray()) >= 0 || string.IsNullOrWhiteSpace(name))
{
return false;
}
if (IsReservedName(name)) return false;
return true;
}
private bool IsReservedName(string name)
{
string[] reservedNames = new[] { "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" };
string nameWithoutExtension = Path.GetFileNameWithoutExtension(name).ToUpperInvariant();
if (reservedNames.Contains(nameWithoutExtension))
{
return true;
}
return false;
}
private static async Task<Exception> RetryActionOnSTAThreadAsync(Action action, int retryCount = 6, int retryDelay = 150)
{
for (var i = 0; i < retryCount; i++)

View file

@ -9,7 +9,7 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
{
if (info is FileInfo)
{
if (!api.IsValidFileName(newName))
if (!SharedCommands.FilesFolders.IsValidFileName(newName))
{
throw new InvalidNameException();
}
@ -17,28 +17,32 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
DirectoryInfo directory;
directory = file.Directory ?? new DirectoryInfo(Path.GetPathRoot(file.FullName));
if (info.FullName == Path.Join(directory.FullName, newName))
string newPath = Path.Join(directory.FullName, newName);
if (info.FullName == newPath)
{
throw new NotANewNameException("New name was the same as the old name");
}
File.Move(info.FullName, Path.Join(directory.FullName, newName));
if (File.Exists(newPath)) throw new ElementAlreadyExistsException();
File.Move(info.FullName, newPath);
return;
}
else if (info is DirectoryInfo)
{
if (!api.IsValidDirectoryName(newName))
if (!SharedCommands.FilesFolders.IsValidDirectoryName(newName))
{
throw new InvalidNameException();
}
DirectoryInfo directory = (DirectoryInfo)info;
DirectoryInfo parent;
parent = directory.Parent ?? new DirectoryInfo(Path.GetPathRoot(directory.FullName));
if (info.FullName == Path.Join(parent.FullName, newName))
string newPath = Path.Join(parent.FullName, newName);
if (info.FullName == newPath)
{
throw new NotANewNameException("New name was the same as the old name");
}
Directory.Move(info.FullName, Path.Join(parent.FullName, newName));
if (Directory.Exists(newPath)) throw new ElementAlreadyExistsException();
Directory.Move(info.FullName, newPath);
}
else
@ -48,7 +52,7 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
}
/// <summary>
/// Renames a file system elemnt (directory or file)
/// Renames a file system element (directory or file)
/// </summary>
/// <param name="NewFileName">The requested new name</param>
/// <param name="oldInfo"> The <see cref="FileInfo"/> or <see cref="DirectoryInfo"/> representing the old file</param>
@ -72,16 +76,17 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
switch (exception)
{
case FileNotFoundException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_file_not_found"), oldInfo.FullName));
return;
case NotANewNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_not_a_new_name"), NewFileName));
api.ShowMainWindow();
return;
case InvalidNameException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_invalid_name"), NewFileName));
return;
case ElementAlreadyExistsException:
api.ShowMsgError(string.Format(api.GetTranslation("plugin_explorer_element_already_exists"), NewFileName));
break;
case IOException iOException:
if (iOException.Message.Contains("incorrect"))
{
@ -111,24 +116,23 @@ namespace Flow.Launcher.Plugin.Explorer.Helper
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
internal class FileAlreadyExistsException : IOException
{
public FileAlreadyExistsException() { }
public FileAlreadyExistsException(string message) : base(message) { }
public FileAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
protected FileAlreadyExistsException(
internal class ElementAlreadyExistsException : IOException {
public ElementAlreadyExistsException() { }
public ElementAlreadyExistsException(string message) : base(message) { }
public ElementAlreadyExistsException(string message, Exception inner) : base(message, inner) { }
protected ElementAlreadyExistsException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}
internal class InvalidNameException : Exception
{
public InvalidNameException() { }
public InvalidNameException(string message) : base(message) { }
public InvalidNameException(string message, Exception inner) : base(message, inner) { }
protected InvalidNameException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
public InvalidNameException() { }
public InvalidNameException(string message) : base(message) { }
public InvalidNameException(string message, Exception inner) : base(message, inner) { }
protected InvalidNameException(
System.Runtime.Serialization.SerializationInfo info,
System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
}

View file

@ -197,8 +197,10 @@
<system:String x:Key="plugin_explorer_not_a_new_name">The given name: {0} was not new.</system:String>
<system:String x:Key="plugin_explorer_field_may_not_be_empty">{0} may not be empty.</system:String>
<system:String x:Key="plugin_explorer_invalid_name">{0} is an invalid name.</system:String>
<system:String x:Key="plugin_explorer_file_not_found">The specified file: {0} was not found</system:String>
<system:String x:Key="plugin_explorer_file_not_found">The specified item: {0} was not found</system:String>
<system:String x:Key="plugin_explorer_rename_subtitle">Open a dialog to rename this.</system:String>
<system:String x:Key="plugin_explorer_cannot_rename">This cannot be renamed.</system:String>
<system:String x:Key="plugin_explorer_successful_rename">Successfully renamed it to: {0}</system:String>
<system:String x:Key="plugin_explorer_element_already_exists">There is already a file with the name: {0} in this location</system:String>
<system:String x:Key="plugin_explorer_failed_to_open_rename_dialog">Failed to open rename dialog.</system:String>
</ResourceDictionary>