自定义功能和相关技术

本篇按照如下结构翻译

自定义功能

  • 自定义数据仓库

相关技术

  • 架构和总体概念

自定义数据仓库

tusdotnet附带一个存储库TusDiskStore,它将文件保存在磁盘上的一个目录中。你可以通过实现以下一个或多个接口(在“接口”这一节中列出来的接口)来实现自己的存储。tusdotnet将自动处理请求并根据用于请求的存储所实现的接口将信息添加到Tus-Extension头部

请注意有一些方法会在请求执行期间多次调用,这取决于仓储正确的缓存数据。

要实现的最常见接口是ITusStore、ITusCreationStore和ITusReadableStore。这将允许仓储(store)创建和上传文件,并读取文件以进行处理或下载。

接口

以下涉及到的名词请查看我的关于tus协议的翻译。

ITusStore

必须实现的接口,在Tus-Extension中的值:<none>

这是tus核心协议的接口,自定义的仓储必须实现。

http://tus.io/protocols/resumable-upload.html#core-protocol或请查看我翻译的tus协议的相关内容。

public interface ITusStore
{
/// <summary>
/// 使用提供的流将数据写入文件
/// 如果流的长度超过了上传文件的长度,必须抛出<exception cref="TusStoreException"></exception>异常/// </summary>
/// <param name="fileId">要写入的文件Id</param>
/// <param name="stream">来自客户端的输入流</param>
/// <param name="cancellationToken">取消令牌</param>
/// <returns>写入的字节长度</returns>
Task<long> AppendDataAsync(string fileId, Stream stream, CancellationToken cancellationToken); /// <summary>
/// 检索一个文件是否存在
/// </summary>
/// <param name="fileId">要检查的文件Id</param>
/// <param name="cancellationToken">取消令牌.</param>
/// <returns></returns>
Task<bool> FileExistAsync(string fileId, CancellationToken cancellationToken); /// <summary>
/// Returns the upload length specified when the file was created or null if Defer-Upload-Lenght was used.
/// </summary>
/// <param name="fileId">The id of the file to check.</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling.</param>
/// <returns>The upload length of the file</returns>
Task<long?> GetUploadLengthAsync(string fileId, CancellationToken cancellationToken); /// <summary>
/// Returns the current size of the file a.k.a. the upload offset.
/// </summary>
/// <param name="fileId">The id of the file to check.</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling.</param>
/// <returns>The size of the current file</returns>
Task<long> GetUploadOffsetAsync(string fileId, CancellationToken cancellationToken);
}

ITusChecksumStore

非必须实现的接口,在Tus-Extension中的值:checksum

支持checksum扩展的接口,用于文件校验和(checksum)的检查。

http://tus.io/protocols/resumable-upload.html#checksum请查看我翻译的tus协议的相关内容。

public interface ITusChecksumStore
{
/// <summary>
/// Returns a collection of hash algorithms that the store supports (e.g. sha1).
/// </summary>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The collection of hash algorithms</returns>
Task<IEnumerable<string>> GetSupportedAlgorithmsAsync(CancellationToken cancellationToken); /// <summary>
/// Verify that the provided checksum matches the file checksum.
/// </summary>
/// <param name="fileId">The id of the file to check</param>
/// <param name="algorithm">The checksum algorithm to use when checking. This algorithm must be supported by the store.</param>
/// <param name="checksum">The checksom to use for verification</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>True if the checksum matches otherwise false</returns>
Task<bool> VerifyChecksumAsync(string fileId, string algorithm, byte[] checksum, CancellationToken cancellationToken);
}

ITusConcatenationStore

非必须实现的接口,Tus-Extension中的值:concatenation

注意:要实现这个接口必须保证ITusCreationStore也被实现。

这个接口添加了对concatenation扩展的支持,这个扩展的作用在于在一个POST请求中将多个文件串联然后得到一个最终文件。

http://tus.io/protocols/resumable-upload.html#concatenation或查看我关于tus协议的相关翻译

