Exemplo n.º 1
0
        private async Task InnerSendFileAsync(string path, long offset, long?count, CancellationToken cancellation)
        {
            cancellation.ThrowIfCancellationRequested();

            var fileInfo = new FileInfo(path);

            if (offset < 0 || offset > fileInfo.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
            }
            if (count.HasValue &&
                (count.Value < 0 || count.Value > fileInfo.Length - offset))
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
            }

            int bufferSize = 1024 * 16;

            var fileStream = new FileStream(
                path,
                FileMode.Open,
                FileAccess.Read,
                FileShare.ReadWrite,
                bufferSize: bufferSize,
                options: FileOptions.Asynchronous | FileOptions.SequentialScan);

            using (fileStream)
            {
                fileStream.Seek(offset, SeekOrigin.Begin);
                await StreamCopyOperation.CopyToAsync(fileStream, _proxyStream, count, cancellation);
            }
        }
Exemplo n.º 2
0
        private static async Task SendFileAsync(this HttpResponse response, FileInfo file)
        {
            HttpContext context = response.HttpContext;

            string physicalPath = file.FullName;
            var    length       = file.Length;
            var    sendFile     = context.Features.Get <IHttpSendFileFeature>();

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                await sendFile.SendFileAsync(physicalPath, 0, length, CancellationToken.None);

                return;
            }

            try
            {
                using (var readStream = file.OpenRead())
                {
                    await StreamCopyOperation.CopyToAsync(readStream, response.Body, length, 64 * 1024, context.RequestAborted);
                }
            }
            catch (OperationCanceledException)
            {
                context.Abort();
            }
        }
Exemplo n.º 3
0
        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();
                }
            }
        }
        /// <summary>
        /// Sends the given file using the SendFile extension.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="file">The file.</param>
        /// <param name="offset">The offset in the file.</param>
        /// <param name="count">The number of bytes to send, or null to send the remainder of the file.</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task SendFileAsync(this HttpResponse response, IFileInfo file, long offset, long?count,
                                               CancellationToken cancellationToken = default(CancellationToken))
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }
            CheckRange(offset, count, file.Length);

            if (string.IsNullOrEmpty(file.PhysicalPath))
            {
                using (var fileContent = file.CreateReadStream())
                {
                    if (offset > 0)
                    {
                        fileContent.Seek(offset, SeekOrigin.Begin);
                    }
                    await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, cancellationToken);
                }
            }
            else
            {
                await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken);
            }
        }
Exemplo n.º 5
0
            public async Task SendAsync()
            {
                ApplyResponseHeaders(Constants.Status200Ok);

                string physicalPath = _fileInfo.PhysicalPath;
                var    sendFile     = _context.Features.Get <IHttpSendFileFeature>();

                if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
                {
                    await sendFile.SendFileAsync(physicalPath, 0, _length, _context.RequestAborted);

                    return;
                }

                Stream readStream = _fileInfo.CreateReadStream();

                try
                {
                    await StreamCopyOperation.CopyToAsync(readStream, _response.Body, _length, _context.RequestAborted);
                }
                finally
                {
                    readStream.Dispose();
                }
            }
Exemplo n.º 6
0
        public async Task SendAsync()
        {
            ApplyResponseHeaders(Constants.Status200Ok);
            string physicalPath = _fileInfo.PhysicalPath;
            var    sendFile     = _context.Features.Get <IHttpSendFileFeature>();

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                // We don't need to directly cancel this, if the client disconnects it will fail silently.
                await sendFile.SendFileAsync(physicalPath, 0, _length, CancellationToken.None);

                return;
            }

            try
            {
                using (var readStream = _fileInfo.CreateReadStream())
                {
                    // Larger StreamCopyBufferSize is required because in case of FileStream readStream isn't going to be buffering
                    await StreamCopyOperation.CopyToAsync(readStream, _response.Body, _length, StreamCopyBufferSize, _context.RequestAborted);
                }
            }
            catch (OperationCanceledException ex)
            {
                _logger.LogWriteCancelled(ex);
                // 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();
            }
        }
