Esempio n. 1
0
        public async Task <IActionResult> Upload(IFormFile bulkUploadFile)
        {
            int?UKPRN;

            if (_session.GetInt32("UKPRN") != null)
            {
                UKPRN = _session.GetInt32("UKPRN").Value;
            }
            else
            {
                return(RedirectToAction("Index", "Home", new { errmsg = "Please select a Provider." }));
            }

            BulkUploadViewModel vm = new BulkUploadViewModel();

            string errorMessage;

            // COUR-1986 restoring delete for inline processing to fix issue with course errors accruing on the dashboard DQIs
            // NB: TEST WITH VOLUME!!! - this may cause time outs again
            var deleteResult = await _courseService.DeleteBulkUploadCourses(UKPRN.Value);

            if (!deleteResult.IsSuccess)
            {
                vm.errors = new string[] { deleteResult.Error };
            }
            else
            {
                if (Validate.ValidateFile(bulkUploadFile, out errorMessage))
                {
                    int    providerUKPRN         = UKPRN.Value;
                    string userId                = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                    string bulkUploadFileNewName = string.Format(@"{0}-{1}", DateTime.Now.ToString("yyMMdd-HHmmss"),
                                                                 bulkUploadFile.FileName);

                    MemoryStream ms = new MemoryStream();
                    bulkUploadFile.CopyTo(ms);

                    if (!Validate.IsBinaryStream(ms))
                    {
                        int  csvLineCount  = _bulkUploadService.CountCsvLines(ms);
                        bool processInline = (csvLineCount <= _blobService.InlineProcessingThreshold);
                        _logger.LogInformation(
                            $"Csv line count = {csvLineCount} threshold = {_blobService.InlineProcessingThreshold} processInline = {processInline}");

                        if (processInline)
                        {
                            bulkUploadFileNewName +=
                                "." + DateTime.UtcNow.ToString("yyyyMMddHHmmss") +
                                ".processed"; // stops the Azure trigger from processing the file
                        }

                        Task task = _blobService.UploadFileAsync(
                            $"{UKPRN.ToString()}/Courses Bulk Upload/Files/{bulkUploadFileNewName}", ms);
                        task.Wait();

                        var errors = await _bulkUploadService.ProcessBulkUpload(ms, providerUKPRN, userId, processInline);

                        if (errors.Any())
                        {
                            vm.errors = errors;
                            return(View(vm));
                        }
                        else
                        {
                            if (processInline)
                            {
                                // All good => redirect to BulkCourses action
                                return(RedirectToAction("Index", "PublishCourses",
                                                        new { publishMode = PublishMode.BulkUpload, fromBulkUpload = true }));
                            }
                            else
                            {
                                return(RedirectToAction("Pending"));
                            }
                        }
                    }
                    else
                    {
                        vm.errors = new string[] { "Invalid file content." };
                    }
                }

                else
                {
                    vm.errors = new string[] { errorMessage };
                }
            }
            return(View(vm));
        }