Ejemplo n.º 1
0
        /// <summary>
        /// 同步md資料進sqlite
        /// </summary>
        public void SynchronizeArticle()
        {
            var contentRootPath = _hostingEnvironment.ContentRootPath;
            var dirPath         = Path.Combine(contentRootPath, "AppData");
            var allFiles        = GetAllFiles(dirPath).Where(x => Path.GetExtension(x) == ".md");

            var allDbArticals = _blogDAL.GetAllArticleList().ToList();

            foreach (var filePath in allFiles)
            {
                var title = Path.GetFileNameWithoutExtension(filePath);
                if (title.StartsWith("#"))
                {
                    title = title.Substring(1, title.Length - 1).Trim();
                }

                if (allDbArticals.Any(x => x.Title == title))
                {
                    continue;
                }

                var relativePath = filePath.Replace(contentRootPath, "").TrimStart('\\');
                var pathSplit    = relativePath.Split('\\');
                _blogDAL.InsertArticle(new BlogArticle
                {
                    Title      = title,
                    Type       = pathSplit[1],
                    FilePath   = relativePath,
                    CreateTime = new DateTime(Convert.ToInt32(pathSplit[2]), Convert.ToInt32(pathSplit[3]), Convert.ToInt32(pathSplit[4])),
                    IsShow     = true
                });
            }
        }