diff --git a/source/NetCoreServer/FileCache.cs b/source/NetCoreServer/FileCache.cs index e5f2814..4da0c3e 100644 --- a/source/NetCoreServer/FileCache.cs +++ b/source/NetCoreServer/FileCache.cs @@ -197,8 +197,8 @@ private class FileCacheEntry public FileCacheEntry(FileCache cache, string prefix, string path, string filter, InsertHandler handler, TimeSpan timespan) { - _prefix = prefix; - _path = path; + _prefix = prefix.Replace('\\', '/').RemoveSuffix('/'); + _path = path.Replace('\\', '/').RemoveSuffix('/'); _handler = handler; _timespan = timespan; _watcher = new FileSystemWatcher(); @@ -242,8 +242,8 @@ private static bool IsDirectory(string path) private static void OnCreated(object sender, FileSystemEventArgs e, FileCache cache, FileCacheEntry entry) { - var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix); - var file = e.FullPath.Replace('\\', '/'); + var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/'); + var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/'); // Skip missing files if (!File.Exists(file)) @@ -260,8 +260,8 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca if (e.ChangeType != WatcherChangeTypes.Changed) return; - var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix); - var file = e.FullPath.Replace('\\', '/'); + var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/'); + var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/'); // Skip missing files if (!File.Exists(file)) @@ -275,18 +275,18 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca private static void OnDeleted(object sender, FileSystemEventArgs e, FileCache cache, FileCacheEntry entry) { - var key = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix); - var file = e.FullPath.Replace('\\', '/'); + var key = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/'); + var file = e.FullPath.Replace('\\', '/').RemoveSuffix('/'); cache.RemoveFileInternal(entry._path, key); } private static void OnRenamed(object sender, RenamedEventArgs e, FileCache cache, FileCacheEntry entry) { - var oldKey = e.OldFullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix); - var oldFile = e.OldFullPath.Replace('\\', '/'); - var newKey = e.FullPath.Replace('\\', '/').Replace(entry._path + "/", entry._prefix); - var newFile = e.FullPath.Replace('\\', '/'); + var oldKey = e.OldFullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/'); + var oldFile = e.OldFullPath.Replace('\\', '/').RemoveSuffix('/'); + var newKey = e.FullPath.Replace('\\', '/').Replace(entry._path, entry._prefix).RemoveSuffix('/'); + var newFile = e.FullPath.Replace('\\', '/').RemoveSuffix('/'); // Skip missing files if (!File.Exists(newFile)) @@ -339,12 +339,10 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa { try { - string keyPrefix = (string.IsNullOrEmpty(prefix) || (prefix == "/")) ? "/" : (prefix + "/"); - // Iterate through all directory entries foreach (var item in Directory.GetDirectories(path)) { - string key = keyPrefix + HttpUtility.UrlDecode(Path.GetFileName(item)); + string key = prefix + "/" + HttpUtility.UrlDecode(Path.GetFileName(item)); // Recursively insert sub-directory if (!InsertPathInternal(root, item, key, timeout, handler)) @@ -353,7 +351,7 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa foreach (var item in Directory.GetFiles(path)) { - string key = keyPrefix + HttpUtility.UrlDecode(Path.GetFileName(item)); + string key = prefix + "/" + HttpUtility.UrlDecode(Path.GetFileName(item)); // Insert file into the cache if (!InsertFileInternal(root, item, key, timeout, handler)) @@ -475,4 +473,13 @@ public WriteLock(ReaderWriterLockSlim locker) : base(locker.ExitWriteLock) locker.EnterWriteLock(); } } + + /// + /// String extensions utility class. + /// + public static class StringExtensions + { + public static string RemoveSuffix(this string str, char toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - 1) : str; + public static string RemoveSuffix(this string str, string toRemove) => str.EndsWith(toRemove) ? str.Substring(0, str.Length - toRemove.Length) : str; + } } diff --git a/source/NetCoreServer/NetCoreServer.csproj b/source/NetCoreServer/NetCoreServer.csproj index 9a887a4..47499ef 100644 --- a/source/NetCoreServer/NetCoreServer.csproj +++ b/source/NetCoreServer/NetCoreServer.csproj @@ -2,7 +2,7 @@ net8.0 - 8.0.3.0 + 8.0.4.0 Ivan Shynkarenka Copyright (c) 2019-2023 Ivan Shynkarenka https://github.com/chronoxor/NetCoreServer