public interface ITusConcatenationStore
{
/// <summary>
/// Returns the type of Upload-Concat header that was used when creating the file.
/// Returns null if no Upload-Concat was used.
/// </summary>
/// <param name="fileId">The file to check</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>FileConcatPartial, FileConcatFinal or null</returns>
Task<FileConcat> GetUploadConcatAsync(string fileId, CancellationToken cancellationToken); /// <summary>
/// Create a partial file. This method is called when a Upload-Concat header is present and when its value is "partial".
/// </summary>
/// <param name="uploadLength">The length of the upload in bytes</param>
/// <param name="metadata">The Upload-Metadata request header or null if no header was provided</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The id of the newly created file</returns>
Task<string> CreatePartialFileAsync(long uploadLength, string metadata, CancellationToken cancellationToken); /// <summary>
/// Creates a final file by concatenating multiple files together. This method is called when a Upload-Concat header
/// is present with a "final" value.
/// </summary>
/// <param name="partialFiles">List of file ids to concatenate</param>
/// <param name="metadata">The Upload-Metadata request header or null if no header was provided</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The id of the newly created file</returns>
Task<string> CreateFinalFileAsync(string[] partialFiles, string metadata, CancellationToken cancellationToken);
}

ITusCreationStore

非必须实现的接口,Tus-Extension的值为:creation

这个接口处理tus协议的创建扩展,并用于创建文件引用,稍后可以利用tus核心协议,使用这个创建的文件引用将数据上载。

http://tus.io/protocols/resumable-upload.html#creation 或查看我的tus协议的相关翻译。

public interface ITusCreationStore
{
/// <summary>
/// Create a file upload reference that can later be used to upload data.
/// </summary>
/// <param name="uploadLength">The length of the upload in bytes</param>
/// <param name="metadata">The Upload-Metadata request header or null if no header was provided</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns></returns>
Task<string> CreateFileAsync(long uploadLength, string metadata, CancellationToken cancellationToken); /// <summary>
/// Get the Upload-Metadata header as it was provided to <code>CreateFileAsync</code>.
/// </summary>
/// <param name="fileId">The id of the file to get the header for</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The Upload-Metadata header</returns>
Task<string> GetUploadMetadataAsync(string fileId, CancellationToken cancellationToken);
}

ITusCreationDeferLengthStore

非必须实现的接口,Tus-Extension中的值: creation-defer-length

注意:要实现这个接口必须保证ITusCreationStore接口同时被实现。

creation-defer-length是creation扩展的子扩展,它允许用户在不预先知道上传文件大小的情况下创建文件。

如果实现了这个接口,并且用户选择使用这个特性,那么对CreateFileAsync (ITusCreationStore)和CreatePartialFileAsync (ITusConcatenationStore)的调用将使用-1作为文件长度。

http://tus.io/protocols/resumable-upload.html#upload-defer-length或查看我的tus文档

public interface ITusCreationDeferLengthStore
{
/// <summary>
/// Set the upload length (in bytes) of the provided file.
/// </summary>
/// <param name="fileId">The id of the file to set the upload length for</param>
/// <param name="uploadLength">The length of the upload in bytes</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>Task</returns>
Task SetUploadLengthAsync(string fileId, long uploadLength, CancellationToken cancellationToken);
}

ITusTerminationStore

非必须实现的接口,Tus-Extensions中的值:termination

这个接口支持了tus协议中的termination扩展,用于删除文件。

http://tus.io/protocols/resumable-upload.html#termination或查看我的tus文档

public interface ITusTerminationStore
{
/// <summary>
/// Delete a file from the data store.
/// </summary>
/// <param name="fileId">The id of the file to delete</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>Task</returns>
Task DeleteFileAsync(string fileId, CancellationToken cancellationToken);
}

ITusReadableStore

非必须实现的接口,Tus-Extension中的值:<none>

