22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5+ using System . Runtime . InteropServices ;
56using System . Threading ;
67using EmbedIO . Utilities ;
78
@@ -18,6 +19,9 @@ public class FileSystemProvider : IDisposable, IFileProvider
1819 /// <summary>
1920 /// Initializes a new instance of the <see cref="FileSystemProvider"/> class.
2021 /// </summary>
22+ /// <remarks>
23+ /// OSX doesn't support <see cref="FileSystemWatcher" />, the parameter <paramref name="isImmutable" /> will be always <see langword="true"/>.
24+ /// </remarks>
2125 /// <param name="fileSystemPath">The file system path.</param>
2226 /// <param name="isImmutable"><see langword="true"/> if files and directories in
2327 /// <paramref name="fileSystemPath"/> are not expected to change during a web server's
@@ -28,10 +32,17 @@ public class FileSystemProvider : IDisposable, IFileProvider
2832 public FileSystemProvider ( string fileSystemPath , bool isImmutable )
2933 {
3034 FileSystemPath = Validate . LocalPath ( nameof ( fileSystemPath ) , fileSystemPath , true ) ;
31- IsImmutable = isImmutable ;
35+ IsImmutable = isImmutable || RuntimeInformation . IsOSPlatform ( OSPlatform . OSX ) ;
3236
33- if ( ! IsImmutable )
34- _watcher = new FileSystemWatcher ( FileSystemPath ) ;
37+ try
38+ {
39+ if ( ! IsImmutable )
40+ _watcher = new FileSystemWatcher ( FileSystemPath ) ;
41+ }
42+ catch ( PlatformNotSupportedException )
43+ {
44+ IsImmutable = true ;
45+ }
3546 }
3647
3748 /// <summary>
@@ -84,7 +95,7 @@ public void Start(CancellationToken cancellationToken)
8495 {
8596 // Unescape the url before continue
8697 urlPath = Uri . UnescapeDataString ( urlPath ) ;
87-
98+
8899 // Bail out early if the path is a rooted path,
89100 // as Path.Combine would ignore our base path.
90101 // See https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine
@@ -165,10 +176,10 @@ private static MappedResourceInfo GetMappedFileInfo(IMimeTypeProvider mimeTypePr
165176
166177 private static MappedResourceInfo GetMappedFileInfo ( IMimeTypeProvider mimeTypeProvider , FileInfo info )
167178 => MappedResourceInfo . ForFile (
168- info . FullName ,
169- info . Name ,
170- info . LastWriteTimeUtc ,
171- info . Length ,
179+ info . FullName ,
180+ info . Name ,
181+ info . LastWriteTimeUtc ,
182+ info . Length ,
172183 mimeTypeProvider . GetMimeType ( info . Extension ) ) ;
173184
174185 private static MappedResourceInfo GetMappedDirectoryInfo ( string localPath )
@@ -180,7 +191,7 @@ private static MappedResourceInfo GetMappedDirectoryInfo(DirectoryInfo info)
180191 private static MappedResourceInfo GetMappedResourceInfo ( IMimeTypeProvider mimeTypeProvider , FileSystemInfo info )
181192 => info is DirectoryInfo directoryInfo
182193 ? GetMappedDirectoryInfo ( directoryInfo )
183- : GetMappedFileInfo ( mimeTypeProvider , ( FileInfo ) info ) ;
194+ : GetMappedFileInfo ( mimeTypeProvider , ( FileInfo ) info ) ;
184195
185196 private void Watcher_ChangedOrDeleted ( object sender , FileSystemEventArgs e )
186197 => ResourceChanged ? . Invoke ( e . FullPath ) ;
0 commit comments