@@ -197,8 +197,8 @@ private class FileCacheEntry
197
197
198
198
public FileCacheEntry ( FileCache cache , string prefix , string path , string filter , InsertHandler handler , TimeSpan timespan )
199
199
{
200
- _prefix = prefix ;
201
- _path = path ;
200
+ _prefix = prefix . Replace ( ' \\ ' , '/' ) . RemoveSuffix ( '/' ) ;
201
+ _path = path . Replace ( ' \\ ' , '/' ) . RemoveSuffix ( '/' ) ;
202
202
_handler = handler ;
203
203
_timespan = timespan ;
204
204
_watcher = new FileSystemWatcher ( ) ;
@@ -242,8 +242,8 @@ private static bool IsDirectory(string path)
242
242
243
243
private static void OnCreated ( object sender , FileSystemEventArgs e , FileCache cache , FileCacheEntry entry )
244
244
{
245
- var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path + "/" , entry . _prefix ) ;
246
- var file = e . FullPath . Replace ( '\\ ' , '/' ) ;
245
+ var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path , entry . _prefix ) . RemoveSuffix ( '/' ) ;
246
+ var file = e . FullPath . Replace ( '\\ ' , '/' ) . RemoveSuffix ( '/' ) ;
247
247
248
248
// Skip missing files
249
249
if ( ! File . Exists ( file ) )
@@ -260,8 +260,8 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca
260
260
if ( e . ChangeType != WatcherChangeTypes . Changed )
261
261
return ;
262
262
263
- var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path + "/" , entry . _prefix ) ;
264
- var file = e . FullPath . Replace ( '\\ ' , '/' ) ;
263
+ var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path , entry . _prefix ) . RemoveSuffix ( '/' ) ;
264
+ var file = e . FullPath . Replace ( '\\ ' , '/' ) . RemoveSuffix ( '/' ) ;
265
265
266
266
// Skip missing files
267
267
if ( ! File . Exists ( file ) )
@@ -275,18 +275,18 @@ private static void OnChanged(object sender, FileSystemEventArgs e, FileCache ca
275
275
276
276
private static void OnDeleted ( object sender , FileSystemEventArgs e , FileCache cache , FileCacheEntry entry )
277
277
{
278
- var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path + "/" , entry . _prefix ) ;
279
- var file = e . FullPath . Replace ( '\\ ' , '/' ) ;
278
+ var key = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path , entry . _prefix ) . RemoveSuffix ( '/' ) ;
279
+ var file = e . FullPath . Replace ( '\\ ' , '/' ) . RemoveSuffix ( '/' ) ;
280
280
281
281
cache . RemoveFileInternal ( entry . _path , key ) ;
282
282
}
283
283
284
284
private static void OnRenamed ( object sender , RenamedEventArgs e , FileCache cache , FileCacheEntry entry )
285
285
{
286
- var oldKey = e . OldFullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path + "/" , entry . _prefix ) ;
287
- var oldFile = e . OldFullPath . Replace ( '\\ ' , '/' ) ;
288
- var newKey = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path + "/" , entry . _prefix ) ;
289
- var newFile = e . FullPath . Replace ( '\\ ' , '/' ) ;
286
+ var oldKey = e . OldFullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path , entry . _prefix ) . RemoveSuffix ( '/' ) ;
287
+ var oldFile = e . OldFullPath . Replace ( '\\ ' , '/' ) . RemoveSuffix ( '/' ) ;
288
+ var newKey = e . FullPath . Replace ( '\\ ' , '/' ) . Replace ( entry . _path , entry . _prefix ) . RemoveSuffix ( '/' ) ;
289
+ var newFile = e . FullPath . Replace ( '\\ ' , '/' ) . RemoveSuffix ( '/' ) ;
290
290
291
291
// Skip missing files
292
292
if ( ! File . Exists ( newFile ) )
@@ -339,12 +339,10 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa
339
339
{
340
340
try
341
341
{
342
- string keyPrefix = ( string . IsNullOrEmpty ( prefix ) || ( prefix == "/" ) ) ? "/" : ( prefix + "/" ) ;
343
-
344
342
// Iterate through all directory entries
345
343
foreach ( var item in Directory . GetDirectories ( path ) )
346
344
{
347
- string key = keyPrefix + HttpUtility . UrlDecode ( Path . GetFileName ( item ) ) ;
345
+ string key = prefix + "/" + HttpUtility . UrlDecode ( Path . GetFileName ( item ) ) ;
348
346
349
347
// Recursively insert sub-directory
350
348
if ( ! InsertPathInternal ( root , item , key , timeout , handler ) )
@@ -353,7 +351,7 @@ private bool InsertPathInternal(string root, string path, string prefix, TimeSpa
353
351
354
352
foreach ( var item in Directory . GetFiles ( path ) )
355
353
{
356
- string key = keyPrefix + HttpUtility . UrlDecode ( Path . GetFileName ( item ) ) ;
354
+ string key = prefix + "/" + HttpUtility . UrlDecode ( Path . GetFileName ( item ) ) ;
357
355
358
356
// Insert file into the cache
359
357
if ( ! InsertFileInternal ( root , item , key , timeout , handler ) )
@@ -475,4 +473,13 @@ public WriteLock(ReaderWriterLockSlim locker) : base(locker.ExitWriteLock)
475
473
locker . EnterWriteLock ( ) ;
476
474
}
477
475
}
476
+
477
+ /// <summary>
478
+ /// String extensions utility class.
479
+ /// </summary>
480
+ public static class StringExtensions
481
+ {
482
+ public static string RemoveSuffix ( this string str , char toRemove ) => str . EndsWith ( toRemove ) ? str . Substring ( 0 , str . Length - 1 ) : str ;
483
+ public static string RemoveSuffix ( this string str , string toRemove ) => str . EndsWith ( toRemove ) ? str . Substring ( 0 , str . Length - toRemove . Length ) : str ;
484
+ }
478
485
}
0 commit comments