public ResponseBase Init(IList <string> urls, IList <AuthenticatedUrl> urlsWithAuthentification = null) { Log.Information("Starting the download process"); if (urlsWithAuthentification == null) { urlsWithAuthentification = new List <AuthenticatedUrl>(); } Log.Information("List of URLs passed down : " + string.Join(";", urls)); Log.Information("List of URLs requiring authentication passed down : " + string.Join(";", urlsWithAuthentification)); var responseInit = _initializeDonwload.InitConfigData(); if (responseInit.Denied) { Log.Fatal("Could not retrieve config"); var failResponse = new ResponseBase() { Denied = true }; failResponse.AddListMessage(responseInit.Messages); } _configurationSetting = responseInit.ReturnedValue; //Prepare the file to be downloaded extract the protocol Type _resourceDetails = ResourceDetail.FromListUrl(urls, urlsWithAuthentification).ToList(); return(new ResponseBase() { Denied = false }); }
public ResponseBase ProcessSegment(ResourceDetail rl, IProtocolDownloader protocolDownloader, Segment segment) { var response = new ResponseBase(); segment.LastError = null; try { if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition) { segment.State = SegmentState.Finished; return(new ResponseBase <CalculatedSegment[]>() { Denied = false }); } int buffSize = 8192; byte[] buffer = new byte[buffSize]; segment.State = SegmentState.Connecting; var responseDownloadSegment = protocolDownloader.CreateStream(rl, segment.StartPosition, segment.EndPosition); if (responseDownloadSegment.Denied || responseDownloadSegment.ReturnedValue == null) { Serilog.Log.Error("Task #" + Task.CurrentId + " File : " + segment.CurrentURL + " Current segment " + segment.Index + "Current Try: " + segment.CurrentTry + " An exception was raised in processSegment.cs"); segment.State = SegmentState.Error; segment.LastError = string.Join(",", responseDownloadSegment.Messages); segment.CurrentTry++; var responseFail = new ResponseBase() { Denied = true }; responseFail.AddListMessage(responseDownloadSegment.Messages); return(responseFail); } segment.InputStream = responseDownloadSegment.ReturnedValue; using (segment.InputStream) { segment.State = SegmentState.Downloading; long readSize; do { readSize = segment.InputStream.Read(buffer, 0, buffSize); if (segment.EndPosition > 0 && segment.StartPosition + readSize > segment.EndPosition) { readSize = (segment.EndPosition - segment.StartPosition); if (readSize <= 0) { segment.StartPosition = segment.EndPosition; break; } } lock (segment.OutputStream) { segment.OutputStream.Position = segment.StartPosition; segment.OutputStream.Write(buffer, 0, (int)readSize); } segment.IncreaseStartPosition(readSize); if (segment.EndPosition > 0 && segment.StartPosition >= segment.EndPosition) { segment.StartPosition = segment.EndPosition; break; } } while (readSize > 0); if (segment.State == SegmentState.Downloading) { Serilog.Log.Information("Task #" + Task.CurrentId + " File : " + segment.CurrentURL + " Current segment " + segment.Index + "Current Try: " + segment.CurrentTry + " finished successfully"); segment.State = SegmentState.Finished; response.Denied = false; } } } catch (Exception ex) { Serilog.Log.Error(ex, "Task #" + Task.CurrentId + " File : " + segment.CurrentURL + " Current segment " + segment.Index + "Current Try: " + segment.CurrentTry + " An exception was raised"); segment.CurrentTry++; while (ex.InnerException != null) { ex = ex.InnerException; } segment.State = SegmentState.Error; segment.LastError = ex.Message; response.Denied = true; response.Messages.Add(ex.Message); } finally { segment.InputStream = null; } return(response); }