Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTOR] 127 Replace Magic Strings in DatabaseService #148

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 40 additions & 33 deletions src/Nexus/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,23 @@ internal class DatabaseService(IOptions<PathsOptions> pathsOptions)

private readonly PathsOptions _pathsOptions = pathsOptions.Value;

private readonly string _users = "users";
Apollo3zehn marked this conversation as resolved.
Show resolved Hide resolved

private readonly string _catalogs = "catalogs";

private readonly string _fileExtension = ".json";

private readonly string _project = "project";

private readonly string _tokens = "tokens";

private readonly string _pipelines = "pipelines";

/* /config/catalogs/catalog_id.json */
public bool TryReadCatalogMetadata(string catalogId, [NotNullWhen(true)] out string? catalogMetadata)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var catalogMetadataFileName = $"{physicalId}.json";
var filePath = SafePathCombine(_pathsOptions.Config, Path.Combine("catalogs", catalogMetadataFileName));
var catalogMetadataFileName = $"{GetPhysicalCatalogId(catalogId)}" + _fileExtension;
var filePath = SafePathCombine(_pathsOptions.Config, Path.Combine(_catalogs, catalogMetadataFileName));

catalogMetadata = default;

Expand All @@ -92,9 +103,8 @@ public bool TryReadCatalogMetadata(string catalogId, [NotNullWhen(true)] out str

public Stream WriteCatalogMetadata(string catalogId)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var catalogMetadataFileName = $"{physicalId}.json";
var folderPath = Path.Combine(_pathsOptions.Config, "catalogs");
var catalogMetadataFileName = $"{GetPhysicalCatalogId(catalogId)}" + _fileExtension;
var folderPath = Path.Combine(_pathsOptions.Config, _catalogs);

Directory.CreateDirectory(folderPath);

Expand All @@ -106,7 +116,7 @@ public Stream WriteCatalogMetadata(string catalogId)
/* /config/project.json */
public bool TryReadProject([NotNullWhen(true)] out string? project)
{
var filePath = Path.Combine(_pathsOptions.Config, "project.json");
var filePath = Path.Combine(_pathsOptions.Config, _project + _fileExtension);
project = default;

if (File.Exists(filePath))
Expand All @@ -122,14 +132,14 @@ public Stream WriteProject()
{
Directory.CreateDirectory(_pathsOptions.Config);

var filePath = Path.Combine(_pathsOptions.Config, "project.json");
var filePath = Path.Combine(_pathsOptions.Config, _project + _fileExtension);
return File.Open(filePath, FileMode.Create, FileAccess.Write);
}

/* /config/users */
public IEnumerable<string> EnumerateUsers()
{
var usersPath = Path.Combine(_pathsOptions.Config, "users");
var usersPath = Path.Combine(_pathsOptions.Config, _users);

if (Directory.Exists(usersPath))
return Directory.EnumerateDirectories(usersPath);
Expand All @@ -138,12 +148,11 @@ public IEnumerable<string> EnumerateUsers()
return Enumerable.Empty<string>();
}

public bool TryReadTokenMap(
string userId,
public bool TryReadTokenMap(string userId,
[NotNullWhen(true)] out string? tokenMap)
{
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, "users"), userId);
var tokenFilePath = Path.Combine(folderPath, "tokens.json");
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, _users), userId);
var tokenFilePath = Path.Combine(folderPath, _tokens + _fileExtension);

tokenMap = default;

Expand All @@ -159,8 +168,8 @@ public bool TryReadTokenMap(
public Stream WriteTokenMap(
string userId)
{
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, "users"), userId);
var tokensFilePath = Path.Combine(folderPath, "tokens.json");
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, _users), userId);
var tokensFilePath = Path.Combine(folderPath, _tokens + _fileExtension);

Directory.CreateDirectory(folderPath);

Expand All @@ -171,8 +180,8 @@ public bool TryReadPipelineMap(
string userId,
[NotNullWhen(true)] out string? pipelineMap)
{
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, "users"), userId);
var pipelinesFilePath = Path.Combine(folderPath, "pipelines.json");
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, _users), userId);
var pipelinesFilePath = Path.Combine(folderPath, _pipelines + _fileExtension);

