Esempio n. 1
0
        public async Task <IActionResult> UpdateNoteTitleAndContent(string token, string noteId, string noteTitle, string content, string dataSignJson, string digitalEnvelopeJson)
        {
            var user = tokenSerivce.GetUserByToken(token);
            var re   = new ApiRe();

            if (user == null)
            {
                return(LeanoteJson(re));
            }
            DigitalEnvelope digitalEnvelope = null;
            var             verify          = false;

            if (this.config.SecurityConfig.ForceDigitalEnvelope)
            {
                //数字信封
                if (this.config.SecurityConfig.ForceDigitalEnvelope)
                {
                    digitalEnvelope = DigitalEnvelope.FromJSON(digitalEnvelopeJson);
                    var data = digitalEnvelope.GetPayLoadValue(this.gMService, this.config.SecurityConfig.PrivateKey);
                    if (data == null)
                    {
                        throw new Exception("数字信封解密失败");
                    }
                    //赋予解密的数字信封
                    content = data;
                }
            }

            if (this.config.SecurityConfig.ForceDigitalSignature)
            {
                //验证签名
                var dataSign = DataSignDTO.FromJSON(dataSignJson);
                verify = await this.ePassService.VerifyDataSign(dataSign);

                if (!verify)
                {
                    return(LeanoteJson(re));
                }
                verify = dataSign.SignData.Operate.Equals("/api/Note/UpdateNoteTitleAndContent");
                if (!verify)
                {
                    re.Msg = "Operate is not Equals ";
                    return(LeanoteJson(re));
                }
                //签字签名和数字信封数据
                if (dataSign != null)
                {
                    var dataSM3 = gMService.SM3(noteId + noteTitle + content);
                    var signSM3 = dataSign.SignData.Hash;
                    if (!dataSM3.ToUpper().Equals(signSM3.ToUpper()))
                    {
                        re.Msg = "SM3 is error";
                        re.Ok  = false;
                        return(LeanoteJson(re));
                    }
                }

                //签名存证
                this.dataSignService.AddDataSign(dataSign, "UpdateNoteTitleAndContent");
            }

            //-------------校验参数合法性
            if (user == null)
            {
                re.Msg = "NOlogin";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }

            // 先判断USN的问题, 因为很可能添加完附件后, 会有USN冲突, 这时附件就添错了
            var note = noteService.GetNote(noteId.ToLongByHex(), user.UserId);

            verify = noteRepositoryService.Verify(note.NotesRepositoryId, user.UserId, RepositoryAuthorityEnum.Write);
            if (!verify)
            {
                return(LeanoteJson(re));
            }

            if (note == null || note.NoteId == 0)
            {
                re.Msg = "notExists";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }
            var des = MyHtmlHelper.SubHTMLToRaw(content, 200);

            var noteContentId = idGenerator.NextId();

            NoteContent noteContent = new NoteContent()
            {
                NoteContentId = noteContentId,
                Abstract      = content,
                Content       = content,

                UserId        = user.UserId,
                NoteId        = note.NoteId,
                CreatedTime   = DateTime.Now,
                UpdatedTime   = DateTime.Now,
                UpdatedUserId = user.UserId
            };

            if (this.config.SecurityConfig.DataBaseEncryption)
            {
                noteContent.Abstract = "DataBaseEncryption";
            }

            noteContentService.UpdateNoteContent(note.NoteId, noteContent);

            noteService.UpdateNoteTitle(note.NoteId, noteTitle);

            var usn = noteRepositoryService.IncrUsn(note.NotesRepositoryId);

            noteService.UpdateUsn(note.NoteId, usn);
            re.Ok   = true;
            re.Data = note;
            if (this.config.SecurityConfig.ForceDigitalEnvelope)
            {
                var key  = digitalEnvelope.getSM4Key(this.gMService, this.config.SecurityConfig.PrivateKey);
                var json = note.ToJson();

                var payLoad = new PayLoadDTO();
                payLoad.SetData(json);

                var payLoadJson = payLoad.ToJson();

                var jsonHex = Common.Utils.HexUtil.ByteArrayToString(Encoding.UTF8.GetBytes(payLoadJson));

                var enc = gMService.SM4_Encrypt_CBC(jsonHex, key, digitalEnvelope.IV, true);
                re.Data       = enc;
                re.Encryption = true;
            }

            return(LeanoteJson(re));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddNote(ApiNote noteOrContent, string token)
        {
            var re = new ApiRe();

            var user = tokenSerivce.GetUserByToken(token);

            if (user == null)
            {
                return(LeanoteJson(re));
            }

            //json 返回状态乱

            long?tokenUserId = GetUserIdByToken(token);;
            long?myUserId    = tokenUserId;

            if (noteOrContent == null || string.IsNullOrEmpty(noteOrContent.NotebookId))
            {
                return(Json(new ApiRe()
                {
                    Ok = false, Msg = "notebookIdNotExists"
                }, MyJsonConvert.GetSimpleOptions()));
            }
            long?noteId = idGenerator.NextId();

            if (noteOrContent.Title == null)
            {
                noteOrContent.Title = "无标题";
            }

            // TODO 先上传图片/附件, 如果不成功, 则返回false
            //-------------新增文件和附件内容
            int attachNum = 0;

            if (noteOrContent.Files != null && noteOrContent.Files.Length > 0)
            {
                for (int i = 0; i < noteOrContent.Files.Length; i++)
                {
                    var file = noteOrContent.Files[i];
                    if (file.HasBody)
                    {
                        if (!string.IsNullOrEmpty(file.LocalFileId))
                        {
                            var result = UploadImages("FileDatas[" + file.LocalFileId + "]", tokenUserId, noteId, file.IsAttach, out long?serverFileId, out string msg);
                            if (!result)
                            {
                                if (string.IsNullOrEmpty(msg))
                                {
                                    re.Msg = "fileUploadError";
                                }
                                else
                                {
                                    re.Msg = msg;
                                    return(Json(re, MyJsonConvert.GetLeanoteOptions()));
                                }
                            }
                            else
                            {
                                // 建立映射
                                file.FileId            = serverFileId.ToHex24();
                                noteOrContent.Files[i] = file;
                                if (file.IsAttach)
                                {
                                    attachNum++;
                                }
                            }
                        }
                        else
                        {   //存在疑问
                            return(Json(new ReUpdate()
                            {
                                Ok = false,
                                Msg = "LocalFileId_Is_NullOrEmpty",
                                Usn = 0
                            }, MyJsonConvert.GetSimpleOptions()));
                        }
                    }
                }
            }
            else
            {
            }
            //-------------替换笔记内容中的文件ID
            FixPostNotecontent(ref noteOrContent);
            if (noteOrContent.Tags != null)
            {
                if (noteOrContent.Tags.Length > 0 && noteOrContent.Tags[0] == null)
                {
                    noteOrContent.Tags = Array.Empty <string>();
                    //noteOrContent.Tags= new string[] { ""};
                }
            }
            //-------------新增笔记对象
            Note note = new Note()
            {
                UserId        = tokenUserId,
                NoteId        = noteId,
                CreatedUserId = tokenUserId,
                UpdatedUserId = noteId,
                NotebookId    = noteOrContent.NotebookId.ToLongByHex(),
                Title         = noteOrContent.Title,
                Tags          = noteOrContent.Tags,
                Desc          = noteOrContent.Desc,
                IsBlog        = noteOrContent.IsBlog.GetValueOrDefault(),
                IsMarkdown    = noteOrContent.IsMarkdown.GetValueOrDefault(),
                AttachNum     = attachNum,
                CreatedTime   = noteOrContent.CreatedTime,
                UpdatedTime   = noteOrContent.UpdatedTime,
                ContentId     = idGenerator.NextId()
            };

            //-------------新增笔记内容对象
            NoteContent noteContent = new NoteContent()
            {
                NoteContentId = note.ContentId,
                NoteId        = noteId,
                UserId        = tokenUserId,
                IsBlog        = note.IsBlog,
                Content       = noteOrContent.Content,
                Abstract      = noteOrContent.Abstract,
                CreatedTime   = noteOrContent.CreatedTime,
                UpdatedTime   = noteOrContent.UpdatedTime,
                IsHistory     = false
            };

            //-------------得到Desc, abstract
            if (string.IsNullOrEmpty(noteOrContent.Abstract))
            {
                if (noteOrContent.IsMarkdown.GetValueOrDefault())
                {
                    // note.Desc = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200);
                    noteContent.Abstract = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200);
                }
                else
                {
                    //note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200);
                    noteContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200);
                }
            }
            else
            {
                note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Abstract, 200);
            }
            if (noteOrContent.Desc == null)
            {
                if (noteOrContent.IsMarkdown.GetValueOrDefault())
                {
                    note.Desc = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200);
                }
                else
                {
                    note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200);
                }
            }
            else
            {
                note.Desc = noteOrContent.Desc;
            }

            note = noteService.AddNoteAndContent(note, noteContent, myUserId);
            //-------------将笔记与笔记内容保存到数据库
            if (note == null || note.NoteId == 0)
            {
                return(Json(new ApiRe()
                {
                    Ok = false,
                    Msg = "AddNoteAndContent_is_error"
                }));
            }
            //-------------API返回客户端信息
            noteOrContent.NoteId      = noteId.ToHex24();
            noteOrContent.UserId      = tokenUserId.ToHex24();
            noteOrContent.Title       = note.Title;
            noteOrContent.Tags        = note.Tags;
            noteOrContent.IsMarkdown  = note.IsMarkdown;
            noteOrContent.IsBlog      = note.IsBlog;
            noteOrContent.IsTrash     = note.IsTrash;
            noteOrContent.IsDeleted   = note.IsDeleted;
            noteOrContent.IsTrash     = note.IsTrash;
            noteOrContent.IsTrash     = note.IsTrash;
            noteOrContent.Usn         = note.Usn;
            noteOrContent.CreatedTime = note.CreatedTime;
            noteOrContent.UpdatedTime = note.UpdatedTime;
            noteOrContent.PublicTime  = note.PublicTime;
            //Files = files

            //------------- 删除API中不需要返回的内容
            noteOrContent.Content  = "";
            noteOrContent.Abstract = "";
            //	apiNote := info.NoteToApiNote(note, noteOrContent.Files)

            return(Json(noteOrContent, MyJsonConvert.GetLeanoteOptions()));
        }
