Skip to content

A Swiss Army knife for developers.

Completed missing parts of IFileStroage in DevToys CLI (#1048)

Etienne BAUDOUXeceb7541bf33
diff --git a/src/app/dev/DevToys.Core/FileHelper.cs b/src/app/dev/DevToys.Core/FileHelper.cs
index 6fc137566..b68770b65 100644
--- a/src/app/dev/DevToys.Core/FileHelper.cs
+++ b/src/app/dev/DevToys.Core/FileHelper.cs
@@ -1,9 +1,12 @@
using System.Collections.Concurrent;
+using Microsoft.Extensions.Logging;
namespace DevToys.Core;
public static class FileHelper
{
+ private static readonly ILogger logger = typeof(FileHelper).Log();
+
private static readonly ConcurrentBag<FileInfo> tempFiles = new();
public static FileInfo CreateTempFile(string baseFolder, string? desiredFileExtension)
@@ -28,6 +31,8 @@ public static class FileHelper
public static void ClearTempFiles(string baseFolder)
{
+ DateTime startTime = DateTime.UtcNow;
+
FileInfo[] files = tempFiles.ToArray();
foreach (FileInfo tempFile in files)
{
@@ -65,5 +70,8 @@ public static class FileHelper
}
}
}
+
+ double elapsedMilliseconds = (DateTime.UtcNow - startTime).TotalMilliseconds;
+ logger.LogInformation("Cleared temp files in {elapsedMilliseconds}ms", elapsedMilliseconds);
}
}
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
index 969fac9e4..926d058a2 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Core/FileStorage/FileStorage.cs
@@ -63,38 +63,187 @@ internal sealed class FileStorage : IFileStorage
public ValueTask<FileStream?> PickSaveFileAsync(params string[] fileTypes)
{
- // TODO: Limit input to the indicated file types.
- Console.WriteLine(CliStrings.PromptSaveFile);
- string? filePath = Console.ReadLine();
-
- if (string.IsNullOrWhiteSpace(filePath))
+ do
{
- return new ValueTask<FileStream?>(Task.FromResult<FileStream?>(null));
- }
+ Console.WriteLine(CliStrings.PromptSaveFile);
+ string? filePath = Console.ReadLine();
+
+ if (filePath is null || string.IsNullOrWhiteSpace(filePath))
+ {
+ return new ValueTask<FileStream?>(Task.FromResult<FileStream?>(null));
+ }
+
+ // Remove quotes in case the user copy-pasted the file path from the file explorer.
+ filePath = TrimFilePath(filePath);
+
+ if (IsFileOfType(filePath, fileTypes))
+ {
+ return new ValueTask<FileStream?>(File.OpenWrite(filePath));
+ }
- return new ValueTask<FileStream?>(File.OpenWrite(filePath!));
+ Console.Error.WriteLine(CliStrings.InvalidFileType, string.Join(", ", fileTypes));
+ } while (true);
}
public ValueTask<SandboxedFileReader?> PickOpenFileAsync(params string[] fileTypes)
{
- // TODO: prompt the user to type in the console a relative or absolute file path that has one of the file types indicated.
- throw new NotImplementedException();
+ do
+ {
+ Console.WriteLine(CliStrings.PromptOpenFile);
+ string? filePath = Console.ReadLine();
+
+ if (filePath is null || string.IsNullOrWhiteSpace(filePath))
+ {
+ return new ValueTask<SandboxedFileReader?>(Task.FromResult<SandboxedFileReader?>(null));
+ }
+
+ // Remove quotes in case the user copy-pasted the file path from the file explorer.
+ filePath = TrimFilePath(filePath);
+
+ var fileInfo = new FileInfo(filePath);
+ if (fileInfo.Exists)
+ {
+ if (IsFileOfType(filePath, fileTypes))
+ {
+ return new ValueTask<SandboxedFileReader?>(SandboxedFileReader.FromFileInfo(fileInfo));
+ }
+ else
+ {
+ Console.Error.WriteLine(CliStrings.InvalidFileType, string.Join(", ", fileTypes));
+ }
+ }
+ else
+ {
+ Console.Error.WriteLine(CliStrings.FileNotFound, fileInfo.FullName);
+ }
+ } while (true);
}
public ValueTask<SandboxedFileReader[]> PickOpenFilesAsync(params string[] fileTypes)
{
- // TODO: prompt the user to type in the console a relative or absolute file path that has one of the file types indicated.
- throw new NotImplementedException();
+ do
+ {
+ Console.WriteLine(CliStrings.PromptOpenFiles);
+ string? filePaths = Console.ReadLine();
+
+ if (filePaths is null || string.IsNullOrWhiteSpace(filePaths))
+ {
+ return new ValueTask<SandboxedFileReader[]>(Task.FromResult(Array.Empty<SandboxedFileReader>()));
+ }
+
+ string[] paths = filePaths.Split(',', StringSplitOptions.RemoveEmptyEntries);
+ var filesInfo = new FileInfo[paths.Length];
+ bool succeeded = true;
+
+ for (int i = 0; i < paths.Length; i++)
+ {
+ string filePath = paths[i];
+
+ // Remove quotes in case the user copy-pasted the file path from the file explorer.
+ filePath = TrimFilePath(filePath);
+
+ var fileInfo = new FileInfo(filePath);
+ if (fileInfo.Exists)
+ {
+ if (IsFileOfType(filePath, fileTypes))
+ {
+ filesInfo[i] = fileInfo;
+ }
+ else
+ {
+ succeeded = false;
+ Console.Error.WriteLine(CliStrings.InvalidFileType, string.Join(", ", fileTypes));
+ break;
+ }
+ }
+ else
+ {
+ succeeded = false;
+ Console.Error.WriteLine(CliStrings.FileNotFound, fileInfo.FullName);
+ break;
+ }
+ }
+
+ if (succeeded)
+ {
+ var readers = new SandboxedFileReader[filesInfo.Length];
+ for (int i = 0; i < filesInfo.Length; i++)
+ {
+ readers[i] = SandboxedFileReader.FromFileInfo(filesInfo[i]);
+ }
+
+ return new ValueTask<SandboxedFileReader[]>(Task.FromResult(readers));
+ }
+ } while (true);
}
public ValueTask<string?> PickFolderAsync()
{
- // TODO: prompt the user to type in the console a relative or absolute file path that has one of the file types indicated.
- throw new NotImplementedException();
+ Console.WriteLine(CliStrings.PromptOpenFolder);
+ string? folderPath = Console.ReadLine();
+
+ if (folderPath is null || string.IsNullOrWhiteSpace(folderPath))
+ {
+ return new ValueTask<string?>(Task.FromResult<string?>(null));
+ }
+
+ // Remove quotes in case the user copy-pasted the file path from the file explorer.
+ folderPath = TrimFilePath(folderPath);
+
+ DirectoryInfo directoryInfo = Directory.CreateDirectory(folderPath);
+
+ return new ValueTask<string?>(Task.FromResult<string?>(directoryInfo.FullName));
}
public FileInfo CreateSelfDestroyingTempFile(string? desiredFileExtension = null)
{
return FileHelper.CreateTempFile(Constants.AppTempFolder, desiredFileExtension);
}
+
+ private static bool IsFileOfType(string filePath, string[] fileTypes)
+ {
+ if (AreAnyFileTypesValid(fileTypes))
+ {
+ return true;
+ }
+
+ string fileExtension = Path.GetExtension(filePath);
+ for (int i = 0; i < fileTypes.Length; i++)
+ {
+ string fileType = fileTypes[i];
+ string editedFileType = "." + fileType.Trim('*').Trim('.').ToLower();
+ if (string.Equals(fileExtension, editedFileType, StringComparison.CurrentCultureIgnoreCase))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private static bool AreAnyFileTypesValid(string[] fileTypes)
+ {
+ if (fileTypes is null || fileTypes.Length == 0)
+ {
+ return true;
+ }
+
+ return Array.Exists(fileTypes, fileType => string.Equals(fileType, "*.*", StringComparison.CurrentCultureIgnoreCase));
+ }
+
+ private static string TrimFilePath(string filePath)
+ {
+ // Remove quotes in case the user copy-pasted the file path from the file explorer.
+ // We do these trim in a loop in case the user input is something like ` "" C:\file.txt "" `.
+
+ int fileLength;
+
+ do
+ {
+ fileLength = filePath.Length;
+ filePath = filePath.Trim(' ').Trim('\"').Trim('\'');
+ } while (filePath.Length != fileLength);
+
+ return filePath;
+ }
}
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.Designer.cs b/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.Designer.cs
index 9436c8410..e8479ea42 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.Designer.cs
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.Designer.cs
@@ -61,7 +61,52 @@ namespace DevToys.CLI.Strings.CliStrings {
}
/// <summary>
- /// Looks up a localized string similar to Please type a relative or absolute file path to write in:.
+ /// Looks up a localized string similar to The file &apos;{0}&apos; doesn&apos;t exist..
+ /// </summary>
+ internal static string FileNotFound {
+ get {
+ return ResourceManager.GetString("FileNotFound", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Invalid file path. The file should have one of the following extensions: {0}.
+ /// </summary>
+ internal static string InvalidFileType {
+ get {
+ return ResourceManager.GetString("InvalidFileType", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Please type a relative or absolute file path to read (or press Enter to cancel):.
+ /// </summary>
+ internal static string PromptOpenFile {
+ get {
+ return ResourceManager.GetString("PromptOpenFile", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Please type one or many relative or absolute file paths to read, separated by a comma (or press Enter to cancel):.
+ /// </summary>
+ internal static string PromptOpenFiles {
+ get {
+ return ResourceManager.GetString("PromptOpenFiles", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Please type a relative or absolute folder path (or press Enter to cancel):.
+ /// </summary>
+ internal static string PromptOpenFolder {
+ get {
+ return ResourceManager.GetString("PromptOpenFolder", resourceCulture);
+ }
+ }
+
+ /// <summary>
+ /// Looks up a localized string similar to Please type a relative or absolute file path to write in (or press Enter to cancel):.
/// </summary>
internal static string PromptSaveFile {
get {
diff --git a/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.resx b/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.resx
index bf2e0ad9e..d45ed80fa 100644
--- a/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.resx
+++ b/src/app/dev/platforms/desktop/DevToys.CLI/Strings/CliStrings/CliStrings.resx
@@ -117,7 +117,22 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
+ <data name="FileNotFound" xml:space="preserve">
+ <value>The file '{0}' doesn't exist.</value>
+ </data>
+ <data name="InvalidFileType" xml:space="preserve">
+ <value>Invalid file path. The file should have one of the following extensions: {0}</value>
+ </data>
+ <data name="PromptOpenFile" xml:space="preserve">
+ <value>Please type a relative or absolute file path to read (or press Enter to cancel):</value>
+ </data>
+ <data name="PromptOpenFiles" xml:space="preserve">
+ <value>Please type one or many relative or absolute file paths to read, separated by a comma (or press Enter to cancel):</value>
+ </data>
+ <data name="PromptOpenFolder" xml:space="preserve">
+ <value>Please type a relative or absolute folder path (or press Enter to cancel):</value>
+ </data>
<data name="PromptSaveFile" xml:space="preserve">
- <value>Please type a relative or absolute file path to write in:</value>
+ <value>Please type a relative or absolute file path to write in (or press Enter to cancel):</value>
</data>
</root>
\ No newline at end of file