예제 #1
0
        public IHttpActionResult AddNewReport()
        {
            string       msg    = String.Empty;
            int          result = 0;
            ActionStatus status = new ActionStatus();

            try
            {
                HttpRequest httpRequest = HttpContext.Current.Request;

                string reportDetailsDyn = httpRequest.Unvalidated["ReportDetail"];
                List <ReportDetail> reportDetails = new JavaScriptSerializer {
                }.Deserialize <List <ReportDetail> >(reportDetailsDyn);
                Validator.ValidateReportDetail(reportDetails);                                                 //to do =>FluentValidation of POST api call having multiple/formData. link=>http://blog.marcinbudny.com/2014/02/sending-binary-data-along-with-rest-api.html
                if (reportDetails.Count != httpRequest.Files.Count)
                {
                    logger.Info("Count mismatch in Report Details and File Count");
                    throw new CountMismatchException();
                }
                else
                {
                    List <ReportInformation> reportInformation = new List <ReportInformation>();

                    for (int i = 0; i < httpRequest.Files.Count; i++)
                    {
                        if (Util.IsPPT(httpRequest.Files[i].ContentType))
                        {
                            string filename = httpRequest.Files[i].FileName;


                            if (Regex.IsMatch(filename, @"^[a-zA-Z0-9\s.\?\,\;\:\&\!\-\\_]+$"))   //Filename Regex
                            {
                                if (httpRequest.Files[i].ContentLength <= Util.MaxFileSize)
                                {
                                    using (var binaryReader = new BinaryReader(httpRequest.Files[i].InputStream))
                                    {
                                        using (var filestream = new MemoryStream(binaryReader.ReadBytes(httpRequest.Files[i].ContentLength)))
                                        {
                                            reportInformation.Add(new ReportInformation
                                            {
                                                ReportDetail = reportDetails[i],
                                                FileStream   = filestream //to do==>close memorystream
                                            });

                                            if (i == httpRequest.Files.Count - 1)
                                            {
                                                result = reportService.AddNewReport(reportInformation, permission);
                                            }
                                        }
                                    }
                                }

                                else
                                {
                                    logger.Info("File:{0} excceeds maximum threshold of 10Mb", httpRequest.Files[i].FileName);
                                    throw new InvalidFileSizeException();
                                }
                            }
                            else
                            {
                                logger.Info("Invalid File Name of :{0} ", httpRequest.Files[i].FileName);
                                throw new FileNameNotValidException();
                            }
                        }
                        else
                        {
                            logger.Info("Invalid File format of :{0}", httpRequest.Files[i].FileName);
                            throw new FormatNotSupportedException();
                        }
                    }
                }
            }
            catch (ReportServiceException ex)
            {
                status.Number = (int)ex.ErrorCodeService;
            }
            catch (BaseException ex)
            {
                status.Number = (int)ex.ErrorCode;
            }
            catch (Exception ex)
            {
                status.Number = -1;
                logger.Error("Exception in Report/addNewReport: {0} \r\n {1}", ex.ToString(), ex.StackTrace);
            }
            if (status.Number != -1)
            {
                return(Ok(new { result = result, Status = status }));
            }
            else
            {
                return(InternalServerError());
            }
        }