Пример #1
0
        public override Task ExecuteResultAsync(ActionContext context)
        {
            var logger = context.HttpContext.RequestServices.GetRequiredService <ILoggerFactory>().CreateLogger <StaticVirtualFileResult>();

            var fileContext = new StaticFileContext(context.HttpContext, _contentTypeProvider, logger, _fileInfo, _contentType);

            if (!fileContext.ValidateMethod())
            {
                LoggerExtensions.LogRequestMethodNotSupported(logger, context.HttpContext.Request.Method);
            }
            else if (!fileContext.LookupContentType())
            {
                LoggerExtensions.LogFileTypeNotSupported(logger, fileContext.SubPath);
            }
            else if (!fileContext.LookupFileInfo())
            {
                LoggerExtensions.LogFileNotFound(logger, fileContext.SubPath);
            }
            else
            {
                // If we get here, we can try to serve the file
                fileContext.ComprehendRequestHeaders();

                switch (fileContext.GetPreconditionState())
                {
                case StaticFileContext.PreconditionState.Unspecified:
                case StaticFileContext.PreconditionState.ShouldProcess:
                    if (fileContext.IsHeadMethod)
                    {
                        return(fileContext.SendStatusAsync(Constants.Status200Ok));
                    }
                    if (fileContext.IsRangeRequest)
                    {
                        return(fileContext.SendRangeAsync());
                    }

                    LoggerExtensions.LogFileServed(logger, fileContext.SubPath, fileContext.PhysicalPath);
                    return(fileContext.SendAsync());

                case StaticFileContext.PreconditionState.NotModified:
                    LoggerExtensions.LogPathNotModified(logger, fileContext.SubPath);
                    return(fileContext.SendStatusAsync(Constants.Status304NotModified));

                case StaticFileContext.PreconditionState.PreconditionFailed:
                    LoggerExtensions.LogPreconditionFailed(logger, fileContext.SubPath);
                    return(fileContext.SendStatusAsync(Constants.Status412PreconditionFailed));

                default:
                    var exception = new NotImplementedException(fileContext.GetPreconditionState().ToString());
                    Debug.Fail(exception.ToString());
                    throw exception;
                }
            }

            return(Task.FromResult(0));
        }
        public override Task Invoke(IOwinContext context)
        {
            var path        = context.Request.Path.ToString();
            var fileContext = new StaticFileContext(context);

            if (fileContext.ValidateMethod())
            {
                var fileInfo = FindFile(path);
                if (fileInfo != null)
                {
                    var contentType = FindContentType(path);
                    fileContext.SetPayload(fileInfo, contentType);
                    fileContext.ComprehendRequestHeaders();

                    switch (fileContext.GetPreconditionState())
                    {
                    case StaticFileContext.PreconditionState.Unspecified:
                    case StaticFileContext.PreconditionState.ShouldProcess:
                        if (fileContext.IsHeadMethod)
                        {
                            return(fileContext.SendStatusAsync(Constants.Status200Ok));
                        }

                        return(fileContext.SendAsync());

                    case StaticFileContext.PreconditionState.NotModified:
                        return(fileContext.SendStatusAsync(Constants.Status304NotModified));

                    case StaticFileContext.PreconditionState.PreconditionFailed:
                        return(fileContext.SendStatusAsync(Constants.Status412PreconditionFailed));

                    default:
                        throw new NotImplementedException(fileContext.GetPreconditionState().ToString());
                    }
                }
            }

            return(Next.Invoke(context));
        }