Skip to content

Commit

Permalink
[ADD] OSX check to FileSystemProvider (#424)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
geoperez authored Jan 15, 2020
1 parent bcd2d64 commit 715aa02
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
os: [ubuntu-latest, macos-latest]

steps:
- uses: actions/checkout@v1
Expand Down
29 changes: 20 additions & 9 deletions src/EmbedIO/Files/FileSystemProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using EmbedIO.Utilities;

Expand All @@ -18,6 +19,9 @@ public class FileSystemProvider : IDisposable, IFileProvider
/// <summary>
/// Initializes a new instance of the <see cref="FileSystemProvider"/> class.
/// </summary>
/// <remarks>
/// OSX doesn't support <see cref="FileSystemWatcher" />, the parameter <paramref name="isImmutable" /> will be always <see langword="true"/>.
/// </remarks>
/// <param name="fileSystemPath">The file system path.</param>
/// <param name="isImmutable"><see langword="true"/> if files and directories in
/// <paramref name="fileSystemPath"/> are not expected to change during a web server's
Expand All @@ -28,10 +32,17 @@ public class FileSystemProvider : IDisposable, IFileProvider
public FileSystemProvider(string fileSystemPath, bool isImmutable)
{
FileSystemPath = Validate.LocalPath(nameof(fileSystemPath), fileSystemPath, true);
IsImmutable = isImmutable;
IsImmutable = isImmutable || RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

if (!IsImmutable)
_watcher = new FileSystemWatcher(FileSystemPath);
try
{
if (!IsImmutable)
_watcher = new FileSystemWatcher(FileSystemPath);
}
catch (PlatformNotSupportedException)
{
IsImmutable = true;
}
}

/// <summary>
Expand Down Expand Up @@ -84,7 +95,7 @@ public void Start(CancellationToken cancellationToken)
{
// Unescape the url before continue
urlPath = Uri.UnescapeDataString(urlPath);

// Bail out early if the path is a rooted path,
// as Path.Combine would ignore our base path.
// See https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine
Expand Down Expand Up @@ -165,10 +176,10 @@ private static MappedResourceInfo GetMappedFileInfo(IMimeTypeProvider mimeTypePr

private static MappedResourceInfo GetMappedFileInfo(IMimeTypeProvider mimeTypeProvider, FileInfo info)
=> MappedResourceInfo.ForFile(
info.FullName,
info.Name,
info.LastWriteTimeUtc,
info.Length,
info.FullName,
info.Name,
info.LastWriteTimeUtc,
info.Length,
mimeTypeProvider.GetMimeType(info.Extension));

private static MappedResourceInfo GetMappedDirectoryInfo(string localPath)
Expand All @@ -180,7 +191,7 @@ private static MappedResourceInfo GetMappedDirectoryInfo(DirectoryInfo info)
private static MappedResourceInfo GetMappedResourceInfo(IMimeTypeProvider mimeTypeProvider, FileSystemInfo info)
=> info is DirectoryInfo directoryInfo
? GetMappedDirectoryInfo(directoryInfo)
: GetMappedFileInfo(mimeTypeProvider, (FileInfo)info);
: GetMappedFileInfo(mimeTypeProvider, (FileInfo) info);

private void Watcher_ChangedOrDeleted(object sender, FileSystemEventArgs e)
=> ResourceChanged?.Invoke(e.FullPath);
Expand Down
3 changes: 3 additions & 0 deletions src/EmbedIO/WebModuleContainerExtensions-Files.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public static TContainer WithStaticFolder<TContainer>(
/// a <seealso cref="FileModule"/>, and adds the latter to a module container,
/// giving it the specified <paramref name="name"/> if not <see langword="null"/>.
/// </summary>
/// <remarks>
/// OSX doesn't support <see cref="FileSystemWatcher" />, the parameter <paramref name="isImmutable" /> will be always <see langword="true"/>.
/// </remarks>
/// <typeparam name="TContainer">The type of the module container.</typeparam>
/// <param name="this">The <typeparamref name="TContainer"/> on which this method is called.</param>
/// <param name="name">The name.</param>
Expand Down
4 changes: 4 additions & 0 deletions test/EmbedIO.Tests/StaticFilesModuleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using EmbedIO.Testing;
using EmbedIO.Utilities;
Expand Down Expand Up @@ -84,6 +85,9 @@ public async Task TestHeadIndex()
[Test]
public async Task FileWritable()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
Assert.Ignore("OSX doesn't support FileSystemWatcher");

var root = Path.GetTempPath();
var file = Path.Combine(root, "index.html");
File.WriteAllText(file, Resources.Index);
Expand Down

0 comments on commit 715aa02

Please sign in to comment.