Exemplo n.º 7
0
        protected static async Task WriteFileAsync(MazeContext context, Stream fileStream, RangeItemHeaderValue range, long rangeLength)
        {
            var outputStream = context.Response.Body;

            using (fileStream)
            {
                try
                {
                    if (range == null)
                    {
                        await StreamCopyOperation.CopyToAsync(fileStream, outputStream, count : null, bufferSize : BufferSize, cancel : context.RequestAborted);
                    }
                    else
                    {
                        fileStream.Seek(range.From.Value, SeekOrigin.Begin);
                        await StreamCopyOperation.CopyToAsync(fileStream, outputStream, rangeLength, BufferSize, 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 static async Task SendFileAsyncCore(HttpResponse response, IFileInfo file, long offset, long?count, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(file.PhysicalPath))
            {
                CheckRange(offset, count, file.Length);
                using var fileContent = file.CreateReadStream();

                var useRequestAborted = !cancellationToken.CanBeCanceled;
                var localCancel       = useRequestAborted ? response.HttpContext.RequestAborted : cancellationToken;

                try
                {
                    localCancel.ThrowIfCancellationRequested();
                    if (offset > 0)
                    {
                        fileContent.Seek(offset, SeekOrigin.Begin);
                    }
                    await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, StreamCopyBufferSize, localCancel);
                }
                catch (OperationCanceledException) when(useRequestAborted)
                {
                }
            }
            else
            {
                await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken);
            }
        }
Exemplo n.º 9
0
        private Task SendFileAsync(string fileName, long offset, long?count, CancellationToken cancel)
        {
            switch (Intercept())
            {
            case InterceptMode.DoingNothing:
            {
                if (_originalSendFileAsyncDelegate != null)
                {
                    return(_originalSendFileAsyncDelegate.Invoke(fileName, offset, count, cancel));
                }

                // TODO: sync errors go faulted task
                var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                fileStream.Seek(offset, SeekOrigin.Begin);
                var copyOperation = new StreamCopyOperation(fileStream, _originalResponseBody, count, cancel);
                return(copyOperation.Start().Finally(fileStream.Close));
            }

            case InterceptMode.CompressingToStorage:
            {
                // TODO: sync errors go faulted task
                var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                fileStream.Seek(offset, SeekOrigin.Begin);
                var copyOperation = new StreamCopyOperation(fileStream, _compressingStream, count, cancel);
                return(copyOperation.Start().Finally(fileStream.Close));
            }

            case InterceptMode.SentFromStorage:
                return(TaskHelpers.Completed());
            }
            throw new NotImplementedException();
        }
Exemplo n.º 10
0
        private async Task SendFileAsync(string filePath, HttpResponse response, long offset, long?count)
        {
            var fileInfo = GetFileInfo(filePath);

            if (offset < 0 || offset > fileInfo.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(offset), offset, string.Empty);
            }

            if (count.HasValue &&
                (count.Value < 0 || count.Value > fileInfo.Length - offset))
            {
                throw new ArgumentOutOfRangeException(nameof(count), count, string.Empty);
            }

            // Copied from SendFileFallback.SendFileAsync
            const int BufferSize = 1024 * 16;

            await using var fileStream = new FileStream(
                            filePath,
                            FileMode.Open,
                            FileAccess.Read,
                            FileShare.ReadWrite,
                            bufferSize: BufferSize,
                            options: FileOptions.Asynchronous | FileOptions.SequentialScan);

            fileStream.Seek(offset, SeekOrigin.Begin);
            await StreamCopyOperation
            .CopyToAsync(fileStream, response.Body, count, BufferSize, CancellationToken.None)
            .ConfigureAwait(true);
        }
Exemplo n.º 11
0
 public Task Invoke(HttpContext context)
 {
     if (IsGetMethod(context.Request.Method) && IsPluginMatchPath(context.Request.Path) && IsSupportContentType(context))
     {
         string filePath = GetAbsolutePath(context.Request.Path);
         if (File.Exists(filePath))
         {
             var file = new PhysicalFileInfo(new FileInfo(filePath));
             context.Response.ContentLength = file.Length;
             context.Response.StatusCode    = 200;
             var sendFileFeature = context.Features.Get <IHttpSendFileFeature>();
             if (sendFileFeature != null)
             {
                 return(sendFileFeature.SendFileAsync(filePath, 0, file.Length, CancellationToken.None));
             }
             using (var readStream = file.CreateReadStream())
             {
                 var task = StreamCopyOperation.CopyToAsync(readStream, context.Response.Body, file.Length, 64 * 1024, context.RequestAborted);
                 task.Wait();
                 return(task);
             }
         }
     }
     return(_next(context));
 }
        // Not safe for overlapped writes.
        private static async Task SendFileAsync(Stream outputStream, string fileName, long offset, long?count,
                                                CancellationToken cancel = default(CancellationToken))
        {
            cancel.ThrowIfCancellationRequested();

            var fileInfo = new FileInfo(fileName);

            CheckRange(offset, count, fileInfo.Length);

            int bufferSize = 1024 * 16;

            var fileStream = new FileStream(
                fileName,
                FileMode.Open,
                FileAccess.Read,
                FileShare.ReadWrite,
                bufferSize: bufferSize,
                options: FileOptions.Asynchronous | FileOptions.SequentialScan);

            using (fileStream)
            {
                fileStream.Seek(offset, SeekOrigin.Begin);
                await StreamCopyOperation.CopyToAsync(fileStream, outputStream, count, cancel);
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// 返回json。
        /// </summary>
        /// <param name="httpResponse">响应。</param>
        /// <param name="bytes">媒体内容。</param>
        /// <param name="cancellationToken">取消。</param>
        /// <returns></returns>
        public static Task WriteImageAsync(this HttpResponse httpResponse, byte[] bytes, CancellationToken cancellationToken = default)
        {
            var stream = new MemoryStream(bytes);

            httpResponse.ContentType = "image/png";

            return(StreamCopyOperation.CopyToAsync(stream, httpResponse.Body, null, bytes.Length, cancellationToken));
        }
Exemplo n.º 14
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            //app.UseDirectoryBrowser();//显示文件夹里的内容列表
            //app.UseDefaultFiles();//放在UseStaticFiles前面



            app.UseStaticFiles();
            //以上在wwwroot中寻找
            //以下在其它目录中寻找
            app.UseStaticFiles(new StaticFileOptions {
                RequestPath  = "/pages",
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "pages"))
            });

            app.MapWhen(context => {
                //当请求不是api开始的时候
                return(!context.Request.Path.Value.StartsWith("/api"));
            }, appBuilder => {
                //var options = new RewriteOptions();
                //options.AddRewrite(".*","/index.html",true);
                //appBuilder.UseRewriter(options);
                //appBuilder.UseStaticFiles();

                #region 断路器的方式
                appBuilder.Run(async c => {
                    var file = env.WebRootFileProvider.GetFileInfo("index.html");
                    c.Response.ContentType = "text/html";

                    using (var fileStream = new FileStream(file.PhysicalPath, FileMode.Open, FileAccess.Read))
                    {
                        await StreamCopyOperation.CopyToAsync(fileStream, c.Response.Body, null, BufferSize, c.RequestAborted);
                    }
                });
                #endregion
            });


            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
Exemplo n.º 15
0
        /// <summary>
        /// Copies the specified file to the target directory.
        /// </summary>
        /// <param name="sourceFileSystem">The source file system.</param>
        /// <param name="sourceFile">The source file.</param>
        /// <param name="targetDirectory">The target directory.</param>
        /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception>
        public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory)
        {
            sourceFileSystem.ThrowIfNull(() => sourceFileSystem);
            sourceFile.ThrowIfNull(() => sourceFile);
            targetDirectory.ThrowIfNull(() => targetDirectory);

            if (!(targetDirectory is LocalDirectoryInfo))
            {
                throw new ArgumentException("The target directory must be of type LocalDirectoryInfo.", "targetDirectory");
            }

            try
            {
                using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile))
                {
                    string targetFilePath = this.CombinePath(targetDirectory.FullName, sourceFile.Name);

                    try
                    {
                        using (FileStream targetStream = File.Create(targetFilePath))
                        {
                            if (sourceFile.Length > 0)
                            {
                                var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 256 * 1024, true);

                                copyOperation.CopyProgressChanged +=
                                    (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e);

                                copyOperation.Execute();
                            }
                        }
                    }

                    catch (IOException)
                    {
                        File.Delete(targetFilePath);

                        throw;
                    }
                }
            }

            catch (UnauthorizedAccessException ex)
            {
                throw new AccessException("The file could not be accessed.", ex);
            }

            catch (SecurityException ex)
            {
                throw new AccessException("The file could not be accessed.", ex);
            }

            catch (IOException ex)
            {
                throw new AccessException("The file could not be accessed.", ex);
            }
        }
        /// <inheritdoc />
        public async Task ExecuteAsync(ActionContext context, LimitedFileStreamResult result)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (result == null)
            {
                throw new ArgumentNullException(nameof(result));
            }

            using (result.FileStream)
            {
                var contentLength = result.FileStream.Length;
                var(range, rangeLength, serveBody) = SetHeadersAndLog(context, result, contentLength, result.EnableRangeProcessing);
                if (!serveBody)
                {
                    return;
                }

                try
                {
                    var cancellationToken = context.HttpContext.RequestAborted;
                    var outputStream      = context.HttpContext.Response.Body;
                    if (range == null)
                    {
                        await StreamCopyOperation.CopyToAsync(
                            result.FileStream,
                            outputStream,
                            contentLength,
                            BufferSize,
                            cancellationToken)
                        .ConfigureAwait(false);
                    }
                    else
                    {
                        result.FileStream.Seek(range.From.Value, SeekOrigin.Begin);
                        await StreamCopyOperation.CopyToAsync(
                            result.FileStream,
                            outputStream,
                            rangeLength,
                            BufferSize,
                            cancellationToken)
                        .ConfigureAwait(false);
                    }
                }
                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.HttpContext.Abort();
                }
            }
        }
