private void ReadStream(Stream stream, HttpRequest request, string username, AsyncFileUploadProgress progress, AsyncFileUploadRequestParser parser)
        {
            const int bufferSize = 1024 * 4; // in bytes

            byte[] buffer = new byte[bufferSize];
            while (progress.BytesRemaining > 0)
            {
                int bytesRead = stream.Read(buffer, 0, Math.Min(progress.BytesRemaining, bufferSize));
                progress.TotalBytesRead = bytesRead == 0
                                          ? progress.ContentLength
                                          : (progress.TotalBytesRead + bytesRead);

                if (bytesRead > 0)
                {
                    parser.ParseNext(buffer, bytesRead);
                    progress.FileName = parser.CurrentFileName;
                }

                _cacheService.SetProgress(username, progress);

#if DEBUG
                if (request.IsLocal)
                {
                    // If the request is from local machine, the upload will be too fast to see the progress.
                    // Slow it down a bit.
                    System.Threading.Thread.Sleep(30);
                }
#endif
            }
        }
Example #2
0
        private void PostAuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (!app.Context.User.Identity.IsAuthenticated)
            {
                return;
            }

            if (!IsAsyncUploadRequest(app.Context))
            {
                return;
            }

            var username = app.Context.User.Identity.Name;

            if (string.IsNullOrEmpty(username))
            {
                return;
            }

            HttpRequest request       = app.Context.Request;
            string      contentType   = request.ContentType;
            int         boundaryIndex = contentType.IndexOf("boundary=", StringComparison.OrdinalIgnoreCase);
            string      boundary      = "--" + contentType.Substring(boundaryIndex + 9);
            var         requestParser = new AsyncFileUploadRequestParser(boundary, request.ContentEncoding);

            var    headers          = request.Headers;
            string uploadTracingKey = UploadHelper.GetUploadTracingKey(headers);

            var progress  = new AsyncFileUploadProgress(request.ContentLength);
            var uploadKey = username + uploadTracingKey;

            _cacheService.SetProgress(uploadKey, progress);

            if (request.ReadEntityBodyMode != ReadEntityBodyMode.None)
            {
                return;
            }

            Stream uploadStream = request.GetBufferedInputStream();

            Debug.Assert(uploadStream != null);

            ReadStream(uploadStream, request, uploadKey, progress, requestParser);
        }
        private void PostAuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;

            if (!app.Context.User.Identity.IsAuthenticated)
            {
                return;
            }

            if (!IsAsyncUploadRequest(app.Context))
            {
                return;
            }

            var username = app.Context.User.Identity.Name;
            if (String.IsNullOrEmpty(username))
            {
                return;
            }

            HttpRequest request = app.Context.Request;
            string contentType = request.ContentType;
            int boundaryIndex = contentType.IndexOf("boundary=", StringComparison.OrdinalIgnoreCase);
            string boundary = "--" + contentType.Substring(boundaryIndex + 9);
            var requestParser = new AsyncFileUploadRequestParser(boundary, request.ContentEncoding);

            var progress = new AsyncFileUploadProgress(request.ContentLength);
            _cacheService.SetProgress(username, progress);

            if (request.ReadEntityBodyMode != ReadEntityBodyMode.None)
            {
                return;
            }

            Stream uploadStream = request.GetBufferedInputStream();
            Debug.Assert(uploadStream != null);

            ReadStream(uploadStream, request, username, progress, requestParser);
        }
        private void ReadStream(Stream stream, HttpRequest request, string username, AsyncFileUploadProgress progress, AsyncFileUploadRequestParser parser)
        {
            const int bufferSize = 1024 * 4; // in bytes

            byte[] buffer = new byte[bufferSize];
            while (progress.BytesRemaining > 0)
            {
                int bytesRead = stream.Read(buffer, 0, Math.Min(progress.BytesRemaining, bufferSize));
                progress.TotalBytesRead = bytesRead == 0
                                          ? progress.ContentLength
                                          : (progress.TotalBytesRead + bytesRead);

                if (bytesRead > 0)
                {
                    parser.ParseNext(buffer, bytesRead);
                    progress.FileName = parser.CurrentFileName;
                }

                _cacheService.SetProgress(username, progress);

#if DEBUG
                if (request.IsLocal)
                {
                    // If the request is from local machine, the upload will be too fast to see the progress.
                    // Slow it down a bit.
                    System.Threading.Thread.Sleep(30);
                }
#endif
            }
        }