Files
LeanderShiftPlannerV2/LeanderShiftPlannerV2/FileIO/GoogleApiLoader.cs
T

92 lines
3.0 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using LeanderShiftPlannerV2.Util;
namespace LeanderShiftPlannerV2.FileIO;
public static class GoogleApiLoader
{
public static bool CheckFileSystem_ReadFile()
{
if (!Directory.Exists(Constants.DataPath))
Directory.CreateDirectory(Constants.DataPath);
if (!File.Exists(Constants.DataPath + Constants.GoogleApiFilePath))
{
File.Create(Constants.DataPath + Constants.GoogleApiFilePath).Close();
File.WriteAllLines(Constants.DataPath + Constants.GoogleApiFilePath,
File.ReadAllLines(Constants.GoogleApiFileResource));
return false;
}
return true;
}
public static bool CheckFileSystem_ReadToken()
{
if (!Directory.Exists(Constants.DataPath))
Directory.CreateDirectory(Constants.DataPath);
if (!File.Exists(Constants.DataPath + Constants.GoogleApiTokenPath))
{
File.Create(Constants.DataPath + Constants.GoogleApiTokenPath);
return false;
}
return true;
}
public static Dictionary<string, string> ReadFile()
{
if (!CheckFileSystem_ReadFile()) return new Dictionary<string, string>();
Dictionary<string, string> apiSecrets = new Dictionary<string, string>();
try
{
string[] lines = File.ReadAllLines(Constants.DataPath + Constants.GoogleApiFilePath);
foreach (string line in lines)
{
string[] parts = line.Split(new[] { '=' }, 2);
if (parts.Length == 2)
{
string pattern = parts[0].Trim();
string value = parts[1].Trim();
if (!string.IsNullOrEmpty(pattern))
{
apiSecrets[pattern] = value;
}
}
}
}
catch (Exception ex)
{
//TODO
Console.WriteLine($"Fehler beim Lesen der Datei: {ex.Message}");
}
return apiSecrets;
}
public static void WriteToken(string token)
{
if (File.Exists(Constants.DataPath + Constants.GoogleApiTokenPath))
File.Delete(Constants.DataPath + Constants.GoogleApiTokenPath);
File.Create(Constants.DataPath + Constants.GoogleApiTokenPath).Close();
File.WriteAllText(Constants.DataPath + Constants.GoogleApiTokenPath, token);
}
public static string? ReadToken()
{
if (!CheckFileSystem_ReadToken()) return null;
string tokenString = null;
try
{
tokenString = File.ReadAllText(Constants.DataPath + Constants.GoogleApiTokenPath);
}
catch (Exception ex)
{
//TODO
Console.WriteLine($"Fehler beim Lesen der Datei: {ex.Message}");
}
return (tokenString.Equals(String.Empty) ? null : tokenString);
}
}