tusdotnet-----一个tus文件上传协议的实现之事件

本章接上篇来继续翻译Tusdotnet的文档,按照如下结构来翻译:

事件

  • OnAuthorize
  • OnFileComplete
  • OnBeforeCreate
  • OnCreateComplete
  • OnBeforeDelete
  • OnDeleteComplete

OnAuthorize事件

一旦一个请求被确定为一个tus请求,OnAuthorize事件是第一个触发的事件。此事件允许为给定的意图授权请求。

在传递给回调函数的OnAuthorizeContext上调用FailRequest将使用提供的http状态代码和状态消息拒绝请求。

  1. app.UseTus(httpContext => new DefaultTusConfiguration
  2. {
  3. UrlPath = "/files",
  4. Store = new TusDiskStore(@"C:\tusfiles\"),
  5. Events = new Events
  6. {
  7. OnAuthorizeAsync = eventContext =>
  8. {
  9. if (!eventContext.HttpContext.User.Identity.IsAuthenticated)
  10. {
  11. eventContext.FailRequest(HttpStatusCode.Unauthorized);
  12. return Task.CompletedTask;
  13. }
  14.  
  15. // Do other verification on the user; claims, roles, etc. In this case, check the username.
  16. if (eventContext.HttpContext.User.Identity.Name != "test")
  17. {
  18. eventContext.FailRequest(HttpStatusCode.Forbidden, "'test' is the only allowed user");
  19. return Task.CompletedTask;
  20. }
  21.  
  22. // Verify different things depending on the intent of the request.
  23. // E.g.:
  24. // Does the file about to be written belong to this user?
  25. // Is the current user allowed to create new files or have they reached their quota?
  26. // etc etc
  27. switch (ctx.Intent) {
  28. case IntentType.CreateFile:
  29. break;
  30. case IntentType.ConcatenateFiles:
  31. break;
  32. case IntentType.WriteFile:
  33. break;
  34. case IntentType.DeleteFile:
  35. break;
  36. case IntentType.GetFileInfo:
  37. break;
  38. case IntentType.GetOptions:
  39. break;
  40. default:
  41. break;
  42. }
  43.  
  44. return Task.CompletedTask;
  45. }
  46. }
  47. });

OnFileComplete事件

Tusdotnet允许在文件完成后使用OnFileCompleteAsync回调来处理文件。

  1. app.UseTus(request => new DefaultTusConfiguration
  2. {
  3. Store = new TusDiskStore(@"C:\tusfiles\"),
  4. UrlPath = "/files",
  5. Events = new Events
  6. {
  7. OnFileCompleteAsync = async ctx =>
  8. {
  9. // ctx.FileId is the id of the file that was uploaded.
  10. // ctx.Store is the data store that was used (in this case an instance of the TusDiskStore)
  11.  
  12. // A normal use case here would be to read the file and do some processing on it.
  13. var file = await ((ITusReadableStore)ctx.Store).GetFileAsync(ctx.FileId, ctx.CancellationToken);
  14. var result = await DoSomeProcessing(file, ctx.CancellationToken);
  15.  
  16. if (!result.Success)
  17. {
  18. throw new MyProcessingException("Something went wrong during processing");
  19. }
  20. }
  21. }
  22. });
  23. ``

OnBeforeCreate事件

OnBeforeCreate事件在创建文件之前触发。

在传递给回调函数的BeforeCreateContext上调用FailRequest将使用400 Bad Request状态码来拒绝请求。多次调用FailRequest将连接错误消息。

  1. app.UseTus(context => new DefaultTusConfiguration
  2. {
  3. UrlPath = "/files",
  4. Store = new TusDiskStore(@"C:\tusfiles\"),
  5. Events = new Events
  6. {
  7. OnBeforeCreateAsync = ctx =>
  8. {
  9. if (!ctx.Metadata.ContainsKey("name"))
  10. {
  11. ctx.FailRequest("name metadata must be specified. ");
  12. }
  13.  
  14. if (!ctx.Metadata.ContainsKey("contentType"))
  15. {
  16. ctx.FailRequest("contentType metadata must be specified. ");
  17. }
  18.  
  19. return Task.CompletedTask;
  20. }
  21. });

OnCreateComplete事件

OnCreateComplete事件会在文件被创建后触发

  1. app.UseTus(context => new DefaultTusConfiguration
  2. {
  3. UrlPath = "/files",
  4. Store = new TusDiskStore(@"C:\tusfiles\"),
  5. Events = new Events
  6. {
  7. OnCreateCompleteAsync = ctx =>
  8. {
  9. logger.LogInformation($"Created file {ctx.FileId} using {ctx.Store.GetType().FullName}");
  10. return Task.CompletedTask;
  11. }
  12. }
  13. });

OnBeforeDelete事件

OnBeforeDelete事件会在文件正好被删除之前触发。

在传递给回调函数的BeforeDeleteContext参数上调用FailRequest会使用400 Bad Request状态码来拒绝请求。多次调用FailRequest会将错误连接起来。

  1. app.UseTus(context => new DefaultTusConfiguration
  2. {
  3. UrlPath = "/files",
  4. Store = new TusDiskStore(@"C:\tusfiles\"),
  5. Events = new Events
  6. {
  7. OnBeforeDeleteAsync = ctx =>
  8. {
  9. if(!SomeBusinessLogic())
  10. {
  11. ctx.FailRequest($"Cannot delete {ctx.FileId} due to business logic");
  12. }
  13.  
  14. return Task.CompletedTask;
  15. }
  16. }
  17. });

OnDeleteComplete事件

OnDeleteComplete会在文件正好被删除之后触发。

  1. app.UseTus(context => new DefaultTusConfiguration
  2. {
  3. UrlPath = "/files",
  4. Store = new TusDiskStore(@"C:\tusfiles\"),
  5. Events = new Events
  6. {
  7. OnDeleteCompleteAsync = ctx =>
  8. {
  9. logger.LogInformation($"Deleted file {ctx.FileId} using {ctx.Store.GetType().FullName}");
  10. return Task.CompletedTask;
  11. }
  12. }
  13. });

 

【翻译】Tusdotnet中文文档(2)事件的更多相关文章

  1. 【翻译】Tusdotnet中文文档(3)自定义功能和相关技术

    自定义功能和相关技术 本篇按照如下结构翻译 自定义功能 自定义数据仓库 相关技术 架构和总体概念 自定义数据仓库 tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目 ...

  2. 【翻译】Tusdotnet中文文档(1)配置和用法

    TUSDOTNET Tusdotnet是tus协议的一个dotnet实现.tus协议是用来规范文件上传的整个过程,tus基于http协议,规定了一些上传过程中的头部(headers)和对上传过程的描述 ...

  3. ORCHARD中文文档(翻译)

    众所周知,Orchard是.net领域最好的开源CMS之一,他使用了微软最先进的技术,有一群先进理念的支持者,但是,所有的事情在国内总得加个但是,Orchard也不例外,中文资料相对比较少,官网提供的 ...

  4. Knockout中文开发指南(完整版API中文文档) 目录索引

    a, .tree li > span { padding: 4pt; border-radius: 4px; } .tree li a { color:#46cfb0; text-decorat ...

  5. Spring中文文档

    前一段时间翻译了Jetty的一部分文档,感觉对阅读英文没有大的提高(*^-^*),毕竟Jetty的受众面还是比较小的,而且翻译过程中发现Jetty的文档写的不是很好,所以呢翻译的兴趣慢慢就不大了,只能 ...

  6. hammer.js中文文档

    转自:http://www.uedsc.com/hammerjs-api.html HammerJS是一个优秀的.轻量级的触屏设备手势库,现在已经更新到2.04版本,跟1.0版本有点天壤地别了,毕竟改 ...

  7. Django 1.10中文文档-第一个应用Part2-模型和管理站点

    本教程继续Part1.我们将设置数据库,创建您的第一个模型,并快速介绍Django的自动生成的管理网站. 数据库设置 现在,编辑mysite/settings.py.它是一个用模块级别变量表示Djan ...

  8. phantomjs 中文文档

    phantomjs 中文文档 转载 入门教程:转载 http://www.cnblogs.com/front-Thinking/p/4321720.html 1.介绍 简介   PhantomJS是一 ...

  9. App.js实现使用js开发app的应用,此文是中文文档

    在阅读前,在此说明下,本人英文一直不好,所以该文档是借助翻译工具翻译的,阅读起来可能有点不好,请各位谅解,哪位大神有标准的中文文档请分享下 Github下载地址:https://github.com/ ...

随机推荐

  1. Windows 下 pycharm 创建Django 项目【用虚拟环境的解释器】

    1.  背景 我在 Windows 下的 pycharm  直接创建 全新 Django  项目 会  pip 和其他报错 ,暂时解决不了,另外后续的多个项目只需要一套python 环境, 所以可以 ...

  2. 安装教程-Xshell 5 远程连接工具的安装

    Xshell 5 远程连接工具的安装 1.实验描述 物理机中安装 Xshell 5 ,为实现 Linux 等操作系统提供远程连接的环境. 2.实验环境 物理机系统:Windows 10 企业版 3.实 ...

  3. Mysql优化之6年工作经验总结

    我们究竟应该如何对MySQL数据库进行优化?下面我就从MySQL对硬件的选择.MySQL的安装.my.cnf的优化.MySQL如何进行架构设计及数据切分等方面来说明这个问题.   服务器物理硬件的优化 ...

  4. conan使用(二)--创建私有仓库

    前面我们已经能够使用conan来从公共服务器上拉取C/C++包来集成进我的工程中,但是在实际开发中,我们可能需要自己封装或使用非公开的库,那么自己搭建一个私服是个很现实的需求. 搭建conan私服有几 ...

  5. 外网穿透-natapp安装配置(windows)

    natapp官网 natapp服务器更新:全面支持HTTPS协议以及本地SSL证书,支持WSS协议.同时支持HTTP/2 WEB协议,支持微信小程序本地开发.全面自动支持泛子域名与访客真实IP地址. ...

  6. 1.python进行if条件相等时候的条件

    在我们进行 if == 判断的时候!其中判断的条件: 1:其值是不是一样 3:其类型是否是一样 ###二者少了任何一个都不可以 >>> pwd = 23>>> cc ...

  7. csp 201709-2 优先队列模拟

    数据规模: 用优先队列对各个事件的发生先后记录即可: #include<iostream> #include<queue> using namespace std; ]; st ...

  8. pip安装指定版本的程序的命令

    pip安装指定版本的程序的命令 pip install -i https://pypi.douban.com/simple/ django==1.10.3 或者 pip install django= ...

  9. .net使用IIdentity和IPrincipal实现自定义身份及权限认证【转】

    1,通过继承BasePage页实现角色权限控制 context.User中保存的信息就是相关的角色与权限信息.Context.User类型为System.Security.Principal.IPri ...

  10. 【Mybatis】多个参数如何写xml和mapper

    1:#{0},#{1}  不写parameterType 2:注解 @Param("id")String id 3:Map   parameterType="hashma ...