Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unified null checks + enabled latest lang features #102

Merged
merged 5 commits into from
Oct 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 2
jobs:
build:
docker:
- image: mcr.microsoft.com/dotnet/core/sdk:3.1
- image: mcr.microsoft.com/dotnet/sdk:5.0
steps:
- checkout
- run:
Expand Down
1 change: 1 addition & 0 deletions Directory.build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project>
<PropertyGroup>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<LangVersion>preview</LangVersion>
</PropertyGroup>
</Project>
8 changes: 4 additions & 4 deletions WopiHost.Abstractions/WopiHost.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>WopiHost.Abstractions Class Library</Description>
<Authors>Petr Svihlik</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.Abstractions</AssemblyName>
<PackageId>WopiHost.Abstractions</PackageId>
<PackageLicenseUrl>https://github.com/petrsvihlik/WopiHost/blob/master/LICENSE.txt</PackageLicenseUrl>
Expand All @@ -29,13 +29,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.AspNetCore.Authorization" Version="3.1.9" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.Security.Claims" Version="4.3.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.6.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.8.0" />
</ItemGroup>

</Project>
24 changes: 9 additions & 15 deletions WopiHost.Cobalt/CobaltSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,10 @@ private CobaltFile GetCobaltFile(IWopiFile file, ClaimsPrincipal principal)

if (file.Exists)
{
using (var stream = file.GetReadStream())
{
var srcAtom = new AtomFromStream(stream);
tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).SetStream(RootId.Default.Value, srcAtom, out var o1);
tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).GetStream(RootId.Default.Value).Flush();
}
using var stream = file.GetReadStream();
var srcAtom = new AtomFromStream(stream);
tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).SetStream(RootId.Default.Value, srcAtom, out var o1);
tempCobaltFile.GetCobaltFilePartition(FilePartitionId.Content).GetStream(RootId.Default.Value).Flush();
}
return tempCobaltFile;
}
Expand All @@ -65,11 +63,9 @@ private CobaltFile GetCobaltFile(IWopiFile file, ClaimsPrincipal principal)
public Stream GetFileStream(IWopiFile file, ClaimsPrincipal principal)
{
//TODO: use in filescontroller
using (var ms = new MemoryStream())
{
new GenericFda(GetCobaltFile(file, principal).CobaltEndpoint).GetContentStream().CopyTo(ms);
return ms;
}
using var ms = new MemoryStream();
new GenericFda(GetCobaltFile(file, principal).CobaltEndpoint).GetContentStream().CopyTo(ms);
return ms;
}


