Beispiel #1
0
        public static int UpdateMemo(string title, string content)
        {
            var oldMemo = GetMemo(title);


            string rawTitle = title;

            title = ConvertTitle(title);

            string path       = Path.Combine(Root, title + ".txt");
            string backupPath = string.Empty;


            if (oldMemo == null)
            {
                string lastBackupPath = Path.Combine(Root, $"{title}.bak");

                if (File.Exists(lastBackupPath))
                {
                    oldMemo = new Memo();

                    lock (m_lockDic)
                    {
                        oldMemo.Load(lastBackupPath);
                    }

                    if (oldMemo.Title != rawTitle)
                    {
                        oldMemo = null;
                    }
                }
            }

            if (oldMemo != null)
            {
                backupPath = Path.Combine(Root, $"{title}.{oldMemo.Revision}.rev");
            }


            var memo = new Memo
            {
                Title    = rawTitle,
                Content  = content,
                Revision = (oldMemo?.Revision ?? -1) + 1,
            };

            lock (m_lockDic)
            {
                if (oldMemo != null)
                {
                    oldMemo.Save(backupPath);
                }

                m_dic[title] = memo;
                memo.Save(path);

                memo.LastModifiedTime = File.GetLastWriteTimeUtc(path);
            }


            lock (m_lockRecentMemos)
            {
                m_recentMemos.Add(memo.Clone());

                if (m_recentMemos.Count > 20)
                {
                    m_recentMemos.RemoveAt(0);
                }
            }


            return(memo.Revision);
        }
Beispiel #2
0
        public static bool RevertMemo(string title)
        {
            string rawTitle = title;
            var    memo     = GetMemo(title);


            title = ConvertTitle(title);

            if (memo == null)
            {
                string lastBackupPath = Path.Combine(Root, $"{title}.bak");

                if (File.Exists(lastBackupPath))
                {
                    memo = new Memo();

                    lock (m_lockDic)
                    {
                        memo.Load(lastBackupPath);

                        if (memo.Title == rawTitle)
                        {
                            var tempMemo = memo.Clone();
                            tempMemo.Revision += 1;
                            tempMemo.Content   = " ";

                            string path = Path.Combine(Root, $"{title}.txt");
                            tempMemo.Save(path);
                        }
                    }

                    if (memo.Title == rawTitle)
                    {
                        if (string.IsNullOrWhiteSpace(memo.Content))
                        {
                            DeleteMemo(rawTitle);
                        }
                        else
                        {
                            UpdateMemo(rawTitle, memo.Content);
                        }

                        return(true);
                    }
                }
            }
            else if (memo.Revision > 0)
            {
                string revPath = Path.Combine(Root, $"{title}.{memo.Revision - 1}.rev");

                if (File.Exists(revPath))
                {
                    var revMemo = new Memo();

                    lock (m_lockDic)
                    {
                        revMemo.Load(revPath);
                    }

                    if (revMemo.Title == rawTitle)
                    {
                        if (string.IsNullOrWhiteSpace(revMemo.Content))
                        {
                            DeleteMemo(rawTitle);
                        }
                        else
                        {
                            UpdateMemo(rawTitle, revMemo.Content);
                        }

                        return(true);
                    }
                }
            }


            return(false);
        }