ITusReadableStore是一个不属于tus规范的简单接口,它用于帮助从数据存储中读取数据,使下载文件或处理上传文件变得更容易。如何使用该接口的示例可以在我之前翻译的tusdotnet文档的下载文件小结中找到。或者查看英文文档:Downloading files

public interface ITusReadableStore
{
/// <summary>
/// Get the file with the specified id.
/// Returns null if the file was not found.
/// </summary>
/// <param name="fileId">The id of the file to get</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The file or null if the file was not found</returns>
Task<ITusFile> GetFileAsync(string fileId, CancellationToken cancellationToken);
}

ITusExpirationStore

非必须实现的接口,Tus-Extension中的值:expiration

这个接口实现了expiration扩展,用于服务端删除那些过了一段时间后未完成上传的文件。过期的文件将通过tusdotnet返回404。服务器仍然可以使用仓储(store)的方法访问文件。

http://tus.io/protocols/resumable-upload.html#expiration

https://github.com/smatsson/tusdotnet/wiki/Removing-expired-incomplete-files 介绍了更多关于如何配置文件清理的信息。

public interface ITusExpirationStore
{
/// <summary>
/// Set the expiry date of the provided file.
/// This method will be called once during creation if absolute expiration is used.
/// This method will be called once per patch request if sliding expiration is used.
/// </summary>
/// <param name="fileId">The id of the file to update the expiry date for</param>
/// <param name="expires">The datetime offset when the file expires</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>Task</returns>
Task SetExpirationAsync(string fileId, DateTimeOffset expires, CancellationToken cancellationToken); /// <summary>
/// Get the expiry date of the provided file (set by <code>SetExpirationAsync</code>).
/// If the datetime offset returned has passed an error will be returned to the client.
/// If no expiry date exist for the file, this method returns null.
/// </summary>
/// <param name="fileId">The id of the file to get the expiry date for</param>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns></returns>
Task<DateTimeOffset?> GetExpirationAsync(string fileId, CancellationToken cancellationToken); /// <summary>
/// Returns a list of ids of incomplete files that have expired.
/// This method can be used to do batch processing of incomplete, expired files before removing them.
/// </summary>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>A list of ids of incomplete files that have expired</returns>
Task<IEnumerable<string>> GetExpiredFilesAsync(CancellationToken cancellationToken); /// <summary>
/// Remove all incomplete files that have expired.
/// </summary>
/// <param name="cancellationToken">Cancellation token to use when cancelling</param>
/// <returns>The number of files that were removed</returns>
Task<int> RemoveExpiredFilesAsync(CancellationToken cancellationToken);
}

架构和总体概念

这部分描述了tusdonet的架构和它的总体概念。

tusdotnet包含了三个主要的部分:

  • Tus的中间件
  • 一个用于配置的对象
  • 一个数据存储库

Tus中间件

这是负责处理所有请求并返回正确响应的OWIN中间件。中间件的功能取决于所使用的数据存储所实现的接口。

要设置中间件,请使用UseTus扩展方法并为其提供配置工厂。使用这种方法,tusdotnet支持为不同的请求(例如不同的用户、租户等)使用不同的配置。

中间件将转发它不能处理的所有请求,例如GET请求或其他缺少Tus-Resumable头的请求。这样,即使客户机不支持tus协议,开发人员仍然可以处理上传。

用于配置的对象

用于配置的对象告诉Tus中间件有哪些选项可用。它目前支持设置要侦听的URL、要使用的数据存储和在请求处理期间运行的自定义回调。

相关信息在这里:Configure tusdotnet 或者你可以查看我翻译的文档。

数据存储库

数据存储是tusdotnet存储数据的地方,也是决定运行tusdotnet的服务器功能的地方。存储库可以实现许多接口,每个接口都赋予系统更多的功能。tusdotnet附带了一个存储库实现TusDiskStore,这是一个简单的存储,将数据保存在服务器磁盘上的文件夹中。支持自定义数据(Custom data stores)存储。

