Beispiel #1
0
        public async Task <IActionResult> Get(Owner.Type ownerType, int ownerId)
        {
            Attachment attachment = null;

            switch (ownerType)
            {
            case Owner.Type.Request:
                var request = await _executor.GetQuery <GetRequestByIdQuery>().Process(q => q.ExecuteAsync(ownerId));

                attachment = request?.MainAttachment;
                break;

            case Owner.Type.Contract:
                var contract = await _executor.GetQuery <GetContractByIdQuery>().Process(q => q.ExecuteAsync(ownerId));

                attachment = contract?.MainAttachment;
                break;

            case Owner.Type.ProtectionDoc:
                var protectionDoc = await _executor.GetQuery <GetProtectionDocByIdQuery>().Process(q => q.ExecuteAsync(ownerId));

                attachment = protectionDoc?.MainAttachment;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(ownerType), ownerType, null);
            }

            if (attachment == null)
            {
                var documentName = ownerType == Owner.Type.Request ? nameof(Request) : nameof(Document);
                throw new DataNotFoundException(documentName, DataNotFoundException.OperationType.Read,
                                                ownerId);
            }

            var fileContent = await _fileStorage.GetAsync(attachment.BucketName, attachment.OriginalName);

            var extention = Path.GetExtension(attachment.OriginalName);

            if (extention != null && extention.ToLower().Contains("odt"))
            {
                using (var memoryStream = new MemoryStream())
                {
                    memoryStream.Write(fileContent, 0, fileContent.Length);
                    memoryStream.Seek(0, SeekOrigin.Begin);
                    var contentType = attachment.ContentType;

                    var newName = attachment.ValidName.Replace(".odt", ".pdf");
                    var pdf     = _fileConverter.DocxToPdf(memoryStream, newName);
                    return(File(pdf.File, contentType, newName));
                }
            }

            return(File(fileContent, attachment.ContentType, attachment.ValidName));
        }
Beispiel #2
0
        public async Task <IActionResult> Get(int id, bool wasScanned, bool isMain)
        {
            byte[] file;
            var    contentType = string.Empty;
            var    validName   = string.Empty;
            //todo: Почему не работает ProcessAsync?
            var document = await _executor.GetQuery <GetDocumentByIdQuery>().Process(q => q.ExecuteAsync(id));

            if (document == null)
            {
                throw new DataNotFoundException(nameof(Domain.Entities.Document.Document), DataNotFoundException.OperationType.Read, id);
            }

            var input = await _executor.GetQuery <GetDocumentUserInputByDocumentIdQuery>().Process(q => q.ExecuteAsync(document.Id));

            if (input != null && !wasScanned)
            {
                var dto = JsonConvert.DeserializeObject <UserInputDto>(input.UserInput);

                var documentGenerator = _templateGeneratorFactory.Create(dto.Code);
                var generatedFile     = documentGenerator.Process(new Dictionary <string, object>
                {
                    { "UserId", NiisAmbientContext.Current.User.Identity.UserId },
                    { "RequestId", dto.OwnerId },
                    { "DocumentId", id },
                    { "UserInputFields", dto.Fields },
                    { "SelectedRequestIds", dto.SelectedRequestIds },
                    { "PageCount", dto.PageCount },
                    { "OwnerType", dto.OwnerType },
                    { "Index", dto.Index }
                });
                file        = generatedFile.File;
                contentType = ContentType.Pdf;
                var typeNameRu = document?.Type?.NameRu;

                if (document.Type.Code == DicDocumentTypeCodes.NotificationOfRegistrationDecision && !string.IsNullOrEmpty(document.OutgoingNumber))
                {
                    var documentLinksIds = new List <int>();
                    documentLinksIds = document.DocumentLinks.Where(d => d.ChildDocument.Type.Code == DicDocumentTypeCodes.ExpertTmRegisterOpinion).Select(d => d.ChildDocumentId).ToList();
                    documentLinksIds.AddRange(document.DocumentParentLinks.Where(d => d.ParentDocument.Type.Code == DicDocumentTypeCodes.ExpertTmRegisterOpinion).Select(d => d.ParentDocumentId).ToList().Where(d => !documentLinksIds.Contains(d)));

                    var linkFiles = new List <byte[]>();
                    linkFiles.Add(file);

                    foreach (var documentLinksId in documentLinksIds)
                    {
                        var linkFile = await GetFileByte(documentLinksId);

                        if (linkFile == null)
                        {
                            continue;
                        }
                        else
                        {
                            linkFiles.Add(linkFile);
                        }
                    }

                    if (linkFiles.Count > 0)
                    {
                        var result = MergeFile(linkFiles);

                        return(string.IsNullOrWhiteSpace(typeNameRu)
                           ? File(result, contentType)
                           : File(result, contentType, typeNameRu));
                    }
                }

                return(string.IsNullOrWhiteSpace(typeNameRu)
                    ? File(file, contentType)
                    : File(file, contentType, typeNameRu));
            }

            Attachment attachment = null;

            if (isMain && document.MainAttachment != null)
            {
                attachment  = document.MainAttachment;
                contentType = document.MainAttachment.ContentType;
                validName   = document.MainAttachment.ValidName;
            }
            else if (document.AdditionalAttachments.Any(d => d.IsMain == false))
            {
                attachment  = document.AdditionalAttachments.FirstOrDefault(d => d.IsMain == false);
                contentType = attachment?.ContentType;
                validName   = attachment?.ValidName;
            }

            if (attachment != null)
            {
                var fileContent = await _fileStorage.GetAsync(attachment.BucketName, attachment.OriginalName);

                var extention = Path.GetExtension(attachment.OriginalName);

                if (extention != null && extention.ToLower().Contains("odt"))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        memoryStream.Write(fileContent, 0, fileContent.Length);
                        memoryStream.Seek(0, SeekOrigin.Begin);

                        var newName = validName.Replace(".odt", ".pdf");
                        var pdf     = _fileConverter.DocxToPdf(memoryStream, newName);
                        return(File(pdf.File, contentType, newName));
                    }
                }

                file = fileContent;
                return(File(file, contentType, validName));
            }

            return(null);
        }