mirror of https://github.com/abpframework/abp.git
csharpabpc-sharpframeworkblazoraspnet-coredotnet-coreaspnetcorearchitecturesaasdomain-driven-designangularmulti-tenancy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
2.7 KiB
90 lines
2.7 KiB
using System;
|
|
using System.Collections.Concurrent;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.Extensions.Primitives;
|
|
using Volo.Abp.DependencyInjection;
|
|
|
|
namespace Volo.Abp.VirtualFileSystem;
|
|
|
|
//TODO: Work with directory & wildcard watches!
|
|
//TODO: Work with dictionaries!
|
|
|
|
/// <remarks>
|
|
/// Current implementation only supports file watch.
|
|
/// Does not support directory or wildcard watches.
|
|
/// </remarks>
|
|
public class DynamicFileProvider : DictionaryBasedFileProvider, IDynamicFileProvider, ISingletonDependency
|
|
{
|
|
protected override IDictionary<string, IFileInfo> Files => DynamicFiles;
|
|
|
|
protected ConcurrentDictionary<string, IFileInfo> DynamicFiles { get; }
|
|
|
|
protected ConcurrentDictionary<string, ChangeTokenInfo> FilePathTokenLookup { get; }
|
|
|
|
public DynamicFileProvider()
|
|
{
|
|
FilePathTokenLookup = new ConcurrentDictionary<string, ChangeTokenInfo>(StringComparer.OrdinalIgnoreCase); ;
|
|
DynamicFiles = new ConcurrentDictionary<string, IFileInfo>();
|
|
}
|
|
|
|
public virtual void AddOrUpdate(IFileInfo fileInfo)
|
|
{
|
|
var filePath = fileInfo.GetVirtualOrPhysicalPathOrNull();
|
|
DynamicFiles.AddOrUpdate(filePath!, fileInfo, (key, value) => fileInfo);
|
|
ReportChange(filePath!);
|
|
}
|
|
|
|
public virtual bool Delete(string filePath)
|
|
{
|
|
if (!DynamicFiles.TryRemove(filePath, out _))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
ReportChange(filePath);
|
|
return true;
|
|
}
|
|
|
|
public override IChangeToken Watch(string filter)
|
|
{
|
|
return GetOrAddChangeToken(filter);
|
|
}
|
|
|
|
private IChangeToken GetOrAddChangeToken(string filePath)
|
|
{
|
|
if (!FilePathTokenLookup.TryGetValue(filePath, out var tokenInfo))
|
|
{
|
|
var cancellationTokenSource = new CancellationTokenSource();
|
|
var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
|
|
tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken);
|
|
tokenInfo = FilePathTokenLookup.GetOrAdd(filePath, tokenInfo);
|
|
}
|
|
|
|
return tokenInfo.ChangeToken;
|
|
}
|
|
|
|
protected virtual void ReportChange(string filePath)
|
|
{
|
|
if (FilePathTokenLookup.TryRemove(filePath, out var tokenInfo))
|
|
{
|
|
tokenInfo.TokenSource.Cancel();
|
|
}
|
|
}
|
|
|
|
protected struct ChangeTokenInfo
|
|
{
|
|
public ChangeTokenInfo(
|
|
CancellationTokenSource tokenSource,
|
|
CancellationChangeToken changeToken)
|
|
{
|
|
TokenSource = tokenSource;
|
|
ChangeToken = changeToken;
|
|
}
|
|
|
|
public CancellationTokenSource TokenSource { get; }
|
|
|
|
public CancellationChangeToken ChangeToken { get; }
|
|
}
|
|
}
|
|
|