public String OnStreamResponse(Stream streamResponse, IDictionary <String, Object> parameters, Boolean localCacheEnabled) { String responseString = null; if (localCacheEnabled) { responseString = C8oTranslator.StreamToString(streamResponse); this.OnStringResponse(responseString, parameters); } else { this.OnXmlResponse(C8oTranslator.StreamToXml(streamResponse), parameters); } return(responseString); }
private async Task DownloadChunk(C8o c8o, Stream createdFileStream, string filepath, string fsConnector, string uuid, int i, JObject task, C8oFileTransferStatus transferStatus) { Stream chunkFS = null; var chunkpath = filepath + ".chunk"; try { var fsurl = c8o.EndpointConvertigo + "/fullsync/" + fsConnector + "/" + uuid + "_" + i; var fsurlatt = fsurl + "/chunk"; var digest = "no digest"; int retry = 5; while (retry > 0) { try { Debug("Getting the document at: " + fsurl); var responseString = C8oTranslator.StreamToString(c8o.httpInterface.HandleGetRequest(fsurl).Result.GetResponseStream()); Debug("The document content: " + responseString); var json = C8oTranslator.StringToJson(responseString); digest = json["_attachments"]["chunk"]["digest"].ToString(); retry = 0; } catch (Exception e) { if (retry-- > 0) { Debug("Failed to get the chunk descriptor, retry. Cause: " + e.Message); } else { throw new Exception("Failed to get the chunk descriptor at " + fsurl, e); } } } chunkFS = fileManager.CreateFile(chunkpath); retry = 5; while (retry > 0) { var md5 = "no md5"; try { Debug("Getting the attachment at: " + fsurlatt); chunkFS.Position = 0; AppendChunk(chunkFS, fsurlatt, c8o); chunkFS.Position = 0; md5 = "md5-" + c8o.GetMD5(chunkFS); } catch (Exception e) { Debug("Download Chunk failed, retry it. Cause: " + e.Message); } Debug("Comparing digests: " + digest + " / " + md5); if (digest.Equals(md5)) { chunkFS.Position = 0; chunkFS.CopyTo(createdFileStream, 4096); Debug("Chunk '" + uuid + "_" + i + "' assembled."); retry = 0; await c8oTask.CallJson("fs://.post", C8o.FS_POLICY, C8o.FS_POLICY_MERGE, "_id", task["_id"].Value <string>(), "download", transferStatus.Current = i + 1, "position", createdFileStream.Position ).Async(); Notify(transferStatus); } else if (retry-- > 0) { Debug("The digest doesn't match, retry downloading."); } else { throw new Exception("Invalid digest: " + digest + " / " + md5); } } } catch (Exception e2) { Debug("Failed to DownloadChunk, retry soon."); throw e2; } finally { if (chunkFS != null) { chunkFS.Dispose(); } fileManager.DeleteFile(chunkpath); } }