public Stream Open(XmltvSource source) { if (source.IsFile) { return(OpenFile(source.Path)); } throw new Exception($"Cannot process {source.Path} because it doesn't link to an existing file."); }
public Stream Open(XmltvSource source) { if (source.IsFile) { return(_fileProcessor.Open(source)); } if (source.IsUrl) { return(_urlProcessor.Open(source)); } throw new Exception($"Cannot process source {source.Path} because it doesn't look as a file or URL"); }
public Stream Open(XmltvSource source) { if (!source.IsUrl) { throw new Exception($"Cannot process source {source.Path} because it doesn't look as URL."); } var uri = new Uri(source.Path); var tempPath = Path.GetTempFileName(); if (File.Exists(tempPath)) { File.Delete(tempPath); } var r = HttpWebRequest.CreateHttp(uri); string responseType; using (var resp = r.GetResponse()) { responseType = resp.ContentType; var length = resp.ContentLength; Log.Logger.Information($"Stream '{uri}' is ready for download. \nContent type: {responseType}\nLength: {length/1024}Kb"); using (var stream = resp.GetResponseStream()) { if (stream == null) { Log.Logger.Error("Cannot read response stream from the server"); } else { using (var dest = new FileStream(tempPath, FileMode.Create)) { int counter = 0; var buffer = new byte[4096]; long percentage = 0; while (counter < length) { var readBytes = stream.Read(buffer, 0, buffer.Length); dest.Write(buffer, 0, readBytes); counter += readBytes; int z = (int)(counter / (length / 100)); if (percentage != z) { Log.Information($"Counter: {counter/1024} of {length/1024} ({z}%)"); percentage = z; } } } } } } var fileName = uri.AbsoluteUri.Split('/').LastOrDefault(); if (string.IsNullOrWhiteSpace(fileName)) { fileName = tempPath + DetermineFileExtension(tempPath, responseType); } File.Move(tempPath, fileName); return(_processor.Open(new XmltvSource(fileName, source.Priority))); }