Skip to content

Commit

Permalink
Merge pull request #961 from eventflow/fix-relative-path
Browse files Browse the repository at this point in the history
Fix: Path.GetRelativePath is there in .NET now
  • Loading branch information
rasmus authored Nov 11, 2022
2 parents acc2317 + 19cb381 commit 3c6ad60
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 41 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Changes since last 1.x pre-release, `1.0.5001-alpha`
vulnerability
- https://github.com/advisories/GHSA-5crp-9r3c-p9vr
- https://security.snyk.io/vuln/SNYK-DOTNET-NEWTONSOFTJSON-2774678
* Fix: `UseFilesEventPersistence` should no longer throw exception for .NET regarding relative paths

Complete 1.0 change log

Expand Down
42 changes: 1 addition & 41 deletions Source/EventFlow/EventStores/Files/FilesEventPersistence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public async Task<IReadOnlyCollection<ICommittedDomainEvent>> CommitEventsAsync(
{
var eventPath = _filesEventLocator.GetEventPath(id, serializedEvent.AggregateSequenceNumber);
_globalSequenceNumber++;
_eventLog[_globalSequenceNumber] = GetRelativePath(_configuration.StorePath, eventPath);
_eventLog[_globalSequenceNumber] = Path.GetRelativePath(_configuration.StorePath, eventPath);

var fileEventData = new FileEventData
{
Expand Down Expand Up @@ -278,45 +278,5 @@ private EventStoreLog RecreateEventStoreLog(string path)
Log = directory,
};
}

/// <summary>
/// Creates a relative path from one file or folder to another.
/// </summary>
/// <param name="relativeTo">Contains the directory that defines the start of the relative path.</param>
/// <param name="path">Contains the path that defines the endpoint of the relative path.</param>
/// <returns>The relative path from the start directory to the end path or <c>toPath</c> if the paths are not related.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="UriFormatException"></exception>
/// <exception cref="InvalidOperationException"></exception>
private string GetRelativePath(string relativeTo, string path)
{
#if NETCOREAPP3_1 || NETCOREAPP3_0
return Path.GetRelativePath(relativeTo, path);
#else
if (string.IsNullOrEmpty(relativeTo))
throw new ArgumentNullException(nameof(relativeTo));
if (string.IsNullOrEmpty(path))
throw new ArgumentNullException(nameof(path));

if (relativeTo.Last() != Path.DirectorySeparatorChar && relativeTo.Last() != Path.AltDirectorySeparatorChar)
relativeTo += Path.DirectorySeparatorChar;

var fromUri = new Uri(relativeTo);
var toUri = new Uri(path);

if (fromUri.Scheme != toUri.Scheme)
return path; // path can't be made relative.

var relativeUri = fromUri.MakeRelativeUri(toUri);
var relativePath = Uri.UnescapeDataString(relativeUri.ToString());

if (toUri.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase))
{
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
}

return relativePath;
#endif
}
}
}

0 comments on commit 3c6ad60

Please sign in to comment.