示例#1
0
        public JsonResult Edit(Blog newModel)
        {
            newModel = UrlCommon.DecodeModel(newModel);
            if (string.IsNullOrWhiteSpace(newModel.BlogId))
            {
                return(ResponseResult(-1, "要编辑的文章ID不正确(无法获取)"));
            }
            var         model = blogApplication.FindById(newModel.BlogId);
            IList <Tag> tags  = null;

            if (!string.IsNullOrWhiteSpace(Request["Tags"]))
            {
                //反序列化tag
                tags = JavaScriptCommon.DeSerialize <IList <Tag> >(UrlCommon.Decode(Request["Tags"]));
            }
            else
            {
                return(ResponseResult(1, new { msg = "标签不允许为空" }));
            }
            //TODO 应该先对两个blog进行修改,如果发现是一样的就不修改blog了
            string validateResult = blogApplication.ValidateAndCorrectSubmit(newModel, classifyApplication);

            if (validateResult == null)
            {
                return(SaidCommon.Transaction(() =>
                {
                    blogApplication.EditBlog(newModel, model, tags, tagApplication, blogTagsApplication);
                    if (blogTagsApplication.Commit())
                    {
                        // 清理 cache,因为前台读取的时候引用了 cache
                        if (CacheHelper.GetCache(model.BlogId) != null)
                        {
                            CacheHelper.RemoveAllCache(model.BlogId);
                        }
                        return ResponseResult(new { id = newModel.BlogId });
                    }
                    return ResponseResult(2, "修改Blog失败");
                }));
            }
            else
            {
                return(ResponseResult(1, new { msg = validateResult }));
            }
        }
示例#2
0
        public JsonResult AddBlog(Blog model)
        {
            //if (string.IsNullOrWhiteSpace(model.ClassifyId))
            //    return ResponseResult(1, "没有填写分类信息");

            //修正编码数据
            model = UrlCommon.DecodeModel(model);
            IList <Tag> tags = null;

            if (!String.IsNullOrWhiteSpace(Request["Tags"]))
            {
                //反序列化tag
                tags = JavaScriptCommon.DeSerialize <IList <Tag> >(UrlCommon.Decode(Request["Tags"]));
            }
            else
            {
                return(ResponseResult(1, new { msg = "标签不允许为空" }));
            }

            string validateResult = blogApplication.ValidateAndCorrectSubmit(model, classifyApplication);

            if (validateResult == null)
            {
                return(SaidCommon.Transaction(() =>
                {
                    blogApplication.AddBlog(model, tags, blogTagsApplication, tagApplication);
                    if (blogApplication.Commit())
                    {
                        return ResponseResult(new { id = model.BlogId });
                    }
                    return ResponseResult(2);
                }));
            }
            else
            {
                return(ResponseResult(1, new { msg = validateResult }));
            }
        }
示例#3
0
        /// <summary>
        /// 上传一个图片,保存并返回图片信息新生成的文件名
        /// </summary>
        /// <param name="file"></param>
        /// <param name="filters"></param>
        /// <param name="maxSize"></param>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private Dictionary <string, string> Save(HttpPostedFileBase file, Array filters, int maxSize, string dirPath)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            dirPath = Server.MapPath(dirPath);
            //FileCommon.ExistsCreate(dirPath);
            if (file == null)
            {
                result.Add("code", "1");
                result.Add("msg", "没有文件");
                return(result);
            }
            if (file.InputStream == null || file.InputStream.Length > maxSize)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件大小超过限制");
                return(result);
            }
            //file.InputStream可以获取到System.io.Stream对象,由此可以对文件进行hash加密运算
            string fileName = file.FileName,
                   fileExt  = Path.GetExtension(fileName).ToLower();//扩展名

            if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filters, fileExt.Substring(1).ToLower()) == -1)
            {
                result.Add("code", "1");
                result.Add("msg", "上传文件扩展名是不允许的扩展名");
                return(result);
            }
            string newFileName = string.Empty, //新生成的文件名
                   filePath    = string.Empty;

            if (string.IsNullOrEmpty(dirPath))
            {
                result.Add("code", "1");
                result.Add("msg", "服务器异常");
                return(result);
            }


            newFileName = FileCommon.CreateFileNameByTime() + fileExt;
            filePath    = dirPath + newFileName;
            file.SaveAs(filePath);

            //TODO 这里需要检测有没有什么异常
            MusicInfo music = MusicCommon.GetFileInfo(filePath);

            music.Size = file.ContentLength;
            music.Type = fileExt.Substring(1);
            //if (string.IsNullOrEmpty(music.Length))
            //{
            //    //测试了10个左右的文件,发现木有音乐文件长度不存在的情况
            //    if (music.BitRate == 0)
            //    {
            //        result.Add("code", "2");
            //        result.Add("msg", "文件异常(无法读取到正确的文件信息)");
            //        return result;
            //    }
            //    else
            //    {
            //        //尝试转换
            //        //这里得到的是字节么?
            //        music.Length = (music.Size * 1024 / music.BitRate * 8).ToString();  //歌曲时长 = 文件大小(mb)/ 位率(KBPS) * 8
            //        System.Diagnostics.Debug.WriteLine(music.Length);
            //    }
            //}


            result.Add("code", "0");
            result.Add("path", filePath);
            result.Add("dir", dirPath);
            result.Add("name", newFileName);
            result.Add("data", JavaScriptCommon.Serialize(music));
            return(result);
        }