Ejemplo n.º 1
0
        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
Ejemplo n.º 2
0
        public FileResult DownloadCustomerFile(string sCustomerID, string sContactEmail, int nFileID)
        {
            ms_oLog.Debug(
                "Broker download customer file request for customer {1} and contact email {0} with file id {2}",
                sContactEmail,
                sCustomerID,
                nFileID
                );

            BrokerForJsonResult oIsAuthResult = IsAuth("Download customer file for customer " + sCustomerID, sContactEmail);

            if (oIsAuthResult != null)
            {
                throw new Exception(oIsAuthResult.error);
            }

            BrokerCustomerFileContentsActionResult oFile;

            try {
                oFile = this.m_oServiceClient.Instance.BrokerDownloadCustomerFile(
                    sCustomerID,
                    sContactEmail,
                    nFileID,
                    UiOrigin
                    );
            } catch (Exception e) {
                ms_oLog.Alert(
                    e,
                    "Failed to download customer file for customer {1} and contact email {0} with file id {2}",
                    sContactEmail,
                    sCustomerID,
                    nFileID
                    );
                throw new Exception("Failed to download requested file.");
            }             // try

            if (string.IsNullOrWhiteSpace(oFile.Name))
            {
                ms_oLog.Alert(
                    "Could not download customer file for customer {1} and contact email {0} with file id {2}",
                    sContactEmail,
                    sCustomerID,
                    nFileID
                    );
                throw new Exception("Failed to download requested file.");
            }             // if

            ms_oLog.Debug(
                "Broker download customer file request for customer {1} and contact email {0} with file id {2} complete.",
                sContactEmail,
                sCustomerID,
                nFileID
                );

            string sFileExt = string.Empty;

            int nLastDotPos = oFile.Name.LastIndexOf('.');

            if ((nLastDotPos > -1) && (nLastDotPos < oFile.Name.Length - 1))
            {
                sFileExt = oFile.Name.Substring(nLastDotPos);
            }

            return(new FileContentResult(oFile.Contents, new MimeTypeResolver()[sFileExt])
            {
                FileDownloadName = oFile.Name,
            });
        }         // DownloadCustomerFile
Ejemplo n.º 3
0
        public JsonResult SaveCrmEntry(
            string type,
            int action,
            int status,
            string comment,
            string customerId,
            string sContactEmail
            )
        {
            ms_oLog.Debug(
                "\nBroker saving CRM entry started:" +
                "\n\ttype: {0}" +
                "\n\taction: {1}" +
                "\n\tstatus: {2}" +
                "\n\tcustomer id: {3}" +
                "\n\tcontact email: {4}" +
                "\n\tcomment: {5}\n",
                type, action, status, customerId, sContactEmail, comment
                );

            BrokerForJsonResult oIsAuthResult = IsAuth("Save CRM entry for customer " + customerId, sContactEmail);

            if (oIsAuthResult != null)
            {
                return(oIsAuthResult);
            }

            StringActionResult oResult;

            try {
                oResult = this.m_oServiceClient.Instance.BrokerSaveCrmEntry(
                    type,
                    action,
                    status,
                    comment,
                    customerId,
                    sContactEmail,
                    UiOrigin
                    );
            } catch (Exception e) {
                ms_oLog.Alert(e,
                              "\nBroker saving CRM entry failed for:" +
                              "\n\ttype: {0}" +
                              "\n\taction: {1}" +
                              "\n\tstatus: {2}" +
                              "\n\tcustomer id: {3}" +
                              "\n\tcontact email: {4}",
                              type, action, status, customerId, sContactEmail
                              );

                return(new BrokerForJsonResult("Failed to save CRM entry."));
            }             // try

            ms_oLog.Debug(
                "\nBroker saving CRM entry {5} for:" +
                "\n\ttype: {0}" +
                "\n\taction: {1}" +
                "\n\tstatus: {2}" +
                "\n\tcustomer id: {3}" +
                "\n\tcontact email: {4}\n" +
                "\n\terror message: {6}\n",
                type, action, status, customerId, sContactEmail,
                string.IsNullOrWhiteSpace(oResult.Value) ? "complete" : "failed",
                string.IsNullOrWhiteSpace(oResult.Value) ? "no error" : oResult.Value
                );

            return(new BrokerForJsonResult(oResult.Value));
        }         // SaveCrmEntry