Esempio n. 3
0
        public JsonResult UpdateNote(ApiNote noteOrContent, string token)
        {
            Note noteUpdate     = new Note();
            var  needUpdateNote = false;
            var  re             = new ReUpdate();
            long?tokenUserId    = GetUserIdByToken(token);
            var  noteId         = noteOrContent.NoteId.ToLongByHex();

            //-------------校验参数合法性
            if (tokenUserId == 0)
            {
                re.Msg = "NOlogin";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }

            if (string.IsNullOrEmpty(noteOrContent.NoteId))
            {
                re.Msg = "noteIdNotExists";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }

            if (noteOrContent.Usn < 1)
            {
                re.Msg = "usnNotExists";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }
            // 先判断USN的问题, 因为很可能添加完附件后, 会有USN冲突, 这时附件就添错了
            var note        = noteService.GetNote(noteId, tokenUserId);
            var noteContent = noteContentService.GetNoteContent(note.NoteId, tokenUserId, false);

            if (note == null || note.NoteId == 0)
            {
                re.Msg = "notExists";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }
            //判断服务器版本与客户端版本是否一致
            if (note.Usn != noteOrContent.Usn)
            {
                re.Msg = "conflict";
                re.Ok  = false;
                return(Json(re, MyJsonConvert.GetSimpleOptions()));
            }
            //-------------更新文件和附件内容
            if (noteOrContent.Files != null && noteOrContent.Files.Length > 0)
            {
                for (int i = 0; i < noteOrContent.Files.Length; i++)
                {
                    var file = noteOrContent.Files[i];
                    if (file.HasBody)
                    {
                        if (!string.IsNullOrEmpty(file.LocalFileId))
                        {
                            var result = UploadImages("FileDatas[" + file.LocalFileId + "]", tokenUserId, noteId, file.IsAttach, out long?serverFileId, out string msg);
                            if (!result)
                            {
                                if (string.IsNullOrEmpty(msg))
                                {
                                    re.Msg = "fileUploadError";
                                }
                                if (!string.Equals(msg, "notImage", System.StringComparison.OrdinalIgnoreCase))
                                {
                                    return(Json(re, MyJsonConvert.GetLeanoteOptions()));
                                }
                            }
                            else
                            {
                                // 建立映射
                                file.FileId            = serverFileId.ToHex24();
                                noteOrContent.Files[i] = file;
                            }
                        }
                        else
                        {
                            return(Json(new ReUpdate()
                            {
                                Ok = false,
                                Msg = "LocalFileId_Is_NullOrEmpty",
                                Usn = 0
                            }, MyJsonConvert.GetSimpleOptions()));
                        }
                    }
                }
            }
            //更新用户元数据
            //int usn = UserService.IncrUsn(tokenUserId);

            // 移到外面来, 删除最后一个file时也要处理, 不然总删不掉
            // 附件问题, 根据Files, 有些要删除的, 只留下这些
            if (noteOrContent.Files != null)
            {
                attachService.UpdateOrDeleteAttachApiAsync(noteId, tokenUserId, noteOrContent.Files);
            }
            //-------------更新笔记内容
            var  afterContentUsn = 0;
            var  contentOk       = false;
            var  contentMsg      = "";
            long?contentId       = 0;

            if (noteOrContent.Content != null)
            {
                // 把fileId替换下
                FixPostNotecontent(ref noteOrContent);
                // 如果传了Abstract就用之
                if (noteOrContent.Abstract != null)
                {
                    noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Abstract, 200);
                }
                else
                {
                    noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200);
                }
            }
            else
            {
                noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteContent.Content, 200);
            }
            //上传noteContent的变更
            contentOk = noteContentService.UpdateNoteContent(
                noteOrContent,
                out contentMsg,
                out contentId
                );
            //返回处理结果
            if (!contentOk)
            {
                re.Ok  = false;
                re.Msg = contentMsg;
                re.Usn = afterContentUsn;
                return(Json(re, MyJsonConvert.GetLeanoteOptions()));
            }

            //-------------更新笔记元数据
            int afterNoteUsn = 0;
            var noteOk       = false;
            var noteMsg      = "";

            noteOk = noteService.UpdateNote(
                ref noteOrContent,
                tokenUserId,
                contentId,
                true,
                true,
                out noteMsg,
                out afterNoteUsn
                );
            if (!noteOk)
            {
                re.Ok  = false;
                re.Msg = noteMsg;
                return(Json(re, MyJsonConvert.GetLeanoteOptions()));
            }
            //处理结果
            //-------------API返回客户端信息
            note = noteService.GetNote(noteId, tokenUserId);
            // noteOrContent.NoteId = noteId.ToHex24();
            // noteOrContent.UserId = tokenUserId.ToHex24();
            //  noteOrContent.Title = note.Title;
            // noteOrContent.Tags = note.Tags;
            // noteOrContent.IsMarkdown = note.IsMarkdown;
            // noteOrContent.IsBlog = note.IsBlog;
            //noteOrContent.IsTrash = note.IsTrash;
            //noteOrContent.IsDeleted = note.IsDeleted;
            //noteOrContent.IsTrash = note.IsTrash;

            //noteOrContent.Usn = note.Usn;
            //noteOrContent.CreatedTime = note.CreatedTime;
            //noteOrContent.UpdatedTime = note.UpdatedTime;
            //noteOrContent.PublicTime = note.PublicTime;

            noteOrContent.Content     = "";
            noteOrContent.Usn         = afterNoteUsn;
            noteOrContent.UpdatedTime = DateTime.Now;
            noteOrContent.IsDeleted   = false;
            noteOrContent.UserId      = tokenUserId.ToHex24();
            return(Json(noteOrContent, MyJsonConvert.GetLeanoteOptions()));
        }
