Exemplo n.º 1
0
        //更新店铺时语音转mp3
        public async Task <string> SendVoiceToAliOss(int itemId, int itemType, string sourceTempUrl)
        {
            if (itemId <= 0 || itemType <= 0 || string.IsNullOrEmpty(sourceTempUrl))
            {
                return(string.Empty);
            }
            C_Attachment model = GetModelByType(itemId, itemType);

            if (model == null)
            {
                model            = new C_Attachment();
                model.itemId     = itemId;
                model.itemType   = itemType;
                model.createDate = DateTime.Now;
                model.thumbnail  = "";
            }
            model.filepath = sourceTempUrl;
            try
            {
                if (!model.filepath.Contains("temp"))//不是新上传的,停止操作!
                {
                    return(string.Empty);
                }
                var    bucket      = ConfigurationManager.AppSettings["BucketName"];
                int    whichDomain = 1;
                string TemplateId  = string.Empty;
                string PipelineId  = string.Empty;
                TemplateId = ConfigurationManager.AppSettings["VoiceTemplateId"] ?? "42d5aac40e6a50bf13045a40aeb83b6f";
                PipelineId = ConfigurationManager.AppSettings["PipelineId"] ?? "4bc9dd15cb3d48e39e0824e19c41defb";
                var finalVoiceKey    = string.Empty;
                var finalVoiceFolder = AliOSSHelper.GetOssVoiceKey("mp3", false, "voice/folder", out finalVoiceKey, whichDomain);
                //上传的本地音频。并且不是mp3|| 微信语音
                if (!string.IsNullOrEmpty(model.VoiceServerId))
                {
                    //转换mp3
                    bool submitResult = await AliMTSHelper.SubmitJobs(model.filepath, finalVoiceKey, bucket, TemplateId, PipelineId, "", whichDomain);

                    if (submitResult)
                    {//转换成功。更新路径
                        model.thumbnail = finalVoiceKey;
                    }//音频转换失败
                    else
                    {
                        log4net.LogHelper.WriteInfo(this.GetType(), "语音给AliOSS转换格式失败!ID为" + model.id + "==" + model.filepath);
                    }
                }//mp3移动
                else
                {
                    //本地音频mp3格式从temp 拷贝
                    Task <bool> moveResult = Task <bool> .Factory.StartNew(
                        () =>
                    {
                        return(AliOSSHelper.CopyObect(model.filepath ?? "", finalVoiceKey));
                    }
                        );

                    if (await moveResult)
                    {//移动成功。更新路径
                        model.thumbnail = finalVoiceKey;
                    }
                    // 移动失败
                    else
                    {
                        log4net.LogHelper.WriteInfo(this.GetType(), "本地音频AliOSS临时文件夹移动到正式文件夹失败!ID为" + sourceTempUrl);
                        return("");
                    }
                }
                if (model.id == 0)
                {
                    Add(model);
                }
                else
                {
                    MySqlParameter[] param = new MySqlParameter[] { new MySqlParameter("@itemId", itemId),
                                                                    new MySqlParameter("@filepath", model.filepath),
                                                                    new MySqlParameter("@thumbnail", model.thumbnail),
                                                                    new MySqlParameter("@id", model.id) };
                    string sql = "update C_Attachment set itemId=@itemId,filepath=@filepath,thumbnail=@thumbnail where id=@id";
                    SqlMySql.ExecuteNonQuery(Utility.dbEnum.MINIAPP.ToString(), System.Data.CommandType.Text, sql, param);
                    RemoveRedis(model.itemId, model.itemType);
                }
                return("");
            }
            catch (Exception ex)
            {
                log4net.LogHelper.WriteError(this.GetType(), new Exception("voiceid" + model.id + "移动语音失败:" + ex.Message));
                return(string.Empty);
            }
        }
