Skip to content

Commit

Permalink
[REFACTOR] 127 Replace Magic Strings in DatabaseService (#148)
Browse files Browse the repository at this point in the history
* [REFACTOR] 127 Replace Magic Strings in DatabaseService

- moved trim replace sequence to its own function
- replaced magic strings by constants

* [REFACTOR] 127 Replace Magic Strings in DatabaseService

- code review changes

---------

Co-authored-by: Timo Schauties <[email protected]>
  • Loading branch information
Conundraah and Timo Schauties authored Sep 5, 2024
1 parent a582937 commit 98ea2f0
Showing 1 changed file with 40 additions and 33 deletions.
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 const string USERS = "users";

private const string CATALOGS = "catalogs";

private const string FILE_EXTENSION = ".json";

private const string PROJECT = "project";

private const string TOKENS = "tokens";

private const 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)}" + FILE_EXTENSION;
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)}" + FILE_EXTENSION;
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 + FILE_EXTENSION);
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 + FILE_EXTENSION);
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 + FILE_EXTENSION);

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 + FILE_EXTENSION);

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 + FILE_EXTENSION);

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 + FILE_EXTENSION);

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("/", "_");
}
}

0 comments on commit 98ea2f0

Please sign in to comment.