《深入浅出.NET框架设计与实现》阅读笔记(四)

静态文件系统


通过ASP.NET Core 提供的静态文件模块和静态文件中间件,可以轻松的让应用程序拥有访问静态文件的功能,同时可以基于IFileProvider对象来自定义文件系统,如基于Redis做扩展文件系统

启动静态文件服务

Program.cs 类中,通过WebApplication的UseStaticFiles扩展方法启动。

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseStaticFiles();
app.Run();

默认存储目录(wwwroot)

默认情况下,静态文件存储在项目的wwwroot目录下。
在这里插入图片描述

  • 读取静态文件(以favicon.ico文件为例):https:// localhost:6379/favicon.ico
  • 读取静态文件(以README.md文件为例):https://localhost:6379/css/open-iconic/README.md

增加自定义静态目录文件

  • 调用UseStaticFiles方法时传递StaticFileOptions配置参数。
  • StaticFileOptionsFileProvider为指定的文件夹路径
  • StaticFileOptionsRequestPath为请求路径的前缀
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
//自定义静态文件目录
StaticFileOptions fileOpt = new()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(builder.Environment.ContentRootPath, "MyStaticFiles")),//指定文件夹目录
    RequestPath = "/StaticFiles"//自定义前缀
};
app.UseStaticFiles(fileOpt);
app.Run();
  • 指定了在项目文件目录下的MyStaticFiles文件夹
    在这里插入图片描述
  • 通过路径来获取想要的文件(以用户手册.pdf为例):https://localhost:6379/StaticFiles/用户手册.pdf
    在这里插入图片描述

自定义一个简单的文件系统

在ASP.NET Core中,允许开发人员自定义文件系统,可以利用IFileProvider接口来构建文件系统。

文件信息类(RedisFileInfo)

public class RedisFileInfo : IFileInfo
{
    /// <summary>
    /// 判断目录或文件是否真的存在
    /// </summary>
    public bool Exists { get; set; } = true;
    /// <summary>
    /// 表示是目录还是文件
    /// </summary>
    public bool IsDirectory { get; set; }
    /// <summary>
    /// 文件或目录最后一次修改的时间
    /// </summary>
    public DateTimeOffset LastModified { get; set; }
    /// <summary>
    /// 表示文件内容的字节长度
    /// </summary>
    public long Length => _fileContent.Length;
    /// <summary>
    /// 表示文件或目录的名字
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// 表示文件或目录的物理路径
    /// </summary>
    public string PhysicalPath { get; set; }

    private readonly byte[] _fileContent;
    public Stream CreateReadStream()
    {
        var stream = new MemoryStream(_fileContent);
        stream.Position = 0;
        return stream;
    }

    public RedisFileInfo() { }
    public RedisFileInfo(string name, string content)
    {
        Name = name;
        LastModified = DateTimeOffset.Now;
        _fileContent = Convert.FromBase64String(content);
    }
    public RedisFileInfo(string name,bool isDirectory)
    {
        Name = name;
        LastModified = DateTimeOffset.Now; 
        IsDirectory = isDirectory;
    }

}

文件目录类(EnumerableDirectoryContents)

public class EnumerableDirectoryContents : IDirectoryContents
{
    private readonly IEnumerable<IFileInfo> _entries;
    public bool Exists => true;

    public EnumerableDirectoryContents(IEnumerable<IFileInfo> entries)
    {
        _entries = entries;
    }

    public IEnumerator<IFileInfo> GetEnumerator()
    {
        return _entries.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

Redis配置文件(RedisFileOptions)

public class RedisFileOptions
{
    /// <summary>
    /// 配置Redius连接信息
    /// </summary>
    public string HostAndPort { get; set; }
}

文件系统逻辑处理类(RedisFileProvider)

    /// <summary>
    /// Redis文件解析器,只要用于通过指定的名称从Redis中读取存储的图片内容
    /// </summary>
public class RedisFileProvider : IFileProvider
{
    private readonly RedisFileOptions _options;
    private readonly ConnectionMultiplexer _redis;

    private static string NormalizePath(string path) => path.TrimStart('/').Replace('/', ':');
    /// <summary>
    /// 参数为Ioptions的好处是可以使用Options.Create()方法来直接生成
    /// </summary>
    /// <param name="options"></param>
    public RedisFileProvider(IOptions<RedisFileOptions> options)
    {
        _options = options.Value;
        _redis = ConnectionMultiplexer.Connect(new ConfigurationOptions
        {
            EndPoints = { _options.HostAndPort }
        });
    }

    /// <summary>
    /// 获得指定的目录
    /// 
    /// 通过
    /// </summary>
    /// <param name="subpath"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public IDirectoryContents GetDirectoryContents(string subpath)
    {
        
        var db = _redis.GetDatabase();
        var server = _redis.GetServer(_options.HostAndPort);
        var list = new List<IFileInfo>();
        subpath = NormalizePath(subpath);
        foreach (var key in server.Keys(0, $"{subpath}*"))
        {
            var k = "";
            if (subpath != "") k = key.ToString().Replace(subpath, "").Split(":")[0];
            else k = key.ToString().Split(":")[0];
            if (list.Find(f => f.Name == k) == null)
            {
                //判断是否存在.
                if (k.IndexOf('.', StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    list.Add(new RedisFileInfo(k, db.StringGet(k)));
                }
                else
                {
                    list.Add(new RedisFileInfo(k, true));
                }
            }
        }
        if (list.Count == 0)
        {
            return NotFoundDirectoryContents.Singleton;
        }
        return new EnumerableDirectoryContents(list);
    }
    /// <summary>
    /// 得到指定目录或文件的IFileInfo对象
    /// 通过subpath参数值再Redis客户端读取文件信息。
    /// </summary>
    /// <param name="subpath"></param>
    /// <returns></returns>
    /// <exception cref="NotImplementedException"></exception>
    public IFileInfo GetFileInfo(string subpath)
    {
        subpath = NormalizePath(subpath);
        var db = _redis.GetDatabase();
        var redisValue = db.StringGet(subpath);
        return !redisValue.HasValue ? new NotFoundFileInfo(subpath) : new RedisFileInfo(subpath, redisValue.ToString());
    }

    public IChangeToken Watch(string filter)
    {
        throw new NotImplementedException();
    }
}

注入服务

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
//使用自定义文件系统
StaticFileOptions fileOpt = new()
{
    FileProvider = new RedisFileProvider(Options.Create(new RedisFileOptions
    {
        HostAndPort = "localhost:6379",
    }))
};
app.UseStaticFiles(fileOpt);
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

baobao熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值