81 lines
1.9 KiB
C#
81 lines
1.9 KiB
C#
namespace DevicesRestApi.Helpers;
|
|
|
|
public class DeviceConfigurationFile
|
|
{
|
|
private readonly string _rootDirectory;
|
|
|
|
private readonly string _deviceName;
|
|
|
|
private const string SettingsFilename = "settings.conf";
|
|
|
|
private readonly AppSettings _appSettings;
|
|
|
|
public DeviceConfigurationFile(
|
|
string deviceName,
|
|
AppSettings appSettings
|
|
) {
|
|
_deviceName = deviceName;
|
|
if (string.IsNullOrEmpty(_deviceName))
|
|
{
|
|
throw new ArgumentException("The provided device name is null or empty.");
|
|
}
|
|
|
|
_appSettings = appSettings;
|
|
var rootPath = _appSettings.RootPath;
|
|
if (string.IsNullOrEmpty(rootPath))
|
|
{
|
|
throw new ArgumentException("Root path is not configured.");
|
|
}
|
|
|
|
_rootDirectory = Path.Combine(rootPath, "devices");
|
|
if (!Directory.Exists(_rootDirectory))
|
|
{
|
|
throw new ArgumentException($"Devices directory doesn't exist: {_rootDirectory}.");
|
|
}
|
|
}
|
|
|
|
public string GetDirectoryPath()
|
|
{
|
|
return Path.Combine(_rootDirectory, _deviceName);
|
|
}
|
|
|
|
public string GetFilePath()
|
|
{
|
|
return Path.Combine(GetDirectoryPath(), SettingsFilename);
|
|
}
|
|
|
|
public bool FileExists()
|
|
{
|
|
return File.Exists(GetFilePath());
|
|
}
|
|
|
|
public bool DirectoryExists()
|
|
{
|
|
return Directory.Exists(GetDirectoryPath());
|
|
}
|
|
|
|
private void CreateDirectory()
|
|
{
|
|
Directory.CreateDirectory(GetDirectoryPath());
|
|
}
|
|
|
|
private void CreateFile()
|
|
{
|
|
using (FileStream fs = File.Create(GetFilePath()))
|
|
{
|
|
// The file is created and opened, we don't need to write anything to it
|
|
}
|
|
}
|
|
|
|
public void Create()
|
|
{
|
|
if (!DirectoryExists())
|
|
{
|
|
CreateDirectory();
|
|
}
|
|
if (!FileExists())
|
|
{
|
|
CreateFile();
|
|
}
|
|
}
|
|
} |