public async Task <bool> LoadInfoFromServer() { bool success; try { var response = await client.GetProcessedFile(Config.UpdateInfoFilename); if (response.IsSuccessful) { var sr = new StreamReader(response.Stream); var raw = await sr.ReadToEndAsync(); sr.Close(); UpdateInfo = JObject.Parse(raw).ToObject <UpdateInfo>(); } success = true; } catch (Exception) { success = false; } return(success); }
/// <summary> /// Downloades the File at the given sourceUri into a Temp file in the /// local System. /// FileInfo for this Temp File will be returend /// </summary> /// <param name="dstUri"></param> /// <returns></returns> public FileInfo DownloadRemoteFileToTemp(CancellationToken cancleToken, string sourceUri) { sourceUri = _config.Remote.RemoteFolderPath.RemoveTailingSlashes() + sourceUri; //No locks needed at this time because we will only read the File! try { using (Stream webstream = _webDavClient.GetProcessedFile(sourceUri).Result.Stream) { //Writing the WebStream to the new generated file string tempFile = string.Format("/tmp/WebDavSync{0}.tmp", Guid.NewGuid().ToString()); _logger.Debug("Downloading remote file " + sourceUri + " to temp dst " + tempFile); bool wasCancled = false; using (var filestream = new FileStream(tempFile, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None)) { byte[] buffer = new byte[5120]; // read in chunks of 5KB int bytesRead; //As long as we can ready data, write the Data to the //new File while ((bytesRead = webstream.Read(buffer, 0, buffer.Length)) > 0 && !wasCancled) { if (cancleToken.IsCancellationRequested) { wasCancled = true; } else { filestream.Write(buffer, 0, bytesRead); } } if (!wasCancled) { _logger.Debug("Download successful"); } if (wasCancled) { _logger.Debug("Download was cancled"); } } FileInfo tmpFile = new FileInfo(tempFile); //Cleanup in case cancleation is requested if (wasCancled) { tmpFile.Delete(); return(null); } return(tmpFile); } } catch (Exception exc) { _logger.Error("Error while downloading remote File as Temp file", exc); return(null); } }
public async Task <Stream> DownloadFile(string relativeFilePath) { using (var client = new WebDavClient(_webDavClientParams)) { var res = await client.GetProcessedFile(relativeFilePath); if (!res.IsSuccessful || res.StatusCode == 404) { throw new FileNotFoundException($"Cannot find file {relativeFilePath}"); } return(res.Stream); } }