Example #1
0
        /// <summary>
        /// Get the file to download
        /// </summary>
        public async override Task <gDownloadFileResponse> GetDownloadFile(gDownloadFileRequest request, ServerCallContext context)
        {
            var result = await _mediatr.Send(new GetDownloadByIdQuery(request.DownloadId, request.RequesterAddress));

            return(new gDownloadFileResponse()
            {
                Status = result.Convert(),
                FileItem = result.File.Convert(),
                DownloadSpeed = result.DownloadSpeed,
            });
        }
Example #2
0
        public async override Task <gDownloadFileResponse> GetDownloadFile(gDownloadFileRequest request, ServerCallContext context)
        {
            var response = new gDownloadFileResponse();
            var result   = await _mediatr.Send(new GetDownloadByIdQuery(request.DownloadId, request.RequesterAddress));

            response.Status = result.Convert();
            if (result.State == OperationState.Success)
            {
                response.FileItem      = result.File.Convert();
                response.DownloadSpeed = result.DownloadSpeed;
            }

            return(response);
        }
        public async Task <StartDownloadResult> Handle(StartDownloadCommand request, CancellationToken cancellationToken)
        {
            StartDownloadResult Result = new StartDownloadResult();

            gDownloadFileRequest downloadRequest = new gDownloadFileRequest()
            {
                DownloadId       = request.DownloadId,
                RequesterAddress = _httpContext.Request.Host.Value
            };
            // query the api
            gDownloadFileResponse dResponse = await _fileMngClient.GetDownloadFileAsync(downloadRequest);

            if (dResponse == null)
            {
                Result.ErrorContent = new ErrorContent("No response has been received from the server.", ErrorOrigin.Server);
                return(Result);
            }
            if (dResponse.Status.Status != Protos.RequestStatus.Success)
            {
                Result.ErrorContent = new ErrorContent(dResponse.Status.Message, ErrorOrigin.None);
                return(Result);
            }
            string filePath = Path.Combine(_uploadOpt.UploadPath, dResponse.FileItem.UserId, dResponse.FileItem.Id, dResponse.FileItem.Id);

            // Check file exist on disk
            if (!File.Exists(filePath))
            {
                _httpContext.Response.StatusCode = 404;
                Result.ErrorContent = new ErrorContent("File not found, it may have been moved or deleted!", ErrorOrigin.Server);
                return(Result);
            }

            // Check file is not too large
            if (dResponse.FileItem.Size > uint.MaxValue) // ~4Gb
            {
                _httpContext.Response.StatusCode = 413;
                Result.ErrorContent = new ErrorContent("File is too large!", ErrorOrigin.Server);
                return(Result);
            }

            // Get the response header information made by the current http request.
            HttpResponseHeader responseHeader = GetResponseHeader(dResponse.FileItem);

            if (responseHeader == null)
            {
                Result.ErrorContent = new ErrorContent("Invalid response header!", ErrorOrigin.Server);
                return(Result);
            }

            // All good, start download
            var dStatus = await StartDownload(responseHeader, filePath, dResponse.DownloadSpeed);

            // Once download is completed we send request to increment download count
            if (dStatus == DownloadStatus.COMPLETED)
            {
                await _fileMngClient.FileDownloadCompletedAsync(new gDownloadCompletedRequest()
                {
                    FileId      = dResponse.FileItem.Id,
                    RequesterIp = _httpContext.Request.Host.Value
                });
            }
            else
            {
                _logger.LogInformation("Download request status: " + dStatus);
            }
            return(null);
        }