public void DownloadDocument(DocumentInfo document, string path) { var request = new DownloadDocumentRequest { OidDocument = document.OidDocument, Version = document.Version, ChunkNumber = 0, ChunkSize = GetChunkSize() }; DownloadDocumentResponse response = _flowDocsService.DownloadDocument(request); var filePath = Path.Combine(path, response.DocumentName); document.DocumentName = filePath; document.Description = response.Description; document.Owner = response.Owner; document.Path = response.Path; document.MimeType = response.MimeType; document.FileHash = response.FileHash; using (var writer = new FileStream(filePath, FileMode.Create)) { int chunks = 0; writer.Write(response.DataField, 0, response.ChunkSize); request.ChunkNumber = ++chunks; while (chunks < response.ChunkTotal) { response = _flowDocsService.DownloadDocument(request); writer.Write(response.DataField, 0, response.ChunkSize); request.ChunkNumber = ++chunks; } } }
public DownloadDocumentResponse DownloadDocument(DownloadDocumentRequest request) { if (request == null) { throw new ArgumentNullException("request"); } DownloadDocumentResponse response = null; var httpResponse = SendGetRequest(request, "DownloadDocument"); //The DownloadDocument endpoint will return either the file as an attachment, or a JSON object if there was an error. //DispositionType will be attachment if file was successfully returned. if (httpResponse.Content.Headers.ContentDisposition != null && httpResponse.Content.Headers.ContentDisposition.DispositionType == "attachment") { response = new DownloadDocumentResponse { FileStream = httpResponse.Content.ReadAsStreamAsync().Result, FileName = httpResponse.Content.Headers.ContentDisposition.FileName }; } else { //File was not successfully returned, check for JSON response instead response = LoadResponse <DownloadDocumentResponse>(httpResponse); } response.DocumentID = request.DocumentID; return(response); }
public void Should_Return_Exception_If_Hash_Doesnt_Match() { var mockDataService = new Mock <IFlowDocsOperations>(); var sut = new FlowDocsDocument(mockDataService.Object); sut.ChunkSize = 10; var response = new DownloadDocumentResponse { ChunkNumber = 1, ChunkSize = 10, ChunkTotal = 10, DataField = ASCIIEncoding.ASCII.GetBytes("0123456789"), Description = "desc", DocumentName = "testfile.txt", OidDocument = Guid.Empty, Owner = "me", Path = "", Version = 1, FileHash = Md5Hash.CreateMd5Hash(ASCIIEncoding.ASCII.GetBytes("000111333")) }; mockDataService.Setup(x => x.DownloadDocument(Moq.It.IsAny <DownloadDocumentRequest>())) .Returns(response); var doc = new DocumentInfo { OidDocument = Guid.Empty, Version = 1 }; var tmpFile = System.Configuration.ConfigurationManager.AppSettings["TmpFile"]; sut.DownloadDocument(doc, tmpFile, DocumentDownloadMode.LastVersion); }
/// <summary> /// Download Document. It calls the abstract method to perform the operation /// </summary> /// <param name="request">Request</param> /// <returns>Response</returns> public DownloadDocumentResponse DownloadDocument(DownloadDocumentRequest request) { if (request.OidDocument == Guid.Empty) { throw new ArgumentException("OidDocument should not be empty"); } DownloadDocumentResponse response = null; DownloadChunk(request, ref response); return(response); }
/// <summary> /// Download Chunk /// </summary> /// <param name="request">Request</param> /// <param name="response">Response</param> override protected void DownloadChunk(DownloadDocumentRequest request, ref DownloadDocumentResponse response) { if (FlowDocsUnitOfWork != null) { DownloadChunk(FlowDocsUnitOfWork, request, ref response); } else { using (var uow = new FlowDocsUnitOfWork()) { DownloadChunk(uow, request, ref response); } } }
/// <summary> /// Download Chunk /// </summary> /// <param name="uow">Uow</param> /// <param name="request">Request</param> /// <param name="response">Response</param> private void DownloadChunk(IFlowDocsUnitOfWork uow, DownloadDocumentRequest request, ref DownloadDocumentResponse response) { Document doc = null; int identity = 0; identity = GetDocument(uow, request, ref doc); response = new DownloadDocumentResponse(); response.DocumentName = doc.DocumentName; response.Version = doc.Version; response.Description = doc.Description; response.Owner = doc.Owner; response.Path = doc.Path; response.MimeType = doc.MimeType; response.FileSize = doc.FileSize; SqlServerBlobStream blob = null; try { if (_provider.OpenProcedure != null && _provider.ReadProcedure != null) { blob = new SqlServerBlobStream( _provider.ConnectionString, identity, _provider.OpenProcedure, _provider.ReadProcedure, FileAccess.Read); } else { blob = new SqlServerBlobStream( _provider.ConnectionString, _provider.TableName, _provider.DataColumnName, identity, _provider.FileNameColumnName, _provider.MIMETypeColumnName, FileAccess.Read); } blob.Seek(request.ChunkNumber * request.ChunkSize, SeekOrigin.Begin); response.DataField = new byte[request.ChunkSize]; response.ChunkSize = blob.Read(response.DataField, 0, request.ChunkSize); response.ChunkNumber = request.ChunkNumber; response.ChunkTotal = (int)Math.Ceiling((decimal)blob.Length / request.ChunkSize); response.FileHash = Md5Hash.CreateMd5Hash(response.DataField); } finally { if (blob != null) { blob.Close(); //blob.Dispose(); } } }
/// <summary> /// Download Chunk /// </summary> /// <remarks> /// Abstract factory method. /// </remarks> /// <param name="request">Request</param> /// <param name="response">Response</param> abstract protected void DownloadChunk(DownloadDocumentRequest request, ref DownloadDocumentResponse response);
/// <summary> /// Download Document /// </summary> /// <param name="document">Document</param> /// <param name="path">Path</param> /// <param name="mode">Mode</param> public void DownloadDocument(DocumentInfo document, string path, DocumentDownloadMode mode) { var request = new DownloadDocumentRequest { OidDocument = document.OidDocument, Version = document.Version, ChunkNumber = 0, ChunkSize = GetChunkSize(), Mode = mode }; // Get the first chunk of data DownloadDocumentResponse response = null; if (_flowDocsService == null) { using (var docsProxy = new FlowDocsOperations()) { response = docsProxy.DownloadDocument(request); } } else { response = _flowDocsService.DownloadDocument(request); } var filePath = Path.Combine(path, response.DocumentName); document.DocumentName = filePath; document.Description = response.Description; document.Owner = response.Owner; document.Path = response.Path; document.MimeType = response.MimeType; document.FileHash = response.FileHash; using (var writer = new FileStream(filePath, FileMode.Create)) { int chunks = 0; long curSize = response.ChunkSize; writer.Write(response.DataField, 0, (int)Math.Min(response.ChunkSize, response.FileSize)); request.ChunkNumber = ++chunks; // Get all the chunks of the document while (chunks < response.ChunkTotal) { if (_flowDocsService == null) { using (var docsProxy = new FlowDocsOperations()) { response = docsProxy.DownloadDocument(request); } } else { response = _flowDocsService.DownloadDocument(request); } int size = response.ChunkSize; if (response.FileSize > 0) { long bytesLeft = response.FileSize - curSize; size = bytesLeft < response.ChunkSize ? (int)bytesLeft : response.ChunkSize; } writer.Write(response.DataField, 0, size); var hash = Md5Hash.CreateMd5Hash(response.DataField); if (hash != response.FileHash) { throw new Exception(Flow.Library.Properties.Resources.ER_HASH); } curSize += size; request.ChunkNumber = ++chunks; } } }
public async Task <IActionResult> Download(int id) { DownloadDocumentResponse download = await Mediator.Send(new DownloadDocumentByIdQuery { Id = id }); return(File(download.FileContent, download.ContentType, $"{download.Name}")); }