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

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;
        }
    }
});

 

 

 

posted @ 2019-07-13 22:41  wall-ee  阅读(666)  评论(1编辑  收藏  举报