1 Star 5 Fork 4

pojianbing / LazyFileServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Lazy FileServer

非常简单的文件服务,仅实现上传然后生成url。

码云地址 Github地址

服务端

服务端提供统一接口,以便各子应用统一上传文件。

1. 安装

Install-Package Lazy.FileServer.Server -Version 1.0.3

2. 配置

"FileServer": {
  "LocalBase": "C:\\upload",
  "HttpBase": "http://localhost",
  /*
  * 文件路径计算方式,分为Date和Hash
  * Date:
  *   格式: appName/year/month/day/上传文件名
  *   重复文件处理:  重复文件追加序号, 例如上传a.txt, 存在重复,则编号为a.1.txt。 再次上传则为a.2.txt
  *   示例: http://localhost/spider/2022/03/03/c220210105154619.348.jpg
  *
  * Hash:
  *   格式: appName/hash前两位/hash3到4位/hash.扩展名
  *   重复文件处理: 重复hash会覆盖
  *   示例: http://localhost/spider/zq/I9/zqI98OULV8j60XbNSTTxQg==.jpg
  */
  "FilePathCalculatorType": "hash",
  // 应用
  "Apps": [
    {
      "AppId": "1",
      "AppName": "spider",
      "AppKey": "123456"
    }
  ]
}
builder.Services.AddLazyFileServer(builder.Configuration);
app.UseLazyFileServer("/");

经过简单的配置,一个上传服务已经搭建好了。本例中通过http://localhost:5001/,header设置appid,appkey即可上传。

3.自定义路径计算方式

  • 定义IFilePathCalculator实现类
public class CustomFilePathCalculator : IFilePathCalculator
{
    public string Name
    {
        get
        {
            return "custom";
        }
    }

    /// <summary>
    /// 直接返回文件名
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public string Calculate(FilePathCalculatorInput input)
    {
        return input.FileName;
    }
}
  • 注入服务
builder.Services.AddLazyFileServer(builder.Configuration).AddFilePathCalculator<CustomFilePathCalculator>();
  • 修改配置
"FilePathCalculatorType": "custom"

4.自定义应用查找器

默认从AppSetting查找.

  • 定义CustomAppFinder实现类
public class CustomAppFinder : IAppFinder
{
    private static List<AppInfo> Apps = new List<AppInfo>()
    {
        new AppInfo{ AppId = "1", AppKey = "654321", AppName = "spider" }
    };

    public Task<AppInfo> FindAsync(string appid)
    {
        return Task.FromResult(Apps.FirstOrDefault(e => e.AppId == appid));
    }
}
  • 替换默认服务
builder.Services.AddLazyFileServer(builder.Configuration).ReplaceAppFinder<CustomAppFinder>();

客户端

前端应用理论上可以直接调用服务的上传接口,但这样会将appid,AppKey裸露在外界。因此需要各应用包裹下,提供一个上传端点。

1. 安装

Install-Package Lazy.FileServer.Client -Version 1.0.3

2. 示例

using Microsoft.AspNetCore.Mvc;

namespace Lazy.FileServer.Client.WebApi.Host.Controllers
{
    [ApiController]
    public class FileController : ControllerBase
    {
        private readonly ILogger<FileController> _logger;

        private IHttpContextAccessor _httpContextAccessor;

        public FileController(ILogger<FileController> logger, IHttpContextAccessor httpContextAccessor)
        {
            _logger = logger;
            _httpContextAccessor = httpContextAccessor;
        }

        [HttpPost()]
        [Route("Upload")]
        public async Task<IEnumerable<string>> UploadAsync()
        {
            var client = new FileServerClient("http://localhost:5001", "1", "123456");
            return await client.UploadAsync(_httpContextAccessor.HttpContext);
        }
    }
}

空文件

简介

非常简单的文件服务,仅实现上传然后生成url。 展开 收起
C#
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C#
1
https://gitee.com/pojianbing/lazy-fileserver.git
git@gitee.com:pojianbing/lazy-fileserver.git
pojianbing
lazy-fileserver
LazyFileServer
master

搜索帮助