mirror of
https://github.com/ultrasn0w/app.git
synced 2025-12-16 03:19:52 +01:00
Register
This commit is contained in:
77
APP/Utility/SaveBoy.cs
Normal file
77
APP/Utility/SaveBoy.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using System.Text.Json;
|
||||
using APP.DTO;
|
||||
|
||||
namespace APP.Utility;
|
||||
|
||||
internal static class SaveBoy
|
||||
{
|
||||
private static readonly string AuthFile = ".." + Path.DirectorySeparatorChar + "reg.json";
|
||||
|
||||
internal static async Task<Auth?> ReadAuth()
|
||||
{
|
||||
if (File.Exists(AuthFile))
|
||||
{
|
||||
return await ReadFromFile<Auth>(AuthFile);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
internal static void DeleteAuth()
|
||||
{
|
||||
if (File.Exists(AuthFile))
|
||||
{
|
||||
File.Delete(AuthFile);
|
||||
}
|
||||
}
|
||||
|
||||
internal static async Task SaveAuth(Auth auth)
|
||||
{
|
||||
await WriteToFile(AuthFile, auth);
|
||||
}
|
||||
|
||||
private static async Task WriteToFile<T>(string filePath, T objectToWrite)
|
||||
{
|
||||
try
|
||||
{
|
||||
await using var sw = new StreamWriter(filePath, false);
|
||||
try
|
||||
{
|
||||
var res = JsonSerializer.Serialize(objectToWrite);
|
||||
await sw.WriteAsync(res);
|
||||
}
|
||||
finally
|
||||
{
|
||||
sw.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<T?> ReadFromFile<T>(string filePath)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var sr = new StreamReader(filePath);
|
||||
try
|
||||
{
|
||||
var text = await sr.ReadToEndAsync();
|
||||
var res = JsonSerializer.Deserialize<T>(text);
|
||||
return res;
|
||||
}
|
||||
finally
|
||||
{
|
||||
sr.Close();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await Console.Error.WriteLineAsync(ex.Message);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user