internal async Task <HandlerResponse <DownloadFile> > GetDownloadFile(Guid applicationId, ApplicationSection section, string pageId, string questionId, string fileName, CancellationToken cancellationToken)
        {
            var page = section.QnAData.Pages.FirstOrDefault(p => p.PageId == pageId);

            if (page == null)
            {
                return(new HandlerResponse <DownloadFile>(success: false, message: $"Page {pageId} in Application {applicationId} does not exist."));
            }

            if (page.Questions.All(q => q.Input.Type != "FileUpload"))
            {
                return(new HandlerResponse <DownloadFile>(success: false, message: $"Page {pageId} in Application {applicationId} does not contain any File Upload questions."));
            }

            if (page.PageOfAnswers == null || !page.PageOfAnswers.Any())
            {
                return(new HandlerResponse <DownloadFile>(success: false, message: $"Page {pageId} in Application {applicationId} does not contain any uploads."));
            }

            var container = await ContainerHelpers.GetContainer(_fileStorageConfig.Value.StorageConnectionString, _fileStorageConfig.Value.ContainerName);

            var questionDirectory = ContainerHelpers.GetDirectory(applicationId, section.SequenceId, section.Id, pageId, questionId, container);
            var pageDirectory     = ContainerHelpers.GetDirectory(applicationId, section.SequenceId, section.Id, pageId, null, container);

            if ((questionId is null))
            {
                return(await PageFiles(applicationId, pageId, cancellationToken, page, pageDirectory));
            }

            if (!(fileName is null))
            {
                return(await SpecifiedFile(applicationId, fileName, pageId, questionId, cancellationToken, page, questionDirectory));
            }

            var blobs = questionDirectory.ListBlobs(useFlatBlobListing: true).ToList();

            if (blobs.Count() == 1)
            {
                var answer = page.PageOfAnswers.SelectMany(poa => poa.Answers).Single(a => a.QuestionId == questionId);
                return(await IndividualFile(answer.Value, cancellationToken, questionDirectory));
            }

            if (!blobs.Any())
            {
                return(new HandlerResponse <DownloadFile>(success: false, message: $"Page {pageId} in Application {applicationId} does not contain any uploads."));
            }

            return(await ZippedMultipleFiles(questionId, cancellationToken, page, questionDirectory));
        }
 private static async Task <CloudBlobContainer> GetContainer(IOptions <FileStorageConfig> config)
 {
     return(await ContainerHelpers.GetContainer(config.Value.StorageConnectionString, config.Value.ContainerName));
 }
Beispiel #3
0
        public async Task <HandlerResponse <bool> > Handle(DeleteFileRequest request, CancellationToken cancellationToken)
        {
            var section = await _dataContext.ApplicationSections.FirstOrDefaultAsync(sec => sec.Id == request.SectionId && sec.ApplicationId == request.ApplicationId, cancellationToken);

            if (section == null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Section {request.SectionId} in Application {request.ApplicationId} does not exist."));
            }

            var qnaData = new QnAData(section.QnAData);
            var page    = qnaData.Pages.FirstOrDefault(p => p.PageId == request.PageId);

            if (page == null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not exist."));
            }

            if (page.Questions.All(q => q.Input.Type != "FileUpload"))
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not contain any File Upload questions."));
            }

            if (page.PageOfAnswers == null || !page.PageOfAnswers.Any())
            {
                return(new HandlerResponse <bool>(success: false, message: $"Page {request.PageId} in Application {request.ApplicationId} does not contain any uploads."));
            }

            var container = await ContainerHelpers.GetContainer(_fileStorageConfig.Value.StorageConnectionString, _fileStorageConfig.Value.ContainerName);

            var directory = ContainerHelpers.GetDirectory(request.ApplicationId, section.SequenceId, request.SectionId, request.PageId, request.QuestionId, container);

            var answer = page.PageOfAnswers.SingleOrDefault(poa => poa.Answers.Any(a => a.QuestionId == request.QuestionId && a.Value == request.FileName));

            if (answer is null)
            {
                return(new HandlerResponse <bool>(success: false, message: $"Question {request.QuestionId} on Page {request.PageId} in Application {request.ApplicationId} does not contain an upload named {request.FileName}."));
            }

            page.PageOfAnswers.Remove(answer);

            if (page.PageOfAnswers.Count == 0)
            {
                page.Complete = false;
                if (page.HasFeedback)
                {
                    foreach (var feedback in page.Feedback.Where(feedback => feedback.IsNew).Select(feedback => feedback))
                    {
                        feedback.IsCompleted = false;
                    }
                }
            }
            else
            {
                var answers = page.PageOfAnswers?.SelectMany(p => p.Answers);
                if (answers != null)
                {
                    var validationErrors = _answerValidator.Validate(answers.ToList(), page);
                    if (validationErrors != null && validationErrors.Any())
                    {
                        page.Complete = false;
                    }
                }
            }

            section.QnAData = qnaData;
            await _dataContext.SaveChangesAsync(cancellationToken);

            var blobRef = directory.GetBlobReference(request.FileName);
            await blobRef.DeleteAsync(cancellationToken);

            await RemoveApplicationDataForThisQuestion(request.ApplicationId, request.QuestionId, page);

            return(new HandlerResponse <bool>(true));
        }