示例#1
0
 private static string GetChunkFilePath(FlowRequestContext context, FlowRequest request)
 {
     return string.Concat(
         context.GetChunkPathFunc(request),
         "/",
         context.GetChunkFileName(request));
 }
示例#2
0
        public string GetFilePath(FlowRequest request)
        {
            if (GetFilePathFunc == null)
            {
                throw new InvalidOperationException("GetFilePathFunc not set. Set it to valid function.");
            }

            return(GetFilePathFunc(request));
        }
示例#3
0
        public string GetChunkFileName(FlowRequest request)
        {
            if (GetChunkFileNameFunc == null)
            {
                throw new InvalidOperationException("GetChunkFileNameFunc is null. Set it to valid function.");
            }

            return(GetChunkFileNameFunc(request));
        }
        public string GetFilePath(FlowRequest request)
        {
            if (GetFilePathFunc == null)
            {
                throw new InvalidOperationException("GetFilePathFunc not set. Set it to valid function.");
            }

            return GetFilePathFunc(request);
        }
        public string GetChunkFileName(FlowRequest request)
        {
            if (GetChunkFileNameFunc == null)
            {
                throw new InvalidOperationException("GetChunkFileNameFunc is null. Set it to valid function.");
            }

            return GetChunkFileNameFunc(request);
        }
示例#6
0
        private async Task <HttpResponseMessage> HandlePostRequest(FlowRequestContext context)
        {
            // read request
            FlowRequest request = await _requestReader.ReadPostAsync(
                context,
                _fileSystem).ConfigureAwait(false);

            // is valid request?
            if (!IsValidRequest(context, request))
            {
                await _fileSystem.DeleteAsync(request.TemporaryFile.Item1).ConfigureAwait(false);

                return(context.HttpRequest.CreateErrorResponse(
                           HttpStatusCode.BadRequest,
                           "Invalid flow POST request"));
            }

            // upload temporary to chunks
            var chunkFilePath = GetChunkFilePath(context, request);

            using (Stream chunkStream = await _fileSystem.OpenWriteAsync(chunkFilePath).ConfigureAwait(false))
            {
                using (Stream tempStream = await _fileSystem.OpenReadAsync(request.TemporaryFile.Item1).ConfigureAwait(false))
                {
                    await tempStream.CopyToAsync(chunkStream).ConfigureAwait(false);
                }
            }

            // delete temporary
            await _fileSystem.DeleteAsync(request.TemporaryFile.Item1).ConfigureAwait(false);

            // if last chunk combine and move to files
            if (request.IsLastChunk)
            {
                await CombineAsync(context, request).ConfigureAwait(false);
            }

            return(context.HttpRequest.CreateResponse(HttpStatusCode.OK));
        }
示例#7
0
        private async Task <HttpResponseMessage> HandleGetRequest(FlowRequestContext context)
        {
            FlowRequest request = await _requestReader.ReadGetAsync(context.HttpRequest).ConfigureAwait(false);

            if (!IsValidRequest(context, request))
            {
                return(context.HttpRequest.CreateErrorResponse(
                           HttpStatusCode.BadRequest,
                           "Invalid flow GET request"));
            }

            string filePath = GetChunkFilePath(context, request);

            if (!await _fileSystem.ExistsAsync(filePath).ConfigureAwait(false))
            {
                return(context.HttpRequest.CreateErrorResponse(
                           HttpStatusCode.NoContent,
                           "File not found"));
            }

            return(context.HttpRequest.CreateResponse(HttpStatusCode.OK));
        }
示例#8
0
        private bool IsValidRequest(FlowRequestContext context, FlowRequest request)
        {
            if (!request.FlowChunkNumber.HasValue ||
                !request.FlowChunkSize.HasValue ||
                !request.FlowTotalSize.HasValue)
            {
                return(false);
            }

            ulong chunkNumber = request.FlowChunkNumber.Value;
            ulong chunkSize   = request.FlowChunkSize.Value;
            ulong totalSize   = request.FlowTotalSize.Value;


            if (chunkNumber == 0 || chunkSize == 0 || totalSize == 0)
            {
                return(false);
            }

            double numberOfChunks =
                Math.Max(Math.Floor(request.FlowTotalSize.Value / (request.FlowChunkSize.Value * 1.0)), 1);

            if (chunkNumber > numberOfChunks)
            {
                return(false);
            }

            if (totalSize > context.MaxFileSize)
            {
                return(false);
            }

            if (chunkSize > context.MaxFileSize)
            {
                return(false);
            }

            return(true);
        }
示例#9
0
        private bool IsValidRequest(FlowRequestContext context, FlowRequest request)
        {
            if (!request.FlowChunkNumber.HasValue || 
                !request.FlowChunkSize.HasValue ||
                !request.FlowTotalSize.HasValue)
            {
                return false;
            }

            ulong chunkNumber = request.FlowChunkNumber.Value;
            ulong chunkSize = request.FlowChunkSize.Value;
            ulong totalSize = request.FlowTotalSize.Value;


            if (chunkNumber == 0 || chunkSize == 0 || totalSize == 0)
            {
                return false;
            }

            double numberOfChunks =
                Math.Max(Math.Floor(request.FlowTotalSize.Value/(request.FlowChunkSize.Value*1.0)), 1);

            if (chunkNumber > numberOfChunks)
            {
                return false;
            }

            if (totalSize > context.MaxFileSize)
            {
                return false;
            }

            if (chunkSize > context.MaxFileSize)
            {
                return false;
            }

            return true;
        }
示例#10
0
        private async Task CombineAsync(FlowRequestContext context, FlowRequest request)
        {
            var filePath = string.Concat(
                context.GetFilePathFunc(request),
                "/",
                context.GetFileName(request));

            var chunkPath = context.GetChunkPathFunc(request);
            var chunkFilePaths = await _fileSystem.ListDirectoryAsync(chunkPath);

            using (var fileStream = await _fileSystem.OpenWriteAsync(filePath))
            {
                foreach (var file in chunkFilePaths)
                {
                    using (var sourceStream = await _fileSystem.OpenReadAsync(file))
                    {
                        await sourceStream.CopyToAsync(fileStream);
                    }
                }

                await fileStream.FlushAsync();
            }

            await _fileSystem.DeleteDirectoryAsync(chunkPath);
        }