Пример #1
0
        public async Task <Result> Handle(SendPageCommand request, CancellationToken token)
        {
            var legal = await _context.Legals
                        .SingleOrDefaultAsync(r => r.Documents.Any(d => d.Id == request.DocumentId), token);

            var document = legal.Documents.FirstOrDefault(d => d.Id == request.DocumentId);

            var page = document.Pages.SingleOrDefault(p => p.Id == request.PageId);

            var downloadResult =
                await _blobService.DownloadDocumentPageAsync(document.Id, page.Id, legal.User.Id, token);

            if (!downloadResult.Succeeded)
            {
                return(Failure(downloadResult));
            }

            var result = await _pspService.AddPageToDocumentAsync(page, document, legal.User.Identifier,
                                                                  downloadResult.Data, token);

            if (!result.Succeeded)
            {
                return(result);
            }

            page.SetUploaded();

            await _context.SaveChangesAsync(token);

            return(result);
        }
Пример #2
0
        public async Task <byte[]> DownloadDocumentAsync(Guid documentId, RequestUser currentUser, CancellationToken token)
        {
            var legal = await _context.Legals
                        .SingleOrDefaultAsync(l => l.Documents.Any(d => d.Id == documentId), token);

            var document = legal.Documents.FirstOrDefault(d => d.Id == documentId);

            if (document == null)
            {
                return(null);
            }

            byte[] archiveFile;
            using (var archiveStream = new MemoryStream())
            {
                using (var archive = new ZipArchive(archiveStream, ZipArchiveMode.Create, true))
                {
                    foreach (var page in document.Pages)
                    {
                        var result = await _blobService.DownloadDocumentPageAsync(document.Id, page.Id, legal.User.Id, token);

                        if (!result.Succeeded)
                        {
                            throw SheaftException.Unexpected(result.Exception, result.Message, result.Params);
                        }

                        var zipArchiveEntry = archive.CreateEntry(page.Filename + page.Extension, CompressionLevel.Optimal);
                        using (var zipStream = zipArchiveEntry.Open())
                            await zipStream.WriteAsync(result.Data, 0, result.Data.Length, token);
                    }
                }

                archiveFile = archiveStream.ToArray();
            }

            return(archiveFile);
        }