public ActionResult BankUploadedFiles() { Response.AddHeader("x-frame-options", "SAMEORIGIN"); OneUploadLimitation oLimitations = CurrentValues.Instance.GetUploadLimitations("CompanyFilesMarketPlaces", "UploadedFiles"); for (int i = 0; i < Request.Files.Count; ++i) { HttpPostedFileBase file = Request.Files[i]; if (file != null) { var content = new byte[file.ContentLength]; int nRead = file.InputStream.Read(content, 0, file.ContentLength); if (nRead != file.ContentLength) { Log.Warn("File {0}: failed to read entire file contents, ignoring.", i); continue; } // if string sMimeType = oLimitations.DetectFileMimeType(content, file.FileName, oLog: Log); if (string.IsNullOrWhiteSpace(sMimeType)) { Log.Warn("Not saving file #{0}: {1} because it has unsupported MIME type.", (i + 1), file.FileName); continue; } // if this.serviceClient.Instance.CompanyFilesUpload(this.customer.Id, OneUploadLimitation.FixFileName(file.FileName), content, sMimeType, true); } // if } // for return Json(new { }); } // BankUploadedFiles
static void Main(string[] args) { ASafeLog oLog = new ConsoleLog(new FileLog("VerifyUploadedFiles")); var oErrors = new SortedDictionary <string, string>(); var oPassed = new SortedDictionary <string, string>(); var oLimits = new OneUploadLimitation(10000000, "image/*,application/pdf,.doc,.docx,.odt,.rtf,.ppt,.pptx,.odp,.xls,.xlsx,.ods,.txt,.csv,.xml,.htm,.html,.xhtml,.mht,.msg,.eml"); oLog.Info("File limitations are: {0}", oLimits); ProcessDir(oLimits, args.Length > 0 ? args[0] : Directory.GetCurrentDirectory(), oPassed, oErrors, oLog); oLog.Info("Passed files - begin:"); foreach (KeyValuePair <string, string> pair in oPassed) { oLog.Info("\t{0}: {1}", pair.Key, pair.Value); } oLog.Info("Passed files - end."); oLog.Info("Failed files - begin:"); foreach (KeyValuePair <string, string> pair in oErrors) { oLog.Info("\t{0}: {1}", pair.Key, pair.Value); } oLog.Info("Failed files - end."); } // Main
public void UploadDoc(string description, int customerId) { var files = Request.Files; if (files.Count == 0) { string sError = string.Format("No files received for customer {0}.", customerId); Log.Debug("{0}", sError); throw new Exception(sError); } // if OneUploadLimitation oLimitations = CurrentValues.Instance.GetUploadLimitations("AlertDocsController", "UploadDoc"); var oErrors = new List <string>(); for (int i = 0; i < files.Count; i++) { var file = Request.Files[i]; if (file == null) { string sError = string.Format("File #{0} for customer {1} is null.", i, customerId); Log.Debug("{0}", sError); oErrors.Add(sError); continue; } // if var body = new byte[file.InputStream.Length]; file.InputStream.Read(body, 0, file.ContentLength); if (string.IsNullOrWhiteSpace(oLimitations.DetectFileMimeType(body, file.FileName))) { string sError = string.Format("File #{0} for customer {1} cannot be accepted due to its MIME type.", i, customerId); Log.Debug("{0}", sError); oErrors.Add(sError); continue; } // if var customerRepo = ObjectFactory.GetInstance <CustomerRepository>(); var customer = customerRepo.Get(customerId); var doc = new MP_AlertDocument { BinaryBody = body, Customer = customer, Employee = _context.User, Description = description, UploadDate = DateTime.UtcNow, DocName = file.FileName }; _docRepo.SaveOrUpdate(doc); } if (oErrors.Count > 0) { throw new Exception(string.Join(" ", oErrors)); } }
} // ProcessFile private static void ProcessFile( OneUploadLimitation oLimits, string sFileName, SortedDictionary <string, string> oPassed, SortedDictionary <string, string> oErrors, ASafeLog oLog ) { try { oLog.Info("Examining file {0}...", sFileName); var fi = new FileInfo(sFileName); var os = new List <string>(); if (fi.Length > oLimits.FileSize) { os.Add("file size " + fi.Length.ToString("N0", CultureInfo.InvariantCulture)); } var oBuf = new byte[256]; FileStream ins = File.OpenRead(sFileName); var nRead = ins.Read(oBuf, 0, oBuf.Length); ins.Close(); string sMimeType = oLimits.DetectFileMimeType(oBuf, sFileName, nRead, oLog); if (string.IsNullOrWhiteSpace(sMimeType)) { os.Add("MIME type"); } string sMsg; if (os.Count > 0) { sMsg = string.Join(" and ", os); oErrors[sFileName] = sMsg; } else { sMsg = "size " + fi.Length.ToString("N0", CultureInfo.InvariantCulture) + " bytes of type " + sMimeType; oPassed[sFileName] = sMsg; } oLog.Info("File {0} has {1}.", sFileName, os.Count == 0 ? "passed with " + sMsg : "failed due to " + sMsg); } catch (Exception e) { oLog.Alert(e, "Failed to process file {0}", sFileName); } // try } // ProcessFile
} // IsDir private static void ProcessDir(OneUploadLimitation oLimits, string sPath, SortedDictionary <string, string> oPassed, SortedDictionary <string, string> oErrors, ASafeLog oLog) { IEnumerable <string> lst; try { lst = Directory.EnumerateFileSystemEntries(sPath); } catch (Exception e) { oLog.Alert(e, "Failed to enumerate objects in {0}.", sPath); return; } // try foreach (string sFileName in lst) { if (IsDir(sFileName)) { ProcessDir(oLimits, sFileName, oPassed, oErrors, oLog); } else { ProcessFile(oLimits, sFileName, oPassed, oErrors, oLog); } } // for each } // ProcessFile
public JsonResult HandleUploadFile() { string sContactEmail = Request.Headers["ezbob-broker-contact-email"]; string sCustomerID = Request.Headers["ezbob-broker-customer-id"]; ms_oLog.Debug( "Broker upload customer file request for customer {1} and contact email {0}", sContactEmail, sCustomerID ); BrokerForJsonResult oIsAuthResult = IsAuth("Upload customer file for customer " + sCustomerID, sContactEmail); if (oIsAuthResult != null) { return(oIsAuthResult); } var nFileCount = Request.Files.Count; var oErrorList = new List <string>(); OneUploadLimitation oLimitations = CurrentValues.Instance.GetUploadLimitations("BrokerHome", "HandleUploadFile"); for (int i = 0; i < nFileCount; i++) { HttpPostedFileBase oFile = Request.Files[i]; if (oFile == null) { ms_oLog.Alert("File object #{0} out of {1} is null.", (i + 1), nFileCount); oErrorList.Add("Failed to upload file #" + (i + 1)); continue; } // if var oFileContents = new byte[oFile.InputStream.Length]; int nRead = oFile.InputStream.Read(oFileContents, 0, oFile.ContentLength); if (nRead != oFile.ContentLength) { oErrorList.Add("Failed to fully file #" + (i + 1) + ": " + oFile.FileName); ms_oLog.Alert( "Failed to fully read file #{0}: {2} out of {1}; only {3} bytes out of {4} have been read.", (i + 1), nFileCount, oFile.FileName, nRead, oFile.ContentLength ); continue; } // if string sMimeType = oLimitations.DetectFileMimeType(oFileContents, oFile.FileName, oLog: ms_oLog); ms_oLog.Debug( "File #{0} out of {1}: {2}; file size is {3} bytes, detected MIME type: {4}", (i + 1), nFileCount, oFile.FileName, nRead, sMimeType ); if (string.IsNullOrWhiteSpace(sMimeType)) { oErrorList.Add( "Not saving file #" + (i + 1) + ": " + oFile.FileName + " because it has unsupported MIME type." ); } else { try { this.m_oServiceClient.Instance.BrokerSaveUploadedCustomerFile( sCustomerID, sContactEmail, oFileContents, oFile.FileName, UiOrigin ); } catch (Exception e) { ms_oLog.Alert(e, "Failed to save file #{0}: {2} out of {1}.", (i + 1), nFileCount, oFile.FileName); oErrorList.Add("Failed to save file #" + (i + 1) + ": " + oFile.FileName); } // try } // if } // for each file ms_oLog.Debug( "Broker upload customer file request for customer {1} and contact email {0} complete.", sContactEmail, sCustomerID ); return(new BrokerForJsonResult(oErrorList.Count == 0 ? string.Empty : string.Join(" ", oErrorList))); } // HandleUploadFile
public ActionResult UploadFile() { Response.AddHeader("x-frame-options", "SAMEORIGIN"); int customerId; if (!int.TryParse(Request.Headers["ezbob-underwriter-customer-id"], out customerId)) { return(Json(new { error = "Failed to upload files: customer id header is missing." })); } OneUploadLimitation oLimitations = CurrentValues.Instance.GetUploadLimitations("CompanyFilesMarketPlaces", "UploadedFiles"); var customer = _customers.Get(customerId); try { new Transactional(() => { var serviceInfo = new CompanyFilesServiceInfo(); var name = serviceInfo.DisplayName; var cf = new CompanyFilesDatabaseMarketPlace(); var mp = _helper.SaveOrUpdateCustomerMarketplace(customer.Name + "_" + name, cf, null, customer); var rdh = mp.Marketplace.GetRetrieveDataHelper(_helper); rdh.UpdateCustomerMarketplaceFirst(mp.Id); //nResult = mp.Id; }).Execute(); } catch (Exception ex) { Log.Error(ex, "Failed to create a company files marketplace for customer {0}.", customer.Stringify()); return(Json(new { error = ex.Message })); } var error = new StringBuilder(); for (int i = 0; i < Request.Files.Count; ++i) { HttpPostedFileBase file = Request.Files[i]; if (file != null) { var content = new byte[file.ContentLength]; int nRead = file.InputStream.Read(content, 0, file.ContentLength); if (nRead != file.ContentLength) { Log.Warn("File {0}: failed to read entire file contents, ignoring.", i); error.AppendLine("File ").Append(file.FileName).Append(" failed to read entire file contents, ignoring."); continue; } // if string sMimeType = oLimitations.DetectFileMimeType(content, file.FileName, oLog: new SafeILog(Log)); if (string.IsNullOrWhiteSpace(sMimeType)) { Log.Warn("Not saving file #" + (i + 1) + ": " + file.FileName + " because it has unsupported MIME type."); error.AppendLine("Not saving file ").Append(file.FileName).Append(" because it has unsupported MIME type."); continue; } // if m_oServiceClient.Instance.CompanyFilesUpload(customerId, file.FileName, content, file.ContentType, true); } } return(error.Length > 0 ? Json(new { error = error.ToString() }) : Json(new { success = true })); } // UploadFile
} // constructor public void Run() { FileCache.ErrorMsg = string.Empty; for (int i = 0; i < FileList.Count; i++) { HttpPostedFileBase oFile = FileList[i]; if (oFile == null) { ms_oLog.Debug("File {0}: not found, ignoring.", i); continue; } // if ms_oLog.Debug("File {0}, name: {1}", i, oFile.FileName); if (oFile.ContentLength == 0) { ms_oLog.Debug("File {0}: is empty, ignoring.", i); continue; } // if var oFileContents = new byte[oFile.ContentLength]; int nRead = oFile.InputStream.Read(oFileContents, 0, oFile.ContentLength); if (nRead != oFile.ContentLength) { ms_oLog.Warn("File {0}: failed to read entire file contents, ignoring.", i); continue; } // if string sMimeType = m_oLimitations.DetectFileMimeType(oFileContents, oFile.FileName, oLog: ms_oLog); ms_oLog.Debug("File {0}, name: {1}, MIME type {2}", i, oFile.FileName, sMimeType); if (string.IsNullOrWhiteSpace(sMimeType)) { ms_oLog.Debug("File {0}: has unsupported content type, ignoring.", i); continue; } // if SaveToDisc(CustomerID, OneUploadLimitation.FixFileName(oFile.FileName), oFileContents); var smd = new SheafMetaData { BaseFileName = OneUploadLimitation.FixFileName(oFile.FileName), DataType = DataType.VatReturn, FileType = FileType.Pdf, Thrasher = null }; FileCache.Add(smd, oFileContents); var vrpt = new VatReturnPdfThrasher(false, ms_oLog); ISeeds oResult; try { oResult = vrpt.Run(smd, oFileContents); } catch (Exception e) { ms_oLog.Warn(e, "Failed to parse file {0} named {1}:", i, oFile.FileName); continue; } // try if (oResult == null) { ErrorMsg = m_sErrorMsg + " " + ((VatReturnSeeds)vrpt.Seeds).FatalError; continue; } // if var oSeeds = (VatReturnSeeds)oResult; var di = new DateInterval(oSeeds.DateFrom, oSeeds.DateTo); ms_oLog.Debug("HMRC file cache state before adding file {0}: {1}.", oFile.FileName, FileCache); if (FileCache.Intersects(oSeeds.RegistrationNo, di)) { return; } FileCache.Add(oSeeds.RegistrationNo, di, smd, oResult); ms_oLog.Debug("HMRC file cache state after adding file {0}: {1}.", oFile.FileName, FileCache); } // for } // Run
} // constructor public JsonResult SaveUploadedFiles( HttpFileCollectionBase oFiles, int nCustomerID, string sControllerName, string sActionName ) { Customer oCustomer = this.customers.ReallyTryGet(nCustomerID); if (oCustomer == null) { if (this.hideRealError) { log.Warn("Could not retrieve customer by id {0}.", nCustomerID); return(CreateJsonError("Could not retrieve customer by requested id.")); } // if return(CreateJsonError("Could not retrieve customer by id {0}.", nCustomerID)); } // if var oProcessor = new HmrcFileProcessor(nCustomerID, oFiles, sControllerName, sActionName); oProcessor.Run(); if (!string.IsNullOrWhiteSpace(oProcessor.ErrorMsg) || oProcessor.AddedCount < 1) { //return CreateJsonError(oProcessor.ErrorMsg); OneUploadLimitation oLimitations = CurrentValues.Instance.GetUploadLimitations("CompanyFilesMarketPlaces", "UploadedFiles"); for (int i = 0; i < oFiles.Count; ++i) { HttpPostedFileBase file = oFiles[i]; if (file != null) { var content = new byte[file.ContentLength]; file.InputStream.Seek(0, SeekOrigin.Begin); int nRead = file.InputStream.Read(content, 0, file.ContentLength); if (nRead != file.ContentLength) { log.Warn("File {0}: failed to read entire file contents, ignoring.", i); continue; } // if string sMimeType = oLimitations.DetectFileMimeType(content, file.FileName, oLog: log); if (string.IsNullOrWhiteSpace(sMimeType)) { log.Warn("Not saving file #{0}: {1} because it has unsupported MIME type.", (i + 1), file.FileName); continue; } // if this.serviceClient.Instance.CompanyFilesUpload(oCustomer.Id, OneUploadLimitation.FixFileName(file.FileName), content, sMimeType, false); } // if } var companyFiles = ObjectFactory.GetInstance <CompanyFilesMarketPlacesController>(); companyFiles.Connect(); return(CreateJsonNoError()); } //if (oProcessor.AddedCount < 1) // return CreateJsonError("No files were accepted."); return(DoSave( oProcessor.Hopper, string.Format("Failed to upload VAT return file{0}.", oProcessor.AddedCount == 1 ? "" : "s"), oCustomer )); } // SaveUploadedFiles