Esempio n. 4
0
        /// <summary>
        /// 更新笔记 元数据
        /// </summary>
        /// <param name="apiNote"></param>
        /// <returns></returns>
        public static bool UpdateNote(ref ApiNote apiNote, long updateUser, long contentId, bool verifyUsn, bool verifyOwner,
                                      out string msg, out int afterUsn)
        {
            var noteId = MyConvert.HexToLong(apiNote.NoteId);

            afterUsn = 0;
            if (apiNote == null)
            {
                msg = "apiNote_is_null";
                return(false);
            }
            // var noteId = MyConvert.HexToLong(apiNote.NoteId);
            if (noteId == 0)
            {
                msg = "noteId_is_note_long_Number";
                return(false);
            }
            using (var db = new DataContext())
            {
                var result = db.Note.Where(b => b.NoteId == noteId && b.UserId == updateUser);
                if (result == null)
                {
                    msg = "inexistence";
                    return(false);
                }
                var note = result.FirstOrDefault();

                if (verifyUsn)
                {
                    if (note.Usn != apiNote.Usn)
                    {
                        msg = "Verify_Usn_Failure";
                        return(false);
                    }
                }
                if (verifyOwner)
                {
                    if (note.UserId != updateUser)
                    {
                        msg = "Verify_updateUser_Failure";
                        return(false);
                    }
                }
                if (apiNote.Desc != null)
                {
                    note.Desc = apiNote.Desc;
                }

                if (apiNote.Title != null)
                {
                    note.Title = apiNote.Title;
                }
                if (apiNote.IsTrash != null)
                {
                    note.IsTrash = apiNote.IsTrash.GetValueOrDefault();
                }
                if (apiNote.IsBlog != null)
                {
                    if (note.IsBlog == false && apiNote.IsBlog == true)
                    {
                        note.PublicTime = DateTime.Now;
                    }
                    note.IsBlog = apiNote.IsBlog.GetValueOrDefault(false);
                }
                if (apiNote.Tags != null)
                {
                    note.Tags = apiNote.Tags;
                    TagService.AddTags(note.UserId, note.Tags);
                    BlogService.ReCountBlogTags(note.UserId);
                }
                if (apiNote.NotebookId != null)
                {
                    var noteBookId = MyConvert.HexToLong(apiNote.NotebookId);
                    if (note.NotebookId == 0)
                    {
                        msg = "NotebookId_Is_Illegal";
                        return(false);
                    }
                    if (note.NotebookId != noteBookId)
                    {
                        // 如果修改了notebookId, 则更新notebookId'count
                        // 两方的notebook也要修改
                        NotebookService.ReCountNotebookNumberNotes(note.NotebookId);
                        NotebookService.ReCountNotebookNumberNotes(noteBookId);
                        note.NotebookId = noteBookId;
                    }
                }
                if (apiNote.Content != null)
                {
                    note.ContentId = contentId;
                    if (apiNote.Abstract == null)
                    {
                        if (apiNote.IsMarkdown.GetValueOrDefault(note.IsMarkdown))
                        {
                            note.Desc = MyHtmlHelper.SubMarkDownToRaw(apiNote.Content, 200);
                        }
                        else
                        {
                            note.Desc = MyHtmlHelper.SubHTMLToRaw(apiNote.Content, 200);
                        }
                        //  note.Desc = MyHtmlHelper.SubStringHTMLToRaw(apiNote.Content, 200);
                    }
                    else
                    {
                        note.Desc = MyHtmlHelper.SubHTMLToRaw(apiNote.Abstract, 200);
                        //note.Desc = MyHtmlHelper.SubStringHTMLToRaw(apiNote.Abstract, 200);
                    }
                }
                if (apiNote.UpdatedTime != null)
                {
                    note.UpdatedTime = Tools.FixUrlTime(apiNote.UpdatedTime);
                }
                else
                {
                    note.UpdatedTime = DateTime.Now;
                }
                if (note.IsBlog && note.HasSelfDefined)
                {
                    note.ImgSrc = null;
                    note.Desc   = null;
                }
                if (apiNote.IsTrash != null)
                {
                    note.IsTrash = apiNote.IsTrash.GetValueOrDefault(false);
                    NotebookService.ReCountNotebookNumberNotes(note.NotebookId);
                }
                if (apiNote.IsMarkdown != null)
                {
                    note.IsMarkdown = apiNote.IsMarkdown.GetValueOrDefault();
                }
                note.UpdatedUserId = MyConvert.HexToLong(apiNote.UserId);
                //更新用户元数据乐观锁
                afterUsn = UserService.IncrUsn(note.UserId);
                //更新笔记元数据乐观锁
                note.Usn = afterUsn;
                db.SaveChanges();
                msg = "success";
                return(true);
            }
        }