Beispiel #1
0
        //================================================================================
        /// <summary>
        /// 根据主键ID获取通话录音信息
        /// </summary>
        private HttpResponseMessage GetCallVoiceByID(HttpRequestMessage request)
        {
            log.Debug(Constant.DEBUG_START);

            string sign = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_SIGN);
            string cmd = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_CMD);
            string random = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_RANDOM);

            string idString = ApiQueryUtil.QueryArgByGet("id");

            Dictionary<string, string> args = new Dictionary<string, string>()
            {
                { Constant.HTTP_HEADER_CMD, cmd},
                { Constant.HTTP_HEADER_RANDOM, random},
                { "id", idString }
            }.OrderBy(element => element.Key).ToDictionary(o => o.Key, p => p.Value);

            ServiceInvokeDTO<CallVoiceDTO> result = null;
            try
            {
                // Check sign
                if (securityService.CheckSign(args, Config.ApiSignSecretKey, sign))
                {
                    ServiceInvokeDTO<CallVoice> getResult = userDataService.GetCallVoiceByID(Convert.ToInt32(idString));

                    // Model --> DTO
                    if (getResult != null && getResult.Code == InvokeCode.SYS_INVOKE_SUCCESS && getResult.Data != null)
                    {
                        CallVoiceDTO dto = new CallVoiceDTO(getResult.Data);
                        dto.DownloadUrl = string.Format(Constant.CALLVOICE_DOWNLOAD_FORMAT, request.Headers.Host, getResult.Data.FileSaveName);

                        result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_INVOKE_SUCCESS, dto);
                    }
                    else
                    {
                        result = new ServiceInvokeDTO<CallVoiceDTO>(getResult.Code);
                    }
                }
                else
                {
                    result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_SIGN_ERROR);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_INNER_ERROR);
            }
            log.Debug(Constant.DEBUG_END);

            return request.CreateResponse(HttpStatusCode.OK, result);
        }
Beispiel #2
0
        /// <summary>
        /// 上传通话录音文件
        /// </summary>
        private HttpResponseMessage AddCallVoice(HttpRequestMessage request)
        {
            log.Debug(Constant.DEBUG_START);

            string sign = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_SIGN);
            string cmd = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_CMD);
            string random = ApiQueryUtil.QueryHeader(Constant.HTTP_HEADER_RANDOM);

            string callIDString = ApiQueryUtil.QueryArgByPost("call_id");
            HttpPostedFile voiceFile = HttpContext.Current.Request.Files["file"];

            Dictionary<string, string> args = new Dictionary<string, string>()
            {
                { Constant.HTTP_HEADER_CMD, cmd },
                { Constant.HTTP_HEADER_RANDOM, random },
                { "call_id", callIDString }
            }.OrderBy(element => element.Key).ToDictionary(o => o.Key, p => p.Value);

            ServiceInvokeDTO<CallVoiceDTO> result = null;
            try
            {
                // Check sign
                if (securityService.CheckSign(args, Config.ApiSignSecretKey, sign))
                {
                    if (voiceFile != null)
                    {
                        // 删除已有数据和文件
                        int callID = Convert.ToInt32(callIDString);
                        ServiceInvokeDTO<CallVoice> oldCallVoiceDTO = userDataService.GetCallVoiceByCallRecordID(callID);
                        if (oldCallVoiceDTO != null && oldCallVoiceDTO.Code == InvokeCode.SYS_INVOKE_SUCCESS && oldCallVoiceDTO.Data != null)
                        {
                            userDataService.DeleteCallVoiceByCallRecordID(callID);
                            string oldFilePath = HttpContext.Current.Request.MapPath(string.Format("~/Files/CallVoices/{0}", oldCallVoiceDTO.Data.FileSaveName));
                            if (System.IO.File.Exists(oldFilePath))
                            {
                                System.IO.File.Delete(oldFilePath);
                            }
                        }

                        // 保存文件及数据
                        string filePostName = voiceFile.FileName;
                        string fileSaveName = Guid.NewGuid().ToString();
                        string ext = System.IO.Path.GetExtension(filePostName);

                        string filePath = HttpContext.Current.Request.MapPath(string.Format("~/Files/CallVoices/{0}{1}", fileSaveName, ext));
                        voiceFile.SaveAs(filePath);

                        CallVoice newCallVoice = new CallVoice();
                        newCallVoice.CallRecordID = callID;
                        newCallVoice.FilePostName = filePostName;
                        newCallVoice.FileSaveName = fileSaveName + ext;
                        newCallVoice.FileSize = voiceFile.ContentLength / 1024;
                        ServiceInvokeDTO<CallVoice> addResult = userDataService.AddCallVoice(newCallVoice);

                        // Model --> DTO
                        if (addResult != null && addResult.Code == InvokeCode.SYS_INVOKE_SUCCESS && addResult.Data != null)
                        {
                            CallVoiceDTO dto = new CallVoiceDTO(addResult.Data);
                            dto.DownloadUrl = string.Format(Constant.CALLVOICE_DOWNLOAD_FORMAT, request.Headers.Host, addResult.Data.FileSaveName);

                            result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_INVOKE_SUCCESS, dto);
                        }
                        else
                        {
                            result = new ServiceInvokeDTO<CallVoiceDTO>(addResult.Code);
                        }
                    }
                    else
                    {
                        result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.CALLVOICE_FILE_NEEDED);
                    }
                }
                else
                {
                    result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_SIGN_ERROR);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                result = new ServiceInvokeDTO<CallVoiceDTO>(InvokeCode.SYS_INNER_ERROR);
            }
            log.Debug(Constant.DEBUG_END);

            return request.CreateResponse(HttpStatusCode.OK, result);
        }