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

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

事件

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

OnAuthorize事件

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

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

app.UseTus(httpContext => new DefaultTusConfiguration
{
UrlPath = "/files",
Store = new TusDiskStore(@"C:\tusfiles\"),
Events = new Events
{
OnAuthorizeAsync = eventContext =>
{
if (!eventContext.HttpContext.User.Identity.IsAuthenticated)
{
eventContext.FailRequest(HttpStatusCode.Unauthorized);
return Task.CompletedTask;
} // Do other verification on the user; claims, roles, etc. In this case, check the username.
if (eventContext.HttpContext.User.Identity.Name != "test")
{
eventContext.FailRequest(HttpStatusCode.Forbidden, "'test' is the only allowed user");
return Task.CompletedTask;
} // Verify different things depending on the intent of the request.
// E.g.:
// Does the file about to be written belong to this user?
// Is the current user allowed to create new files or have they reached their quota?
// etc etc
switch (ctx.Intent) {
case IntentType.CreateFile:
break;
case IntentType.ConcatenateFiles:
break;
case IntentType.WriteFile:
break;
case IntentType.DeleteFile:
break;
case IntentType.GetFileInfo:
break;
case IntentType.GetOptions:
break;
default:
break;
} return Task.CompletedTask;
}
}
});

OnFileComplete事件

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

app.UseTus(request => new DefaultTusConfiguration
{
Store = new TusDiskStore(@"C:\tusfiles\"),
UrlPath = "/files",
Events = new Events
{
OnFileCompleteAsync = async ctx =>
{
// ctx.FileId is the id of the file that was uploaded.
// ctx.Store is the data store that was used (in this case an instance of the TusDiskStore) // A normal use case here would be to read the file and do some processing on it.
var file = await ((ITusReadableStore)ctx.Store).GetFileAsync(ctx.FileId, ctx.CancellationToken);
var result = await DoSomeProcessing(file, ctx.CancellationToken); if (!result.Success)
{
throw new MyProcessingException("Something went wrong during processing");
}
}
}
});
``

OnBeforeCreate事件

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

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

app.UseTus(context => new DefaultTusConfiguration
{
UrlPath = "/files",
Store = new TusDiskStore(@"C:\tusfiles\"),
Events = new Events
{
OnBeforeCreateAsync = ctx =>
{
if (!ctx.Metadata.ContainsKey("name"))
{
ctx.FailRequest("name metadata must be specified. ");
} if (!ctx.Metadata.ContainsKey("contentType"))
{
ctx.FailRequest("contentType metadata must be specified. ");
} return Task.CompletedTask;
}
});

OnCreateComplete事件

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

app.UseTus(context => new DefaultTusConfiguration
{
UrlPath = "/files",
Store = new TusDiskStore(@"C:\tusfiles\"),
Events = new Events
{
OnCreateCompleteAsync = ctx =>
{
logger.LogInformation($"Created file {ctx.FileId} using {ctx.Store.GetType().FullName}");
return Task.CompletedTask;
}
}
});

OnBeforeDelete事件

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

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

app.UseTus(context => new DefaultTusConfiguration
{
UrlPath = "/files",
Store = new TusDiskStore(@"C:\tusfiles\"),
Events = new Events
{
OnBeforeDeleteAsync = ctx =>
{
if(!SomeBusinessLogic())
{
ctx.FailRequest($"Cannot delete {ctx.FileId} due to business logic");
} return Task.CompletedTask;
}
}
});

OnDeleteComplete事件

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

app.UseTus(context => new DefaultTusConfiguration
{
UrlPath = "/files",
Store = new TusDiskStore(@"C:\tusfiles\"),
Events = new Events
{
OnDeleteCompleteAsync = ctx =>
{
logger.LogInformation($"Deleted file {ctx.FileId} using {ctx.Store.GetType().FullName}");
return Task.CompletedTask;
}
}
});

 

【翻译】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. JS高阶---原型面试

    测试1: 分析如下 结果如下: 测试2: 测试3: 测试4: .

  2. 第14节_BLE协议ATT层

    下面这个图是BLE协议各层跟医院的各个科室的类比图: 跟医院类比,ATT层就是化验室,通过它可以得到各种检查结果──属性.这些检查结果之间有什么联系,它们组合起来体现了什么,化验室是不知道的,这些得由 ...

  3. 201871010133-赵永军《面向对象程序设计(java)》第十三周学习总结

    201871010133-赵永军<面向对象程序设计(java)>第十三周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...

  4. 201871010135 张玉晶《面向对象程序设计(java)》第十三周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/zyja/p/11918 ...

  5. 02-人脸识别-基于MTCNN,框选人脸区域-detect_face

    (本系列随笔持续更新) 这部分代码是基于参考中的链接,修改后适用于TensorFlow1.6.0版本的代码.由于TensorFlow的频繁更新,所以不一定支持后续新或者就版本,特此说明. 程序的最初版 ...

  6. MySQL数据库的下载与安装

    1.百度:MySQL,点击MySQL官网 2. 进入官网后,点击DOWNLOADS,然后点击Windows 3.点击MySQL Installer 4.下载第二个MSI文佳,点击downloads 5 ...

  7. LG1410 子序列 二分图判定

    问题描述 LG1410 题解 如果\(i<j,a_j \le a_i\),那么他它们不能在一个上升序列中. 于是在\(i,j\)之间建边,看建出来的图是不是二分图即可. \(\mathrm{Co ...

  8. Emu8086三种格式的代码-(顺序,分支,循环)

    这个学期准备考研,于是就没有怎么听别的课,现在临近期末,汇编成了个难题.下面是我学校的实验报告 做一个复习的记录吧,下面的代码都是在Emu8086上运行出来的代码 下面先介绍一下,汇编里面的格式问题 ...

  9. 【oracle】11g服务器安装详细步骤

    以下是百度经验:https://jingyan.baidu.com/article/363872eccfb9266e4aa16f5d.html 1.同时解压 2.setup 3.

  10. 2019-2020 ICPC Asia Hong Kong Regional Contest

    题解: https://files.cnblogs.com/files/clrs97/19HKEditorial-V1.zip Code:(Part) A. Axis of Symmetry #inc ...