Skip to content

Commit 715aa02

Browse files
authored
[ADD] OSX check to FileSystemProvider (#424)
* [ADD] OSX check to FileSystemProvider * Fix logic of IsImmutable * Update EmbedIO.csproj * Change logic to use only Windows the FileWatcher * Revert last commit * Update build.yml * Add Warning
1 parent bcd2d64 commit 715aa02

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jobs:
88
runs-on: ${{ matrix.os }}
99
strategy:
1010
matrix:
11-
os: [ubuntu-latest]
11+
os: [ubuntu-latest, macos-latest]
1212

1313
steps:
1414
- uses: actions/checkout@v1

src/EmbedIO/Files/FileSystemProvider.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Runtime.InteropServices;
56
using System.Threading;
67
using 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);

src/EmbedIO/WebModuleContainerExtensions-Files.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public static TContainer WithStaticFolder<TContainer>(
4242
/// a <seealso cref="FileModule"/>, and adds the latter to a module container,
4343
/// giving it the specified <paramref name="name"/> if not <see langword="null"/>.
4444
/// </summary>
45+
/// <remarks>
46+
/// OSX doesn't support <see cref="FileSystemWatcher" />, the parameter <paramref name="isImmutable" /> will be always <see langword="true"/>.
47+
/// </remarks>
4548
/// <typeparam name="TContainer">The type of the module container.</typeparam>
4649
/// <param name="this">The <typeparamref name="TContainer"/> on which this method is called.</param>
4750
/// <param name="name">The name.</param>

test/EmbedIO.Tests/StaticFilesModuleTest.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net;
88
using System.Net.Http;
99
using System.Net.Http.Headers;
10+
using System.Runtime.InteropServices;
1011
using System.Threading.Tasks;
1112
using EmbedIO.Testing;
1213
using EmbedIO.Utilities;
@@ -84,6 +85,9 @@ public async Task TestHeadIndex()
8485
[Test]
8586
public async Task FileWritable()
8687
{
88+
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
89+
Assert.Ignore("OSX doesn't support FileSystemWatcher");
90+
8791
var root = Path.GetTempPath();
8892
var file = Path.Combine(root, "index.html");
8993
File.WriteAllText(file, Resources.Index);

0 commit comments

Comments
 (0)