public IActionResult UploadFiles()

in tree/master/cloud/src/solution/Microsoft.Legal.MatterCenter.Web/Controllers/DocumentController.cs [456:596]


        public IActionResult UploadFiles()
        {
            try
            {
                IFormFileCollection fileCollection = Request.Form.Files;
                Regex regEx = new Regex("[*?|\\\t/:\"\"'<>#{}%~&]");
                string clientUrl = Request.Form["clientUrl"];
                string folderUrl = Request.Form["folderUrl"];
                string folderName = folderUrl.Substring(folderUrl.LastIndexOf(ServiceConstants.FORWARD_SLASH, StringComparison.OrdinalIgnoreCase) + 1);
                string documentLibraryName = Request.Form["documentLibraryName"];
                MatterExtraProperties documentExtraProperites=null;
                if (!string.IsNullOrWhiteSpace(Request.Form["DocumentExtraProperties"]))
                {
                     documentExtraProperites = JsonConvert.DeserializeObject<MatterExtraProperties>(Request.Form["DocumentExtraProperties"].ToString());
                }
                bool isDeployedOnAzure = Convert.ToBoolean(generalSettings.IsTenantDeployment, CultureInfo.InvariantCulture);
                
                string originalName = string.Empty;
                bool allowContentCheck = false;
                bool.TryParse(Request.Form["AllowContentCheck"], out allowContentCheck);
                Int16 isOverwrite = 3;     
                //Input validation           
                #region Error Checking                
                GenericResponseVM genericResponse = null;
                IList<object> listResponse = new List<object>();
                bool continueUpload = true;
                if (isDeployedOnAzure == false && string.IsNullOrWhiteSpace(clientUrl) && string.IsNullOrWhiteSpace(folderUrl))
                {
                    genericResponse = new GenericResponseVM()
                    {
                        Value = errorSettings.MessageNoInputs,
                        Code = HttpStatusCode.BadRequest.ToString(),
                        IsError = true
                    };
                    return matterCenterServiceFunctions.ServiceResponse(genericResponse, (int)HttpStatusCode.OK);
                }
                #endregion
                //Get all the files which are uploaded by the user
                for (int fileCounter = 0; fileCounter < fileCollection.Count; fileCounter++)
                {
                    IFormFile uploadedFile = fileCollection[fileCounter];
                    if (!Int16.TryParse(Request.Form["Overwrite" + fileCounter], out isOverwrite))
                    {
                        isOverwrite = 3;
                    }
                    continueUpload = true;
                    ContentDispositionHeaderValue fileMetadata = ContentDispositionHeaderValue.Parse(uploadedFile.ContentDisposition);
                    string fileName = originalName = fileMetadata.FileName.Trim('"');
                    fileName = System.IO.Path.GetFileName(fileName);
                    ContentCheckDetails contentCheckDetails = new ContentCheckDetails(fileName, uploadedFile.Length);
                    string fileExtension = System.IO.Path.GetExtension(fileName).Trim();
                    if (-1 < fileName.IndexOf('\\'))
                    {
                        fileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
                    }
                    else if (-1 < fileName.IndexOf('/'))
                    {
                        fileName = fileName.Substring(fileName.LastIndexOf('/') + 1);
                    }
                    if (null != uploadedFile.OpenReadStream() && 0 == uploadedFile.OpenReadStream().Length)
                    {
                        listResponse.Add(new GenericResponseVM() { Code = fileName, Value = errorSettings.ErrorEmptyFile, IsError = true });
                    }
                    else if (regEx.IsMatch(fileName))
                    {
                        listResponse.Add(new GenericResponseVM() { Code = fileName, Value = errorSettings.ErrorInvalidCharacter, IsError = true });
                    }
                    else
                    {
                        string folder = folderUrl.Substring(folderUrl.LastIndexOf(ServiceConstants.FORWARD_SLASH, StringComparison.OrdinalIgnoreCase) + 1);
                        //If User presses "Perform content check" option in overwrite Popup
                        if (2 == isOverwrite)   
                        {                            
                            genericResponse = documentProvision.PerformContentCheck(clientUrl, folderUrl, uploadedFile, fileName);
                        }
                        //If user presses "Cancel upload" option in overwrite popup or file is being uploaded for the first time
                        else if (3 == isOverwrite)  
                        {
                            genericResponse = documentProvision.CheckDuplicateDocument(clientUrl, folderUrl, documentLibraryName, fileName, contentCheckDetails, allowContentCheck);
                        }
                        //If User presses "Append date to file name and save" option in overwrite Popup
                        else if (1 == isOverwrite)  
                        {
                            string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(fileName);
                            string timeStampSuffix = DateTime.Now.ToString(documentSettings.TimeStampFormat, CultureInfo.InvariantCulture).Replace(":", "_");
                            fileName = fileNameWithoutExt + "_" + timeStampSuffix + fileExtension;
                        }
                        if(genericResponse==null)
                        {
                            genericResponse = documentProvision.UploadFiles(uploadedFile, fileExtension, originalName, folderUrl, fileName,
                                clientUrl, folder, documentLibraryName, documentExtraProperites);
                        }
                        if (genericResponse == null)
                        {
                            string documentIconUrl = string.Empty;
                            fileExtension = fileExtension.Replace(".", "");
                            if (fileExtension.ToLower() != "pdf")
                            {
                                documentIconUrl = $"{generalSettings.SiteURL}/_layouts/15/images/ic{fileExtension}.gif";
                            }
                            else
                            {
                                documentIconUrl = $"{generalSettings.SiteURL}/_layouts/15/images/ic{fileExtension}.png";
                            }
                            //Create a json object with file upload success
                            var successFile = new
                            {
                                IsError = false,
                                Code = HttpStatusCode.OK.ToString(),
                                Value = UploadEnums.UploadSuccess.ToString(),
                                FileName = fileName,
                                DropFolder = folderName,
                                DocumentIconUrl = documentIconUrl
                            };
                            listResponse.Add(successFile);
                        }
                        else
                        {
                            //Create a json object with file upload failure
                            var errorFile = new
                            {
                                IsError = true,
                                Code = genericResponse.Code.ToString(),
                                Value = genericResponse.Value.ToString(),
                                FileName = fileName,
                                DropFolder = folderName
                            };
                            listResponse.Add(errorFile);                           
                        }                   
                    }
                }
                //Return the response with proper http status code and proper response object     
                return matterCenterServiceFunctions.ServiceResponse(listResponse, (int)HttpStatusCode.OK);
                
            }
            catch (Exception ex)
            {
                customLogger.LogError(ex, MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name, logTables.SPOLogTable);
                throw;
            }
        }