Exemplo n.º 17
0
            // When there is only a single range the bytes are sent directly in the body.
            internal async Task SendRangeAsync()
            {
                bool rangeNotSatisfiable = false;

                if (_ranges.Count == 0)
                {
                    rangeNotSatisfiable = true;
                }

                if (rangeNotSatisfiable)
                {
                    // 14.16 Content-Range - A server sending a response with status code 416 (Requested range not satisfiable)
                    // SHOULD include a Content-Range field with a byte-range-resp-spec of "*". The instance-length specifies
                    // the current length of the selected resource.  e.g. */length
                    _responseHeaders.ContentRange = new ContentRangeHeaderValue(_length);
                    ApplyResponseHeaders(Constants.Status416RangeNotSatisfiable);

                    LoggerExtensions.LogRangeNotSatisfiable(_logger, SubPath);
                    return;
                }

                // Multi-range is not supported.
                Debug.Assert(_ranges.Count == 1);

                long start, length;

                _responseHeaders.ContentRange = ComputeContentRange(_ranges[0], out start, out length);
                _response.ContentLength       = length;
                ApplyResponseHeaders(Constants.Status206PartialContent);

                string physicalPath = _fileInfo.PhysicalPath;
                var    sendFile     = _context.Features.Get <IHttpSendFileFeature>();

                if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
                {
                    LoggerExtensions.LogSendingFileRange(_logger, _response.Headers[HeaderNames.ContentRange], physicalPath);
                    await sendFile.SendFileAsync(physicalPath, start, length, _context.RequestAborted);

                    return;
                }

                Stream readStream = _fileInfo.CreateReadStream();

                try
                {
                    readStream.Seek(start, SeekOrigin.Begin); // TODO: What if !CanSeek?
                    LoggerExtensions.LogCopyingFileRange(_logger, _response.Headers[HeaderNames.ContentRange], SubPath);
                    await StreamCopyOperation.CopyToAsync(readStream, _response.Body, length, _context.RequestAborted);
                }
                finally
                {
                    readStream.Dispose();
                }
            }
        public async Task ExecuteAsync(ActionContext context, BufferedFileStreamActionResult result)
        {
            if (!Streams.TryGetValue(result.FileName, out var buffer) || buffer == null)
            {
                buffer = new BufferData(result.StreamId,
                                        new FileStream(result.FileName, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize));
                Streams[result.FileName] = buffer;
            }
            buffer.LastAccess = DateTime.Now;

            var fileLength = buffer.SourceStream.Length;

            var(range, rangeLength, serveBody) = SetHeadersAndLog(
                context,
                result,
                fileLength,
                result.EnableRangeProcessing,
                result.LastModified,
                result.EntityTag);

            if (!serveBody)
            {
                return;
            }

            var outputStream = context.HttpContext.Response.Body;

            try
            {
                if (range == null)
                {
                    await StreamCopyOperation.CopyToAsync(buffer.AccessStream, outputStream, count : null, bufferSize : BufferSize, cancel : context.HttpContext.RequestAborted);
                }
                else
                {
                    buffer.AccessStream.Seek(range.From.Value, SeekOrigin.Begin);
                    await StreamCopyOperation.CopyToAsync(buffer.AccessStream, outputStream, rangeLength, BufferSize, context.HttpContext.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.HttpContext.Abort();
            }
            //using (var sourceStream = buffer.AccessStream.CreateViewStream(range.From ?? 0, range.To ?? fileLength, MemoryMappedFileAccess.Read))
            //{
            //}
        }
Exemplo n.º 19
0
        // When there is only a single range the bytes are sent directly in the body.
        internal async Task SendRangeAsync()
        {
            if (_range == null)
            {
                // 14.16 Content-Range - A server sending a response with status code 416 (Requested range not satisfiable)
                // SHOULD include a Content-Range field with a byte-range-resp-spec of "*". The instance-length specifies
                // the current length of the selected resource.  e.g. */length
                _responseHeaders.ContentRange = new ContentRangeHeaderValue(_length);
                ApplyResponseHeaders(Constants.Status416RangeNotSatisfiable);

                _logger.LogRangeNotSatisfiable(SubPath);
                return;
            }

            long start, length;

            _responseHeaders.ContentRange = ComputeContentRange(_range, out start, out length);
            _response.ContentLength       = length;
            ApplyResponseHeaders(Constants.Status206PartialContent);

            string physicalPath = _fileInfo.PhysicalPath;
            var    sendFile     = _context.Features.Get <IHttpSendFileFeature>();

            if (sendFile != null && !string.IsNullOrEmpty(physicalPath))
            {
                _logger.LogSendingFileRange(_response.Headers[HeaderNames.ContentRange], physicalPath);
                // We don't need to directly cancel this, if the client disconnects it will fail silently.
                await sendFile.SendFileAsync(physicalPath, start, length, CancellationToken.None);

                return;
            }

            try
            {
                using (var readStream = _fileInfo.CreateReadStream())
                {
                    readStream.Seek(start, SeekOrigin.Begin); // TODO: What if !CanSeek?
                    _logger.LogCopyingFileRange(_response.Headers[HeaderNames.ContentRange], SubPath);
                    await StreamCopyOperation.CopyToAsync(readStream, _response.Body, length, _context.RequestAborted);
                }
            }
            catch (OperationCanceledException ex)
            {
                _logger.LogWriteCancelled(ex);
                // 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();
            }
        }
Exemplo n.º 20
0
        /// <summary>
        /// Performs the stream operation.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override async Task WriteBody(HttpResponse response)
        {
            using var conn = db.GetDbConnection();
            await conn.OpenAsync(response.HttpContext.RequestAborted)
            .ConfigureAwait(false);

            using var cmd = conn.CreateCommand();
            makeCommand(cmd);

            using var reader = await cmd.ExecuteReaderAsync(
                      CommandBehavior.SingleResult
                      | CommandBehavior.SequentialAccess
                      | CommandBehavior.CloseConnection,
                      response.HttpContext.RequestAborted)
                               .ConfigureAwait(false);

            var read = await reader.ReadAsync(response.HttpContext.RequestAborted)
                       .ConfigureAwait(false);

            if (!read)
            {
                throw new EndOfStreamException();
            }

            using var stream = reader.GetStream(0);
            if (hasRange)
            {
                const int FRAME_SIZE = 64 * 1024;
                if (rangeStart > 0)
                {
                    var buffer = new byte[FRAME_SIZE];
                    var toBurn = rangeStart;
                    while (toBurn > 0)
                    {
                        var shouldBurn = (int)Math.Min(toBurn, buffer.Length);
                        var wasBurned  = await stream.ReadAsync(buffer, 0, shouldBurn, response.HttpContext.RequestAborted);

                        toBurn -= wasBurned;
                    }
                }

                await StreamCopyOperation.CopyToAsync(stream, response.Body, RangeLength, FRAME_SIZE, response.HttpContext.RequestAborted);
            }
            else
            {
                await stream.CopyToAsync(response.Body, response.HttpContext.RequestAborted)
                .ConfigureAwait(false);
            }
        }
        /// <summary>
        /// sets up the response
        /// </summary>
        public async override Task ExecuteResultAsync(ActionContext context)
        {
            var response = context.HttpContext.Response;

            response.ContentType = ContentType;
            var contentDisposition = new ContentDispositionHeaderValue("attachment");

            contentDisposition.SetHttpFileName(FileDownloadName);
            response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString();
            response.StatusCode = StatusCodes.Status200OK;
            using (_fileStream)
            {
                await StreamCopyOperation.CopyToAsync(_fileStream, response.Body, null, context.HttpContext.RequestAborted);
            }
            File.Delete(_filePath);
        }
Exemplo n.º 22
0
        protected virtual async Task WriteExceptionAsync(ServerActionContext context, Exception error)
        {
            MemoryStream serializedException = new MemoryStream();

            context.RequestAborted.ThrowIfCancellationRequested();
            var httpContext = context.HttpContext;

            httpContext.Response.StatusCode = 500;

            try
            {
                object wrappedException = context.Configuration.ExceptionSerializer.Write(error);
                if (wrappedException == null)
                {
                    httpContext.Response.Body.Dispose();
                    return;
                }

                await context.GetSerializerOrThrow().WriteAsync(httpContext.Response.Body, context.Configuration.ExceptionSerializer.Type, wrappedException);

                serializedException.Seek(0, SeekOrigin.Begin);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new BoltServerException(
                          $"Failed to serialize exception response for action {context.Action.Name}.",
                          ServerErrorCode.SerializeException,
                          context.Action.Name,
                          context.RequestUrl,
                          e);
            }

            if (serializedException.Length == 0)
            {
                httpContext.Response.Body.Dispose();
                return;
            }

            httpContext.Response.ContentLength = serializedException.Length;
            httpContext.Response.ContentType   = context.GetSerializerOrThrow().MediaType;

            await StreamCopyOperation.CopyToAsync(serializedException, httpContext.Response.Body, null, httpContext.RequestAborted);
        }
Exemplo n.º 23
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            //app.UseDefaultFiles();
            //app.UseDirectoryBrowser();
            app.UseStaticFiles();
            app.MapWhen(context =>
            {
                return(!context.Request.Path.Value.StartsWith("/api"));
            }, appBuilder =>
            {
                var option = new RewriteOptions();
                option.AddRewrite(".*", "/index.html", true);
                appBuilder.UseRewriter(option);
                appBuilder.UseStaticFiles();

                appBuilder.Run(async c =>
                {
                    var file = env.WebRootFileProvider.GetFileInfo("index.html");

                    c.Response.ContentType = "text/html";
                    using (var fileStream = new FileStream(file.PhysicalPath, FileMode.Open, FileAccess.Read))
                    {
                        await StreamCopyOperation.CopyToAsync(fileStream, c.Response.Body, null, BufferSize, c.RequestAborted);
                    }
                });
            });
            //app.UseStaticFiles(new StaticFileOptions
            //{
            //    RequestPath = "/files",
            //    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "file"))
            //});
            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
Exemplo n.º 24
0
 public static IEndpointRouteBuilder UseBlocklyAutomation(this IEndpointRouteBuilder endpoints)
 {
     //endpoints.MapFallbackToFile("BlocklyAutomation/{**:nonfile}", "BlocklyAutomation/index.html");
     endpoints.Map("BlocklyAutomation/{**:nonfile}", async ctx =>
     {
         var dir              = FileProvider.GetDirectoryContents("BlocklyAutomation").ToArray();
         var file             = dir.Where(it => it?.Name?.ToLower() == "index.html").FirstOrDefault();
         var response         = ctx.Response;
         response.ContentType = contentFromExtension(file.Name);
         //in net 6 use sendfileasync
         using (var fileContent = file.CreateReadStream())
         {
             await StreamCopyOperation.CopyToAsync(fileContent, response.Body, file.Length, CancellationToken.None);
         }
     });
     return(endpoints);
 }
Exemplo n.º 25
0
        private void LoadToTempFile()
        {
            string path = Path.GetTempFileName();

            using (Stream sourceStream = File.OpenRead(this.OriginalPath))
            {
                using (Stream targetStream = File.OpenWrite(path))
                {
                    var operation = new StreamCopyOperation(sourceStream, targetStream, 32 * 1024, true);

                    operation.CopyProgressChanged += (sender, e) => this.OnCachingProgressChanged(e);

                    operation.Execute();
                }
            }

            this.StreamingPath = path;
        }
Exemplo n.º 26
0
        /// <summary>
        /// Copies the specified file to the target directory.
        /// </summary>
        /// <param name="sourceFileSystem">The source file system.</param>
        /// <param name="sourceFile">The source file.</param>
        /// <param name="targetDirectory">The target directory.</param>
        /// <exception cref="AccessException">The source file or target directory could not be accessed.</exception>
        /// /// <exception cref="ArgumentException">The target directory is not of type <see cref="FlagFtp.FtpDirectoryInfo"/>.</exception>
        public void CopyFile(IFileSystem sourceFileSystem, IFileInfo sourceFile, IDirectoryInfo targetDirectory)
        {
            sourceFileSystem.ThrowIfNull(() => sourceFileSystem);
            sourceFile.ThrowIfNull(() => sourceFile);
            targetDirectory.ThrowIfNull(() => targetDirectory);

            if (!(targetDirectory is FtpDirectoryInfo))
            {
                throw new ArgumentException("The target directory must be of type FtpDirectoryInfo.", "targetDirectory");
            }

            Uri targetFilePath = new Uri(this.CombinePath(targetDirectory.FullName, sourceFile.Name));

            try
            {
                using (Stream sourceStream = sourceFileSystem.OpenFileStream(sourceFile))
                {
                    using (Stream targetStream = this.client.OpenWrite(targetFilePath))
                    {
                        if (sourceFile.Length > 0)
                        {
                            var copyOperation = new StreamCopyOperation(sourceStream, targetStream, 8 * 1024, true);

                            copyOperation.CopyProgressChanged +=
                                (sender, e) => this.FileCopyProgressChanged.RaiseSafe(this, e);

                            copyOperation.Execute();
                        }
                    }
                }
            }

            catch (WebException ex)
            {
                switch (ex.Status)
                {
                case WebExceptionStatus.ConnectFailure:
                case WebExceptionStatus.ProxyNameResolutionFailure:
                    throw new FileSystemUnavailableException("The FTP server is currently unavailable.", ex);
                }

                throw new AccessException("The file could not be accessed", ex);
            }
        }
        protected async Task WriteResource(DashboardResponse response, string filePath)
        {
            var fileInfo = _fileProvider.GetFileInfo(filePath);

            if (!fileInfo.Exists)
            {
                throw new ArgumentException($@"Could not find file '{fileInfo.PhysicalPath}'");
            }
            using (var readStream = fileInfo.CreateReadStream())
            {
                if (readStream == null)
                {
                    throw new ArgumentException($@"Resource with name {filePath} not found.");
                }
                // await readStream.CopyToAsync(response.Body).ConfigureAwait(false);
                // Larger StreamCopyBufferSize is required because in case of FileStream readStream isn't going to be buffering
                await StreamCopyOperation.CopyToAsync(readStream, response.Body, fileInfo.Length, StreamCopyBufferSize, CancellationToken.None);
            }
        }
        public static async Task _SendStreamContents(this HttpResponse h, Stream sourceStream, long?count, string?contentsType = Consts.MimeTypes.OctetStream, CancellationToken cancel = default,
                                                     ReadOnlyMemory <byte> preData = default, ReadOnlyMemory <byte> postData = default, int statusCode = Consts.HttpStatusCodes.Ok)
        {
            h.ContentType   = contentsType;
            h.ContentLength = count + preData.Length + postData.Length;
            h.StatusCode    = statusCode;

            if (preData.IsEmpty == false)
            {
                await h.Body.WriteAsync(preData, cancel);
            }

            await StreamCopyOperation.CopyToAsync(sourceStream, h.Body, count, cancel);

            if (postData.IsEmpty == false)
            {
                await h.Body.WriteAsync(postData, cancel);
            }
        }
        private static async Task SendFileAsyncCore(HttpResponse response, IFileInfo file, long offset, long?count, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(file.PhysicalPath))
            {
                CheckRange(offset, count, file.Length);

                using (var fileContent = file.CreateReadStream())
                {
                    if (offset > 0)
                    {
                        fileContent.Seek(offset, SeekOrigin.Begin);
                    }
                    await StreamCopyOperation.CopyToAsync(fileContent, response.Body, count, cancellationToken);
                }
            }
            else
            {
                await response.SendFileAsync(file.PhysicalPath, offset, count, cancellationToken);
            }
        }
Exemplo n.º 30
0
        /// <summary>
        /// Copy a stream into the response body
        /// </summary>
        /// <param name="response">Current <see cref="HttpResponse"/></param>
        /// <param name="stream">The <see cref="Stream"/> to copy from</param>
        /// <param name="contentType">The content type for the response</param>
        /// <param name="contentDisposition">The content disposition to allow file downloads</param>
        /// <returns><see cref="Task"/></returns>
        public static async Task FromStream(this HttpResponse response, Stream source, string contentType, ContentDisposition contentDisposition = null)
        {
            var contentLength = source.Length;

            response.Headers["Accept-Ranges"] = "bytes";

            response.ContentType = contentType;

            if (contentDisposition != null)
            {
                response.Headers["Content-Disposition"] = contentDisposition.ToString();
            }

            if (RangeHeaderValue.TryParse(response.HttpContext.Request.Headers["Range"].ToString(), out var rangeHeader))
            {
                //Server should return multipart/byteranges; if asking for more than one range but pfft...
                var rangeStart = rangeHeader.Ranges.First().From;
                var rangeEnd   = rangeHeader.Ranges.First().To ?? contentLength - 1;

                if (!rangeStart.HasValue || rangeEnd > contentLength - 1)
                {
                    response.StatusCode = (int)HttpStatusCode.RequestedRangeNotSatisfiable;
                }
                else
                {
                    response.Headers["Content-Range"] = $"bytes {rangeStart}-{rangeEnd}/{contentLength}";
                    response.StatusCode = (int)HttpStatusCode.PartialContent;
                    if (!source.CanSeek)
                    {
                        throw new InvalidOperationException("Sending Range Responses requires a seekable stream eg. FileStream or MemoryStream");
                    }

                    source.Seek(rangeStart.Value, SeekOrigin.Begin);
                    await StreamCopyOperation.CopyToAsync(source, response.Body, rangeEnd - rangeStart.Value + 1, 65536, response.HttpContext.RequestAborted);
                }
            }
            else
            {
                await StreamCopyOperation.CopyToAsync(source, response.Body, default, 65536, response.HttpContext.RequestAborted);