自定义功能和相关技术

本篇按照如下结构翻译

自定义功能

  • 自定义数据仓库

相关技术

  • 架构和总体概念

自定义数据仓库

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. K-means 和 EM 比较

    回顾 前几篇对 k-means 有过理解和写了一版伪代码, 因为思想比较非常朴素, 就是初始化几个中心点, 然后通过计算距离的方式, "物以类聚", 不断迭代中心点, 最后收敛, ...

  2. 【IDE_IntelliJ IDEA】idea中设置类和方法的注释模板

    参考博文:idea生成类注释和方法注释的正确方法

  3. Linux 信号量之Posix基于内存的信号量

    信号量(semaphore),也和互斥锁一样提供了线程间或者进程间的同步功能. 信号量有三种: Posix有名字的信号量 Posix基于内存的信号量 System V信号量 信号量比互斥锁高级,互斥锁 ...

  4. Delphi-基础

    一.Delphi 安装 1.1.快速启动程序,去掉加载开始欢迎页.在快捷方式--目标中添加路径 -pDelphi之后加 -np(例如,rcadero\Studio\20.p\bin\bds.exe&q ...

  5. jmeter压测学习8-压测带token的接口

    前言 工作中我们需要压测的接口大部分都是需要先登陆后,带着token的接口(或者带着cookies),我们可以先登陆获取token再关联到下个接口. 比如我现在要压测一个修改用户的个人信息接口,每个用 ...

  6. centos7 下安装rpm的mysql 5.7

    在centos7下安装mysql5.7 一:下载mysql 去官网上去下载:这里我下载的二进制格式的 https://dev.mysql.com/downloads/mysql/ 去下载对应平台的my ...

  7. SQL-on-Hadoop 技术

    SQL-on-Hadoop 技术 备注 Apache Hive Cloudera Impala Facebook Presto Apache Drill Spark SQL Apache Phoeni ...

  8. selenium 2定位方式实例

    #########百度输入框的定位方式########## #通过id方式定位 browser.find_element_by_id("kw").send_keys("s ...

  9. 论文阅读笔记六十六:Wide Activation for Efficient and Accurate Image Super-Resolution(CVPR2018)

    论文原址:https://arxiv.org/abs/1808.08718 代码:https://github.com/JiahuiYu/wdsr_ntire2018 摘要 本文证明在SISR中在Re ...

  10. ProceedingJoinPoint获取实现类接口上的注解

    使用aspectj处理拦截aop,需要获取实现类接口上的注解 public Object around(ProceedingJoinPoint pjp) throws Throwable{ long ...