Example #1
0
		public JsonResult Connect() {
			int mpId = -1;

			try {
				new Transactional(() => {
					var serviceInfo = new CompanyFilesServiceInfo();
					var name = serviceInfo.DisplayName;
					var cf = new CompanyFilesDatabaseMarketPlace();
					var mp = this.dbHelper.SaveOrUpdateCustomerMarketplace(this.customer.Name + "_" + name, cf, null, this.customer);
					var rdh = mp.Marketplace.GetRetrieveDataHelper(this.dbHelper);
					rdh.UpdateCustomerMarketplaceFirst(mp.Id);
					mpId = mp.Id;
				}).Execute();

				if (mpId != -1) {
					this.serviceClient.Instance.UpdateMarketplace(this.customer.Id, mpId, true, this.context.UserId);
					this.serviceClient.Instance.MarketplaceInstantUpdate(mpId);
				} // if
			}
			catch (Exception ex) {
				Log.Error(ex, "Error connecting a company files account.");
			} // try

			return Json(new {
				Data = new { success = true },
				JsonRequestBehavior = JsonRequestBehavior.AllowGet,
			});
		} // Connect
        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