2025-01-13 18:51:26 +00:00
|
|
|
using AIStudio.Tools.Rust;
|
|
|
|
|
|
2025-02-15 14:41:12 +00:00
|
|
|
namespace AIStudio.Tools.Services;
|
2025-01-13 18:51:26 +00:00
|
|
|
|
|
|
|
|
public sealed partial class RustService
|
|
|
|
|
{
|
|
|
|
|
public async Task<DirectorySelectionResponse> SelectDirectory(string title, string? initialDirectory = null)
|
|
|
|
|
{
|
|
|
|
|
PreviousDirectory? previousDirectory = initialDirectory is null ? null : new (initialDirectory);
|
|
|
|
|
var result = await this.http.PostAsJsonAsync($"/select/directory?title={title}", previousDirectory, this.jsonRustSerializerOptions);
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
this.logger!.LogError($"Failed to select a directory: '{result.StatusCode}'");
|
|
|
|
|
return new DirectorySelectionResponse(true, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await result.Content.ReadFromJsonAsync<DirectorySelectionResponse>(this.jsonRustSerializerOptions);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 13:43:12 +00:00
|
|
|
public async Task<FileSelectionResponse> SelectFile(string title, FileTypeFilter? filter = null, string? initialFile = null)
|
2025-01-13 18:51:26 +00:00
|
|
|
{
|
2025-05-03 13:43:12 +00:00
|
|
|
var payload = new SelectFileOptions
|
|
|
|
|
{
|
|
|
|
|
Title = title,
|
|
|
|
|
PreviousFile = initialFile is null ? null : new (initialFile),
|
|
|
|
|
Filter = filter
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = await this.http.PostAsJsonAsync("/select/file", payload, this.jsonRustSerializerOptions);
|
2025-01-13 18:51:26 +00:00
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
this.logger!.LogError($"Failed to select a file: '{result.StatusCode}'");
|
|
|
|
|
return new FileSelectionResponse(true, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await result.Content.ReadFromJsonAsync<FileSelectionResponse>(this.jsonRustSerializerOptions);
|
|
|
|
|
}
|
2025-11-11 14:30:17 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initiates a dialog to let the user select a file for a writing operation.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="title">The title of the save file dialog.</param>
|
|
|
|
|
/// <param name="filter">An optional file type filter for filtering specific file formats.</param>
|
|
|
|
|
/// <param name="initialFile">An optional initial file path to pre-fill in the dialog.</param>
|
|
|
|
|
/// <returns>A <see cref="FileSaveResponse"/> object containing information about whether the user canceled the
|
|
|
|
|
/// operation and whether the select operation was successful.</returns>
|
|
|
|
|
public async Task<FileSaveResponse> SaveFile(string title, FileTypeFilter? filter = null, string? initialFile = null)
|
|
|
|
|
{
|
|
|
|
|
var payload = new SaveFileOptions
|
|
|
|
|
{
|
|
|
|
|
Title = title,
|
|
|
|
|
PreviousFile = initialFile is null ? null : new (initialFile),
|
|
|
|
|
Filter = filter
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = await this.http.PostAsJsonAsync("/save/file", payload, this.jsonRustSerializerOptions);
|
|
|
|
|
if (!result.IsSuccessStatusCode)
|
|
|
|
|
{
|
|
|
|
|
this.logger!.LogError($"Failed to select a file for writing operation '{result.StatusCode}'");
|
|
|
|
|
return new FileSaveResponse(true, string.Empty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await result.Content.ReadFromJsonAsync<FileSaveResponse>(this.jsonRustSerializerOptions);
|
|
|
|
|
}
|
2025-01-13 18:51:26 +00:00
|
|
|
}
|