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.
iot/projects/Infrastructure/UI/FileConfigureOptions.cs

65 lines
2.7 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
namespace Infrastructure.UI
{
public class FileConfigureOptions : IPostConfigureOptions<StaticFileOptions>
{
private readonly List<string> _assemblies;
private List<IFileProvider> _providers = new List<IFileProvider>();
public FileConfigureOptions(IWebHostEnvironment environment)
{
Environment = environment;
}
public FileConfigureOptions(IWebHostEnvironment environment, List<string> assemblies) : this(environment)
{
this._assemblies = assemblies;
}
public IWebHostEnvironment Environment { get; }
public void PostConfigure(string name, StaticFileOptions options)
{
name = name ?? throw new ArgumentNullException(nameof(name));
options = options ?? throw new ArgumentNullException(nameof(options));
options.ServeUnknownFileTypes = true;
var contentTypeProvider = new FileExtensionContentTypeProvider();
contentTypeProvider.Mappings.Add(".apk", "application/vnd.android.package-archive");
options.ContentTypeProvider = contentTypeProvider;
if (options.FileProvider == null && Environment.WebRootFileProvider == null)
{
#pragma warning disable CA1303 // 请不要将文本作为本地化参数传递
throw new InvalidOperationException("Missing FileProvider.");
#pragma warning restore CA1303 // 请不要将文本作为本地化参数传递
}
if (this._providers == null)
{
this._providers = new List<IFileProvider>();
}
this._providers.Add(new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")));
this._providers.Add(new ManifestEmbeddedFileProvider(GetType().Assembly, "wwwroot"));
if (_assemblies != null && _assemblies.Count > 0)
{
var list = AppDomain.CurrentDomain.GetAssemblies().Where(o => !o.FullName.StartsWith("System", StringComparison.Ordinal) && !o.FullName.StartsWith("Microsoft", StringComparison.Ordinal));
foreach (var item in list)
{
if (this._assemblies.Contains(item.GetName().Name))
{
this._providers.Add(new ManifestEmbeddedFileProvider(item, "wwwroot"));
}
}
}
options.FileProvider = new CompositeFileProvider(this._providers.ToArray());
}
}
}