//todo:格式化URL //todo:得到内容 public IActionResult GetNoteContent(string token, string noteId) { ApiRe falseRe = new ApiRe() { Ok = false, Msg = "GetNoteContent_is_error" }; Note note = NoteService.GetNote(MyConvert.HexToLong(noteId), getUserIdByToken(token)); NoteContent noteContent = NoteContentService.GetNoteContent(MyConvert.HexToLong(noteId), getUserIdByToken(token), false); if (noteContent == null || note == null) { return(Json(falseRe, MyJsonConvert.GetOptions())); } if (noteContent != null && !string.IsNullOrEmpty(noteContent.Content)) { noteContent.Content = NoteService.FixContent(noteContent.Content, note.IsMarkdown); } ApiNoteContent apiNote = new ApiNoteContent() { NoteId = note.NoteId, UserId = note.UserId, Content = noteContent.Content }; return(Json(apiNote, MyJsonConvert.GetOptions())); }
public async Task <IActionResult> GetNoteContent(string token, string noteId) { ApiRe re = new ApiRe() { Ok = false, Msg = "GetNoteContent_is_error" }; try { var user = GetUserByToken(token); if (user == null) { return(LeanoteJson(re)); } Note note = noteService.GetNote(noteId.ToLongByHex(), GetUserIdByToken(token)); NoteContent noteContent = noteContentService.GetNoteContent(noteId.ToLongByHex(), GetUserIdByToken(token), false); if (noteContent == null || note == null) { return(Json(re, MyJsonConvert.GetLeanoteOptions())); } if (noteContent != null && !string.IsNullOrEmpty(noteContent.Content)) { noteContent.Content = noteService.FixContent(noteContent.Content, note.IsMarkdown); } else { noteContent.Content = "<p>Content is IsNullOrEmpty<>"; } if (noteContent.IsEncryption) { var dec = this.cryptographyProvider.SM4Decrypt(noteContent.Content.Base64ToByteArray()); noteContent.Content = Encoding.UTF8.GetString(dec); } re.Ok = true; re.Data = noteContent; return(LeanoteJson(re)); } catch (Exception ex) { re.Ok = false; re.Msg = ex.Message; throw; } }
public IActionResult Editor(string noteIdHex) { this.SetLocale(); //设置区域化信息 //todo:存在一个问题,可能导出用户敏感信息 var userInfo = this.GetUserAndBlogUrl(); //得到用户信息+博客主页 //判断用户ID是否已经登录 var user = this.GetUserBySession(); var userId = user.UserId; Notebook[] noteBoooks = notebookService.GetNoteBookTree(user.UserId); //是否已经开放注册功能 ViewBag.openRegister = configFileService.WebConfig.SecurityConfig.OpenRegister; // 已登录了, 那么得到所有信息 var notebooks = notebookService.GetNotebooks(userId); //获得共享的笔记 // 还需要按时间排序(DESC)得到notes List <Note> notes = new List <Note>(); NoteContent noteContent = new NoteContent(); if (!notebooks.IsNullOrNothing()) { // noteId是否存在 // 是否传入了正确的noteId var hasRightNoteId = false; long?noteId = noteIdHex.ToLongByHex(); if (noteId != null) { //说明ID本身是有效的 var note = noteService.GetNoteById(noteId); if (note != null) { var noteOwner = note.UserId; noteContent = noteContentService.GetNoteContent(noteId, noteOwner, false); hasRightNoteId = true; ViewBag.curNoteId = noteId.ToHex24(); ViewBag.curNotebookId = note.NotebookId.ToHex24(); // 打开的是共享的笔记, 那么判断是否是共享给我的默认笔记 if (noteOwner != GetUserIdBySession()) { if (shareService.HasReadPerm(noteOwner, GetUserIdBySession(), noteId)) { ViewBag.curSharedNoteNotebookId = note.NotebookId.ToHex24(); ViewBag.curSharedUserId = noteOwner; } else { hasRightNoteId = false; } } else { notes = noteService.ListNotes(this.GetUserIdBySession(), note.NotebookId, false, GetPage(), 50, defaultSortField, false, null); // 如果指定了某笔记, 则该笔记放在首位 } } //获得最近的笔记 int count2 = 0; var latestNotes = noteService.ListNotes(userId, false, GetPage(), 50, defaultSortField, false, null); ViewBag.latestNotes = latestNotes; } // 没有传入笔记 // 那么得到最新笔记 if (!hasRightNoteId) { notes = noteService.ListNotes(userId, false, GetPage(), 50, defaultSortField, false, null); if (notes.Any()) { noteContent = noteContentService.GetValidNoteContent(notes[0].NoteId, userId); ViewBag.curNoteId = notes[0].NoteId; } } } ViewBag.isAdmin = user.Username.Equals(config.SecurityConfig.AdminUsername); ViewBag.IsDevelopment = config.APPConfig.Dev; ViewBag.userInfo = userInfo; ViewBag.notebooks = notebooks; //ViewBag.shareNotebooks= shareNotebooks; ViewBag.notes = notes; ViewBag.noteContentJson = noteContent; ViewBag.noteContent = noteContent == null ? null : noteContent.Content; ViewBag.tags = tagService.GetTags(userId); ViewBag.config = config; Dictionary <string, string> js = new Dictionary <string, string>(); SetLocale(); ViewBag.js = js; //页面的值 ViewBag.isAdmin = configFileService.WebConfig.SecurityConfig.AdminUsername.Equals(user.Username); ViewBag.userInfo = userInfo; ViewBag.OpenRegister = config.SecurityConfig.OpenRegister; //编辑器偏好 ViewBag.MarkdownEditorOption = userInfo.MarkdownEditorOption; ViewBag.RichTextEditorOption = userInfo.RichTextEditorOption; return(View()); }
//todo:更新笔记 public JsonResult UpdateNote(ApiNote noteOrContent, string token) { Note noteUpdate = new Note(); var needUpdateNote = false; var re = new ReUpdate(); long tokenUserId = getUserIdByToken(token); var noteId = MyConvert.HexToLong(noteOrContent.NoteId); //-------------校验参数合法性 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 = upload("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.GetOptions())); } } else { // 建立映射 file.FileId = serverFileId.ToString("x"); 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.UpdateOrDeleteAttachApi(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.GetOptions())); } //-------------更新笔记元数据 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.GetOptions())); } //处理结果 //-------------API返回客户端信息 note = NoteService.GetNote(noteId, tokenUserId); // noteOrContent.NoteId = noteId.ToString("x"); // noteOrContent.UserId = tokenUserId.ToString("x"); // 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.ToString("x"); return(Json(noteOrContent, MyJsonConvert.GetOptions())); }