pipelineMap = default;

Expand All @@ -188,8 +197,8 @@ public bool TryReadPipelineMap(
public Stream WritePipelineMap(
string userId)
{
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, "users"), userId);
var pipelineFilePath = Path.Combine(folderPath, "pipelines.json");
var folderPath = SafePathCombine(Path.Combine(_pathsOptions.Config, _users), userId);
var pipelineFilePath = Path.Combine(folderPath, _pipelines + _fileExtension);

Directory.CreateDirectory(folderPath);

Expand All @@ -200,16 +209,14 @@ public Stream WritePipelineMap(

public bool AttachmentExists(string catalogId, string attachmentId)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, physicalId), attachmentId);
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId)), attachmentId);

return File.Exists(attachmentFile);
}

public IEnumerable<string> EnumerateAttachments(string catalogId)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFolder = SafePathCombine(_pathsOptions.Catalogs, physicalId);
var attachmentFolder = SafePathCombine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId));

if (Directory.Exists(attachmentFolder))
return Directory
Expand All @@ -224,8 +231,7 @@ public bool TryReadAttachment(string catalogId, string attachmentId, [NotNullWhe
{
attachment = default;

var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFolder = Path.Combine(_pathsOptions.Catalogs, physicalId);
var attachmentFolder = Path.Combine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId));

if (Directory.Exists(attachmentFolder))
{
Expand All @@ -245,8 +251,7 @@ public bool TryReadFirstAttachment(string catalogId, string searchPattern, Enume
{
attachment = default;

var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFolder = SafePathCombine(_pathsOptions.Catalogs, physicalId);
var attachmentFolder = SafePathCombine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId));

if (Directory.Exists(attachmentFolder))
{
Expand All @@ -266,8 +271,7 @@ public bool TryReadFirstAttachment(string catalogId, string searchPattern, Enume

public Stream WriteAttachment(string catalogId, string attachmentId)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, physicalId), attachmentId);
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId)), attachmentId);
var attachmentFolder = Path.GetDirectoryName(attachmentFile)!;

Directory.CreateDirectory(attachmentFolder);
Expand All @@ -277,8 +281,7 @@ public Stream WriteAttachment(string catalogId, string attachmentId)

public void DeleteAttachment(string catalogId, string attachmentId)
{
var physicalId = catalogId.TrimStart('/').Replace("/", "_");
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, physicalId), attachmentId);
var attachmentFile = SafePathCombine(Path.Combine(_pathsOptions.Catalogs, GetPhysicalCatalogId(catalogId)), attachmentId);

File.Delete(attachmentFile);
}
Expand Down Expand Up @@ -310,7 +313,7 @@ public Stream WriteArtifact(string fileName)

/* /cache */
private string GetCacheEntryDirectoryPath(string catalogId, DateOnly day)
=> Path.Combine(_pathsOptions.Cache, $"{catalogId.TrimStart('/').Replace("/", "_")}/{day:yyyy-MM}/{day:dd}");
=> Path.Combine(_pathsOptions.Cache, $"{GetPhysicalCatalogId(catalogId)}/{day:yyyy-MM}/{day:dd}");

private string GetCacheEntryId(CatalogItem catalogItem, DateTime begin)
{
Expand Down Expand Up @@ -414,7 +417,6 @@ private static async Task DeleteCacheEntryAsync(string cacheEntry, TimeSpan time
throw new Exception($"Cannot delete cache entry {cacheEntry}.");
}

//
private static string SafePathCombine(string basePath, string relativePath)
{
var filePath = Path.GetFullPath(Path.Combine(basePath, relativePath));
Expand All @@ -424,4 +426,9 @@ private static string SafePathCombine(string basePath, string relativePath)

return filePath;
}

private string GetPhysicalCatalogId(string catalogId)
{
return catalogId.TrimStart('/').Replace("/", "_");
}
}
Loading