【翻译】Tusdotnet中文文档(3)自定义功能和相关技术的更多相关文章

  1. 【翻译】Tusdotnet中文文档(2)事件

    tusdotnet-----一个tus文件上传协议的实现之事件 本章接上篇来继续翻译Tusdotnet的文档,按照如下结构来翻译: 事件 OnAuthorize OnFileComplete OnBe ...

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

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

  3. Ocelot中文文档-Raft(实验功能不能用于生产环境)

    Ocelot最近整合了Rafty,这是我在去年一直研究的Raft的一个实现. 这个项目实验性非常强,所以在我认为它没问题之前,请不要在生产环境中使用Ocelot的这个功能. Raft是一种分布式一致性 ...

  4. ORCHARD中文文档(翻译)

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

  5. Spring中文文档

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

  6. hammer.js中文文档

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

  7. Core 中文文档

    ASP.NET Core 中文文档 第二章 指南(1)用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序   原文:Your First ASP. ...

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

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

  9. Django 1.10中文文档-执行查询

    Django 1.10中文文档: https://github.com/jhao104/django-chinese-doc 只要创建好 数据模型, Django 会自动为生成一套数据库抽象的API, ...

随机推荐

  1. 一条SELECT查询语句在数据库里执行时都经历了什么

    每天都在跟 mysql 打交道,你知道执行一条简单的 select 语句,都经历了哪些过程吗? 首先,mysql 主要是由 server 层和存储层两部分构成的.server 层主要包括连接器.查询缓 ...

  2. angular6 使用信息提示框toast

    angular6 可以使用的toast插件有好多个,在目前来看ngx-toastr在过去一年时间的使用量和受欢迎程度可以说是一骑绝尘,如下图: 我也就选择了ngx-toastr这个插件,使用步骤如下: ...

  3. mysql-存储过程-触发器-事务---4

    本节所讲内容: 存储过程   触发器 事务 一.存储过程 什么是存储过程 大多数SQL语句都是针对一个或多个表的单条语句.并非所有的操作都怎么简单.经常会有一个完整的操作需要多条才能完成.存储过程(S ...

  4. 201871010105-曹玉中《面向对象程序设计(java)》第十三周学习总结

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

  5. Android开发环境搭建(个人环境非通用)

    1.安装andorid studio 2.连接模拟器,AMD处理器为无法使用AVD manager ,所以连接第三方的Genymotion模拟器,设置中安装Genymotion插件,重启即可(Geny ...

  6. LG3812 「模板」线性基 线性基

    问题描述 LG3812 题解 线性基是一类擅长解决异或问题的数据结构(也不算数据结构吧...就是一种玄学的东西) 对于数列 \(a\) ,它的线性基 \(d\) 为 出现 \(1\) 的最高位在第 \ ...

  7. 学习-velocity

    Velocity是什么?  Velocity是一个基于java的模板引擎(template engine).它允许任何人仅仅简单的使用模板语言(template language)来引用由java代码 ...

  8. 关于对STL容器重载运算符的问题

    注意:下文中vector数组指的是每个数组元素都是一个vector的数组 大部分有序STL容器都重载了小于号,且比较方式是对容器中元素进行字典序比较. 此时如果你写\(bool\ operator&l ...

  9. CF717A Festival Organization(第一类斯特林数,斐波那契数列)

    题目大意:求 $\sum\limits_{n=l}^{r}\dbinom{f_n}{k}\bmod 10^9+7$.其中 $f_n$ 是长度为 $n$ 的 $01$ 序列中,没有连续两个或超过两个 $ ...

  10. 《30天自制操作系统》笔记2 --- 初步了解汇编产生的二进制(Day1)

    nask.exe应该就是nas kit(nas开发工具的意思),由于这个编译器是作者自己写的,所以这种汇编语言应该是作者改造出来的,所以我叫它nas汇编语言. 作者说nask是模仿nasm语法的,关于 ...