113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using System.IO;
|
|
|
|
namespace UtopiaCanteenSystem.Data;
|
|
|
|
/// <summary>
|
|
/// Central location for persistent paths (database and config). Uses LocalApplicationData
|
|
/// so data survives app updates when the application is deployed from a file server.
|
|
/// </summary>
|
|
public static class DatabasePath
|
|
{
|
|
private static string? _appDataFolder;
|
|
private static string? _dbPath;
|
|
private static string _productFolderName = "UtopiaCanteenSystem";
|
|
|
|
/// <summary>
|
|
/// Use "UtopiaCanteenBackend" for the Windows Service; default is legacy WPF folder name.
|
|
/// </summary>
|
|
public static void UseBackendServiceStorage()
|
|
{
|
|
_productFolderName = "UtopiaCanteenBackend";
|
|
_appDataFolder = null;
|
|
_dbPath = null;
|
|
}
|
|
|
|
public static void UseClientStorage()
|
|
{
|
|
_productFolderName = "UtopiaCanteenClient";
|
|
_appDataFolder = null;
|
|
_dbPath = null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Folder under LocalApplicationData for DB and config. Created on first use.
|
|
/// </summary>
|
|
public static string GetAppDataFolder()
|
|
{
|
|
if (_appDataFolder != null)
|
|
return _appDataFolder;
|
|
|
|
_appDataFolder = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
_productFolderName);
|
|
Directory.CreateDirectory(_appDataFolder);
|
|
return _appDataFolder;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Full path to the SQLite database file. If a DB exists in the legacy app-directory
|
|
/// location, it is copied to the new location once.
|
|
/// </summary>
|
|
public static string GetDbPath()
|
|
{
|
|
if (_dbPath != null)
|
|
return _dbPath;
|
|
|
|
var folder = GetAppDataFolder();
|
|
_dbPath = Path.Combine(folder, "utopia_canteen.db");
|
|
MigrateLegacyDbIfNeeded(_dbPath);
|
|
return _dbPath;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Full path to appsettings.json (remember-me credentials and other config).
|
|
/// Stored in the same persistent folder so it survives updates. If a config exists
|
|
/// in the legacy app-directory location, it is copied to the new location once.
|
|
/// </summary>
|
|
public static string GetConfigPath()
|
|
{
|
|
var folder = GetAppDataFolder();
|
|
var newPath = Path.Combine(folder, "appsettings.json");
|
|
MigrateLegacyConfigIfNeeded(newPath);
|
|
return newPath;
|
|
}
|
|
|
|
private static void MigrateLegacyDbIfNeeded(string newPath)
|
|
{
|
|
if (File.Exists(newPath))
|
|
return;
|
|
|
|
var legacyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "utopia_canteen.db");
|
|
if (!File.Exists(legacyPath))
|
|
return;
|
|
|
|
try
|
|
{
|
|
File.Copy(legacyPath, newPath);
|
|
}
|
|
catch
|
|
{
|
|
// If copy fails, app will start with a new DB at new path.
|
|
}
|
|
}
|
|
|
|
private static void MigrateLegacyConfigIfNeeded(string newPath)
|
|
{
|
|
if (File.Exists(newPath))
|
|
return;
|
|
|
|
var legacyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json");
|
|
if (!File.Exists(legacyPath))
|
|
return;
|
|
|
|
try
|
|
{
|
|
File.Copy(legacyPath, newPath);
|
|
}
|
|
catch
|
|
{
|
|
// If copy fails, app will start with a new config at new path.
|
|
}
|
|
}
|
|
}
|