private async Task WriteFileAsync(HttpContext context, FileAuthorizeResult result, FileInfo fileInfo) { var response = context.Response; var sendFile = response.HttpContext.Features.Get <IHttpSendFileFeature>(); if (sendFile != null) { await sendFile.SendFileAsync(fileInfo.FullName, 0L, null, default(CancellationToken)); return; } using (var fileStream = new FileStream( fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, BufferSize, FileOptions.Asynchronous | FileOptions.SequentialScan)) { try { await StreamCopyOperation.CopyToAsync(fileStream, context.Response.Body, count : null, bufferSize : BufferSize, cancel : context.RequestAborted); } catch (OperationCanceledException) { // Don't throw this exception, it's most likely caused by the client disconnecting. // However, if it was cancelled for any other reason we need to prevent empty responses. context.Abort(); } } }
private void SetResponseHeaders(HttpContext context, FileAuthorizeResult result, FileInfo fileInfo) { var response = context.Response; response.ContentType = GetContentType(fileInfo); SetContentDispositionHeader(context, result); var heders = response.GetTypedHeaders(); heders.LastModified = fileInfo.LastWriteTimeUtc; }
private void SetContentDispositionHeader(HttpContext context, FileAuthorizeResult result) { if (!string.IsNullOrEmpty(result.FileDownloadName)) { // From RFC 2183, Sec. 2.3: // The sender may want to suggest a filename to be used if the entity is // detached and stored in a separate file. If the receiving MUA writes // the entity to a file, the suggested filename should be used as a // basis for the actual filename, where possible. var contentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = result.FileDownloadName }; context.Response.Headers["Content-Disposition"] = contentDisposition.ToString(); } }
public async Task <FileAuthorizeResult> AuthorizeAsync(HttpContext context, string path) { var handlerScheme = GetHandlerScheme(path); if (handlerScheme == null || !Provider.Exist(handlerScheme)) { _logger.LogInformation($"request handler scheme is not found. request path is: {path}"); return(FileAuthorizeResult.Fail()); } var handlerType = Provider.GetHandlerType(handlerScheme); if (!(context.RequestServices.GetRequiredService(handlerType) is IFileAuthorizeHandler handler)) { throw new Exception($"the required file authorization handler of '{handlerScheme}' is not found "); } // start with slash var requestFilePath = GetRequestFileUri(path, handlerScheme); return(await handler.AuthorizeAsync(context, requestFilePath)); }
public Task <FileAuthorizeResult> AuthorizeAsync(HttpContext context, string path) { return(Task.FromResult(FileAuthorizeResult.Success(GetRelativeFilePath(path), GetDownloadFileName(path)))); }