Expand All @@ -86,10 +82,8 @@ public Action<Stream> ProcessCobalt(IWopiFile file, ClaimsPrincipal principal, b

if (requestBatch.Requests.Any(request => request is PutChangesRequest && request.PartitionId == FilePartitionId.Content))
{
using (var stream = file.GetWriteStream())
{
new GenericFda(cobaltFile.CobaltEndpoint).GetContentStream().CopyTo(stream);
}
using var stream = file.GetWriteStream();
new GenericFda(cobaltFile.CobaltEndpoint).GetContentStream().CopyTo(stream);
}
var response = requestBatch.SerializeOutputToProtocol(protocolVersion);
Action<Stream> copyToAction = s => { response.CopyTo(s); };
Expand Down
4 changes: 2 additions & 2 deletions WopiHost.Cobalt/WopiHost.Cobalt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>WopiHost.Cobalt Class Library</Description>
<Authors>Petr Svihlik</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.Cobalt</AssemblyName>
<PackageId>WopiHost.Cobalt</PackageId>
<PackageLicenseUrl>https://github.com/petrsvihlik/WopiHost/blob/master/LICENSE.txt</PackageLicenseUrl>
Expand Down Expand Up @@ -31,7 +31,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.CobaltCore" Version="15.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
8 changes: 4 additions & 4 deletions WopiHost.Core/Controllers/FilesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public class FilesController : WopiControllerBase

private HostCapabilities HostCapabilities => new HostCapabilities
{
SupportsCobalt = CobaltProcessor is { },
SupportsCobalt = CobaltProcessor is not null,
SupportsGetLock = true,
SupportsLocks = true,
SupportsExtendedLockLength = true,
SupportsFolders = true,//?
SupportsCoauth = true,//?
SupportsUpdate = nameof(PutFile) is { } //&& PutRelativeFile - usercannotwriterelative
SupportsUpdate = nameof(PutFile) is not null //&& PutRelativeFile - usercannotwriterelative
};

/// <summary>
Expand Down Expand Up @@ -87,7 +87,7 @@ public async Task<IActionResult> GetFile(string id)

// Check expected size
var maximumExpectedSize = HttpContext.Request.Headers[WopiHeaders.MAX_EXPECTED_SIZE].ToString().ToNullableInt();
if (maximumExpectedSize is { } && file.GetCheckFileInfo(User, HostCapabilities).Size > maximumExpectedSize.Value)
if (maximumExpectedSize is not null && file.GetCheckFileInfo(User, HostCapabilities).Size > maximumExpectedSize.Value)
{
return new PreconditionFailedResult();
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public async Task<IActionResult> ProcessCobalt(string id)

// TODO: remove workaround https://github.com/aspnet/Announcements/issues/342 (use FileBufferingWriteStream)
var syncIoFeature = HttpContext.Features.Get<IHttpBodyControlFeature>();
if (syncIoFeature is { })
if (syncIoFeature is not null)
{
syncIoFeature.AllowSynchronousIO = true;
}
Expand Down
2 changes: 1 addition & 1 deletion WopiHost.Core/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static CheckFileInfo GetCheckFileInfo(this IWopiFile file, ClaimsPrincipa
}

var checkFileInfo = new CheckFileInfo();
if (principal is { })
if (principal is not null)
{
checkFileInfo.UserId = principal.FindFirst(ClaimTypes.NameIdentifier)?.Value.ToSafeIdentity();
checkFileInfo.UserFriendlyName = principal.FindFirst(ClaimTypes.Name)?.Value;
Expand Down
18 changes: 15 additions & 3 deletions WopiHost.Core/HttpHeaderAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@

namespace WopiHost.Core
{
/// <summary>
/// A header-based constraint for HTTP actions.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class HttpHeaderAttribute : Attribute, IActionConstraint
{
public string Header { get; set; }
public string[] Values { get; set; }
private string Header { get; set; }

private string[] Values { get; set; }

/// <summary>
/// Creates an instance of a constraint based on a header name and allowed values.
/// </summary>
/// <param name="header">Header name to check.</param>
/// <param name="values">Accepted header values.</param>
public HttpHeaderAttribute(string header, params string[] values)
{
Header = header;
Values = values;
}

/// <inheritdoc />
public bool Accept(ActionConstraintContext context)
{
return (context is null) && (context.RouteContext.HttpContext.Request.Headers.TryGetValue(Header, out var value) && Values.Contains(value[0]));
return (context is not null) && context.RouteContext.HttpContext.Request.Headers.TryGetValue(Header, out var value) && Values.Contains(value[0]);
}

/// <inheritdoc />
public int Order => 0;
}
}
4 changes: 2 additions & 2 deletions WopiHost.Core/Results/FileResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public override async Task ExecuteResultAsync(ActionContext context)
var response = context.HttpContext.Response;
response.ContentType = ContentType;
var targetStream = response.Body;
if (CopyStream is { })
if (CopyStream is not null)
{
await Task.Factory.StartNew(() =>
{
CopyStream(targetStream);
});
}
else if (Content is { })
else if (Content is not null)
{
await targetStream.WriteAsync(Content, 0, Content.Length);
}
Expand Down
8 changes: 4 additions & 4 deletions WopiHost.Core/WopiHost.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>WopiHost.Core Class Library</Description>
<Authors>Petr Svihlik</Authors>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.Core</AssemblyName>
<PackageId>WopiHost.Core</PackageId>
<PackageLicenseUrl>https://github.com/petrsvihlik/WopiHost/blob/master/LICENSE.txt</PackageLicenseUrl>
Expand Down Expand Up @@ -34,12 +34,12 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.6.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.9" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.8.0" />
</ItemGroup>

</Project>
12 changes: 11 additions & 1 deletion WopiHost.Core/WopiOverrideHeaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
namespace WopiHost.Core
using System;

namespace WopiHost.Core
{
/// <summary>
/// An action constraint based on the X-WOPI-Override header.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
public class WopiOverrideHeaderAttribute : HttpHeaderAttribute
{
/// <summary>
/// Creates an instance of the header-based constraint based on allowed values.
/// </summary>
/// <param name="values">Accepted header values.</param>
public WopiOverrideHeaderAttribute(string[] values) : base(WopiHeaders.WOPI_OVERRIDE, values)
{
}
Expand Down
2 changes: 1 addition & 1 deletion WopiHost.Discovery.Tests/WopiDiscovererTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public async void FavIconTests(string extension, string expectedValue, string fi
var result = await _wopiDiscoverer.GetApplicationFavIconAsync(extension);

// Assert
Assert.Equal(expectedValue != null ? new Uri(expectedValue) : null, result);
Assert.Equal(expectedValue is not null ? new Uri(expectedValue) : null, result);
}

[Theory]
Expand Down
8 changes: 4 additions & 4 deletions WopiHost.Discovery.Tests/WopiHost.Discovery.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Authors>Petr Svihlik</Authors>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.Discovery.Tests</AssemblyName>
<PackageId>WopiHost.Discovery.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand All @@ -25,16 +25,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
12 changes: 6 additions & 6 deletions WopiHost.Discovery/WopiDiscoverer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public WopiDiscoverer(IDiscoveryFileProvider discoveryFileProvider, NetZoneEnum

private async Task<IEnumerable<XElement>> GetAppsAsync()
{
if (_apps == null)
if (_apps is null)
{
_apps = (await DiscoveryFileProvider.GetDiscoveryXmlAsync())
.Elements(ElementNetZone)
Expand Down Expand Up @@ -65,15 +65,15 @@ public async Task<bool> SupportsExtensionAsync(string extension)
{
var query = (await GetAppsAsync()).Elements()
.FirstOrDefault(e => (string)e.Attribute(AttrActionExtension) == extension);
return query is { };
return query is not null;
}

///<inheritdoc />
public async Task<bool> SupportsActionAsync(string extension, WopiActionEnum action)
{
var actionString = action.ToString().ToLowerInvariant();

var query = (await GetAppsAsync()).Elements().Where(e => (string)e.Attribute(AttrActionExtension) == extension && (string)e.Attribute(AttrActionName).Value.ToLowerInvariant() == actionString);
var query = (await GetAppsAsync()).Elements().Where(e => (string)e.Attribute(AttrActionExtension) == extension && e.Attribute(AttrActionName).Value.ToLowerInvariant() == actionString);

return query.Any();
}
Expand All @@ -83,7 +83,7 @@ public async Task<IEnumerable<string>> GetActionRequirementsAsync(string extensi
{
var actionString = action.ToString().ToLowerInvariant();

var query = (await GetAppsAsync()).Elements().Where(e => (string)e.Attribute(AttrActionExtension) == extension && (string)e.Attribute(AttrActionName).Value.ToLowerInvariant() == actionString).Select(e => e.Attribute(AttrActionRequires).Value.Split(','));
var query = (await GetAppsAsync()).Elements().Where(e => (string)e.Attribute(AttrActionExtension) == extension && e.Attribute(AttrActionName).Value.ToLowerInvariant() == actionString).Select(e => e.Attribute(AttrActionRequires).Value.Split(','));

return query.FirstOrDefault();
}
Expand All @@ -92,7 +92,7 @@ public async Task<IEnumerable<string>> GetActionRequirementsAsync(string extensi
public async Task<bool> RequiresCobaltAsync(string extension, WopiActionEnum action)
{
var requirements = await GetActionRequirementsAsync(extension, action);
return requirements is { } && requirements.Contains(AttrValCobalt);
return requirements is not null && requirements.Contains(AttrValCobalt);
}

///<inheritdoc />
Expand All @@ -116,7 +116,7 @@ public async Task<Uri> GetApplicationFavIconAsync(string extension)
{
var query = (await GetAppsAsync()).Where(e => e.Descendants(ElementAction).Any(d => (string)d.Attribute(AttrActionExtension) == extension)).Select(e => e.Attribute(AttrAppFavicon).Value);
var result = query.FirstOrDefault();
return result != null ? new Uri(result) : null;
return result is not null ? new Uri(result) : null;
}
}
}
4 changes: 2 additions & 2 deletions WopiHost.Discovery/WopiHost.Discovery.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>WopiHost.Discovery Class Library</Description>
<Authors>Petr Svihlik</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.Discovery</AssemblyName>
<PackageId>WopiHost.Discovery</PackageId>
<PackageLicenseUrl>https://github.com/petrsvihlik/WopiHost/blob/master/LICENSE.txt</PackageLicenseUrl>
Expand All @@ -29,7 +29,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<Authors>Petr Svihlik</Authors>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<AssemblyName>WopiHost.FileSystemProvider.Tests</AssemblyName>
<PackageId>WopiHost.FileSystemProvider.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
Expand All @@ -19,16 +19,16 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit.runner.console" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Loading