Exemplo n.º 2
0
        //更新店铺时语音转mp3
        public async Task <string> SendVoiceToAliOss(int voiceId, int artId, int commentId, string content = "", bool needUpdateContent = false, bool isupdate = false)
        {
            string artcont = content;

            if (voiceId < 1)
            {
                return(string.Empty);
            }
            C_Attachment model = GetModel(voiceId);

            if (model == null)
            {
                return(string.Empty);
            }
            model.itemId = artId;
            try
            {
                if (!string.IsNullOrEmpty(model.filepath))
                {
                    if (isupdate)
                    {
                        if (!model.filepath.Contains("temp"))//不是新上传的,停止操作!
                        {
                            log4net.LogHelper.WriteInfo(this.GetType(), "观察日志:修改文章停止旧语音移动。文章ID=" + artId + "语音路径" + model.filepath);
                            return(string.Empty);
                        }
                    }
                    var    bucket      = ConfigurationManager.AppSettings["BucketName"];
                    int    whichDomain = 1;
                    string TemplateId  = string.Empty;
                    string PipelineId  = string.Empty;
                    TemplateId = ConfigurationManager.AppSettings["VoiceTemplateId"] ?? "42d5aac40e6a50bf13045a40aeb83b6f";
                    PipelineId = ConfigurationManager.AppSettings["PipelineId"] ?? "4bc9dd15cb3d48e39e0824e19c41defb";
                    var finalVoiceKey    = string.Empty;
                    var finalVoiceFolder = AliOSSHelper.GetOssVoiceKey("mp3", false, "voice/folder", out finalVoiceKey, whichDomain);
                    //上传的本地音频。并且不是mp3|| 微信语音
                    if (!string.IsNullOrEmpty(model.VoiceServerId))
                    {
                        //转换mp3
                        bool submitResult = await AliMTSHelper.SubmitJobs(model.filepath, finalVoiceKey, bucket, TemplateId, PipelineId, "", whichDomain);

                        if (submitResult)
                        {//转换成功。更新路径
                            model.thumbnail = finalVoiceKey;
                            //图文混排内容里的音频替换
                            if (needUpdateContent)
                            {
                                var voiceurl = model.filepath;
                                artcont = CRegex.Replace(artcont, voiceurl, finalVoiceKey, 0);
                            }
                        }//音频转换失败
                        else
                        {
                            log4net.LogHelper.WriteInfo(this.GetType(), "语音给AliOSS转换格式失败!ID为" + model.id + "==" + model.filepath);
                        }
                    }//mp3移动
                    else
                    {
                        //本地音频mp3格式从temp 拷贝
                        Task <bool> moveResult = Task <bool> .Factory.StartNew(
                            () =>
                        {
                            return(AliOSSHelper.CopyObect(model.thumbnail, finalVoiceKey));
                        }
                            );

                        ;
                        if (await moveResult)
                        {//移动成功。更新路径
                            model.thumbnail = finalVoiceKey;
                            //图文混排内容里的音频替换
                            if (needUpdateContent)
                            {
                                var voiceurl = model.filepath;
                                artcont = CRegex.Replace(artcont, voiceurl, finalVoiceKey, 0);
                            }
                        }
                        // 移动失败
                        else
                        {
                            log4net.LogHelper.WriteInfo(this.GetType(), "本地音频AliOSS临时文件夹移动到正式文件夹失败!ID为" + model.id);
                        }
                    }
                }

                MySqlParameter[] param = new MySqlParameter[] { new MySqlParameter("@itemId", artId),
                                                                new MySqlParameter("@filepath", model.filepath),
                                                                new MySqlParameter("@thumbnail", model.thumbnail),
                                                                new MySqlParameter("@id", model.id) };

                string sql = "update C_Attachment set itemId=@itemId,filepath=@filepath,thumbnail=@thumbnail where id=@id";
                SqlMySql.ExecuteNonQuery(Utility.dbEnum.MINIAPP.ToString(), System.Data.CommandType.Text, sql, param);
                RedisUtil.Remove(string.Format(CImageKey, model.itemId, model.itemType));
                RemoveRedis(model.itemId, model.itemType);

                return(artcont);
            }
            catch (Exception ex)
            {
                log4net.LogHelper.WriteError(this.GetType(), new Exception("voiceid" + model.id + "移动语音失败:" + ex.Message));
                return(string.Empty);
            }
        }