Skip to content

Commit

Permalink
removed dependency on IHttpContextAccessor because HasIso8601Fraction…
Browse files Browse the repository at this point in the history
…Bug was always returning true anyway, and it was the only place that used it
  • Loading branch information
lostmsu committed Feb 16, 2025
1 parent 1bb066a commit 6876497
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ internal class AzureBlobCollectionPropertyManager : PropertyManager<AzureBlobCol
{
private static readonly XElement s_xDavCollection = new(WebDavNamespaces.DavNs + "collection");

public AzureBlobCollectionPropertyManager(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) : base(GetProperties(httpContextAccessor, lockingManager))
public AzureBlobCollectionPropertyManager(ILockingManager lockingManager) : base(GetProperties(lockingManager))
{
}

private static DavProperty<AzureBlobCollection>[] GetProperties(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) => new DavProperty<AzureBlobCollection>[]
private static DavProperty<AzureBlobCollection>[] GetProperties(ILockingManager lockingManager) => new DavProperty<AzureBlobCollection>[]
{
// RFC-2518 properties
new DavCreationDate<AzureBlobCollection>(httpContextAccessor)
new DavCreationDate<AzureBlobCollection>()
{
Getter = collection => collection.StoreItemMetadata.CreatedOn,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ namespace NWebDav.Sample.AzureBlob.AzureBlob;

internal class AzureBlobItemPropertyManager : PropertyManager<AzureBlobItem>
{
public AzureBlobItemPropertyManager(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) : base(GetProperties(httpContextAccessor, lockingManager))
public AzureBlobItemPropertyManager(ILockingManager lockingManager) : base(GetProperties(lockingManager))
{
}

private static DavProperty<AzureBlobItem>[] GetProperties(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) => new DavProperty<AzureBlobItem>[]
private static DavProperty<AzureBlobItem>[] GetProperties(ILockingManager lockingManager) => new DavProperty<AzureBlobItem>[]
{
// RFC-2518 properties
new DavCreationDate<AzureBlobItem>(httpContextAccessor)
new DavCreationDate<AzureBlobItem>()
{
Getter = item => item.StoreItemMetadata.CreatedOn,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ internal class AzureContainerCollectionPropertyManager : PropertyManager<AzureCo
{
private static readonly XElement s_xDavCollection = new(WebDavNamespaces.DavNs + "collection");

public AzureContainerCollectionPropertyManager(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) : base(GetProperties(httpContextAccessor, lockingManager))
public AzureContainerCollectionPropertyManager(ILockingManager lockingManager) : base(GetProperties(lockingManager))
{
}

private static DavProperty<AzureContainerCollection>[] GetProperties(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) => new DavProperty<AzureContainerCollection>[]
private static DavProperty<AzureContainerCollection>[] GetProperties(ILockingManager lockingManager) => new DavProperty<AzureContainerCollection>[]
{
// RFC-2518 properties
new DavCreationDate<AzureContainerCollection>(httpContextAccessor)
new DavCreationDate<AzureContainerCollection>()
{
Getter = _ => DateTime.UnixEpoch,
},
Expand Down
33 changes: 10 additions & 23 deletions NWebDav.Server/Props/DavTypedProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,47 +188,34 @@ public abstract class DavIso8601Date<TEntry> : DavTypedProperty<TEntry, DateTime
{
private readonly Iso8601DateConverter _converter;

protected DavIso8601Date(IHttpContextAccessor httpContextAccessor)
protected DavIso8601Date()
{
_converter = new Iso8601DateConverter(httpContextAccessor);
_converter = Iso8601DateConverter.Instance;
}

private class Iso8601DateConverter : IConverter
{
private readonly IHttpContextAccessor _httpContextAccessor;

public Iso8601DateConverter(IHttpContextAccessor httpContextAccessor)
internal static readonly Iso8601DateConverter Instance = new();
Iso8601DateConverter()
{
_httpContextAccessor = httpContextAccessor;
}

public object ToXml(DateTime value)
{
// The older built-in Windows WebDAV clients have a problem, so
// they cannot deal with more than 3 digits for the
// milliseconds.
if (HasIso8601FractionBug)
{
// We need to recreate the date again, because the Windows 7
// WebDAV client cannot
var dt = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond, DateTimeKind.Utc);
return XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc);
}

return XmlConvert.ToString(value, XmlDateTimeSerializationMode.Utc);
// P.S. I think the previous comment meant "less than 3 digits".

// We need to recreate the date again, because the Windows 7
// WebDAV client cannot
var dt = new DateTime(value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second, value.Millisecond, DateTimeKind.Utc);
return XmlConvert.ToString(dt, XmlDateTimeSerializationMode.Utc);
}

public DateTime FromXml(object value) => XmlConvert.ToDateTime((string)value, XmlDateTimeSerializationMode.Utc);

private bool HasIso8601FractionBug
{
get
{
var userAgent = _httpContextAccessor.HttpContext?.Request.Headers.UserAgent.FirstOrDefault();
_ = userAgent; // TODO: Determine if this bug is present based on the user-agent
return true;
}
}
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions NWebDav.Server/Props/StandardProperties.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Xml.Linq;
using Microsoft.AspNetCore.Http;
using NWebDav.Server.Stores;

namespace NWebDav.Server.Props;
Expand All @@ -25,7 +24,7 @@ public class DavCreationDate<TEntry> : DavIso8601Date<TEntry> where TEntry : ISt
// ReSharper disable once StaticMemberInGenericType
public static readonly XName PropertyName = WebDavNamespaces.DavNs + "creationdate";

public DavCreationDate(IHttpContextAccessor httpContextAccessor) : base(httpContextAccessor)
public DavCreationDate() : base()
{}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions NWebDav.Server/Stores/DiskStoreCollectionPropertyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class DiskStoreCollectionPropertyManager : PropertyManager<DiskStoreColle
{
private static readonly XElement s_xDavCollection = new(WebDavNamespaces.DavNs + "collection");

public DiskStoreCollectionPropertyManager(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) : base(GetProperties(httpContextAccessor, lockingManager))
public DiskStoreCollectionPropertyManager(ILockingManager lockingManager) : base(GetProperties(lockingManager))
{
}

private static DavProperty<DiskStoreCollection>[] GetProperties(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) => new DavProperty<DiskStoreCollection>[]
private static DavProperty<DiskStoreCollection>[] GetProperties(ILockingManager lockingManager) => new DavProperty<DiskStoreCollection>[]
{
// RFC-2518 properties
new DavCreationDate<DiskStoreCollection>(httpContextAccessor)
new DavCreationDate<DiskStoreCollection>()
{
Getter = collection => collection.DirectoryInfo.CreationTimeUtc,
Setter = (collection, value) =>
Expand Down
6 changes: 3 additions & 3 deletions NWebDav.Server/Stores/DiskStoreItemPropertyManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace NWebDav.Server.Stores;

public class DiskStoreItemPropertyManager : PropertyManager<DiskStoreItem>
{
public DiskStoreItemPropertyManager(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) : base(GetProperties(httpContextAccessor, lockingManager))
public DiskStoreItemPropertyManager(ILockingManager lockingManager) : base(GetProperties(lockingManager))
{
}

private static DavProperty<DiskStoreItem>[] GetProperties(IHttpContextAccessor httpContextAccessor, ILockingManager lockingManager) => new DavProperty<DiskStoreItem>[]
private static DavProperty<DiskStoreItem>[] GetProperties(ILockingManager lockingManager) => new DavProperty<DiskStoreItem>[]
{
// RFC-2518 properties
new DavCreationDate<DiskStoreItem>(httpContextAccessor)
new DavCreationDate<DiskStoreItem>()
{
Getter = item => item.FileInfo.CreationTimeUtc,
Setter = (item, value) =>
Expand Down

0 comments on commit 6876497

Please sign in to comment.