Exemplo n.º 1
0
        public void UploadPicture(string blogFilePath, bool testFlag)
        {
            Console.WriteLine($"[INFO]START UPLOAD PICTURE IN FILE:\n{blogFilePath}");

            const string matchRule     = @"!\[.*?\]\((.*?)\)";
            var          pictureList   = MdHandler.RegexParser(blogFilePath, matchRule);
            var          pictureUrlDic = new Dictionary <string, string>();

            Client blogClient = null;

            if (!testFlag)
            {
                blogClient = new Client(_connectionInfo);
            }

            foreach (var picturePath in pictureList)
            {
                if (picturePath.StartsWith("http"))
                {
                    Console.WriteLine($"[INFO]Jump picture:{picturePath}");
                    continue;
                }

                try
                {
                    var pictureAbsPath = Path.Combine(new FileInfo(blogFilePath).DirectoryName, picturePath);
                    if (File.Exists(pictureAbsPath))
                    {
                        if (!testFlag)
                        {
                            var pictureUrl = blogClient.NewMediaObject(picturePath, _picFileTable[new FileInfo(picturePath).Extension.ToLower()], File.ReadAllBytes(pictureAbsPath));

                            if (!pictureUrlDic.ContainsKey(picturePath))
                            {
                                pictureUrlDic.Add(picturePath, pictureUrl.URL);
                            }
                            Console.WriteLine($"[INFO]{picturePath} uploaded");
                        }
                        else
                        {
                            Console.WriteLine($"[INFO]{picturePath} needs upload");
                        }
                    }
                    else
                    {
                        Console.WriteLine($"[ERROR]No such file:{picturePath}");
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[ERROR]" + e.Message);
                }
            }
            if (!testFlag)
            {
                var blogContent = MdHandler.ReplaceContentWithUrl(blogFilePath, pictureUrlDic);
                MdHandler.WriteFile(blogFilePath, new FileInfo(blogFilePath).DirectoryName, "", blogContent);
            }

            Console.WriteLine("[INFO]END UPLOAD PICTURE\n");
        }
Exemplo n.º 2
0
        public static void ReplaceBlogUrl(string blogFilePath, string inDirPath, string outDirPath, string jsonFilePath, string blogPlatform)
        {
            Console.WriteLine($"[INFO]START REPLACE BLOG URL:\n{blogFilePath}");
            const string matchRule = @"\[.*?\]\((.*?\.md)\)";
            var          linkList  = MdHandler.RegexParser(blogFilePath, matchRule);

            var blogUrlDic = new Dictionary <string, string>();

            var blogJson    = (JArray)JsonConvert.DeserializeObject(File.ReadAllText(jsonFilePath));
            var blogJsonDic = new Dictionary <string, JObject>();

            if (!(inDirPath.EndsWith("\\") || inDirPath.EndsWith("/")))
            {
                inDirPath = inDirPath + '\\';
            }

            foreach (var jToken in blogJson)
            {
                var blog = (JObject)jToken;
                blogJsonDic.Add(
                    PathHandler.GetAbsPath(Path.Combine(inDirPath, blog.Properties().First().Name), false),
                    blog);
            }

            foreach (var link in linkList)
            {
                if (link.StartsWith("http"))
                {
                    Console.WriteLine($"[INFO]Jump Link:{link}");
                    continue;
                }
                try
                {
                    var absLink = PathHandler.GetAbsPath(Path.Combine(new FileInfo(blogFilePath).DirectoryName, link), false);
                    var blogUrl = blogJsonDic[absLink][blogJsonDic[absLink].Properties().First().Name][blogPlatform].ToString();

                    if (!blogUrlDic.ContainsKey(link))
                    {
                        Console.WriteLine($"[INFO]Replace link {link} to {blogUrl}");
                        blogUrlDic.Add(link, blogUrl);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[ERROR]" + e.Message);
                    return;
                }
            }
            var blogContent = MdHandler.ReplaceContentWithUrl(blogFilePath, blogUrlDic);

            FileInfo outPutFile = new FileInfo(Path.Combine(outDirPath,
                                                            ".\\output\\" + blogPlatform + "\\",
                                                            blogFilePath.Replace(inDirPath, ".\\")//这里要改
                                                            ));

            if (!Directory.Exists(outPutFile.DirectoryName))
            {
                Directory.CreateDirectory(outPutFile.DirectoryName);
            }

            File.WriteAllText(outPutFile.FullName, blogContent);

            Console.WriteLine("[INFO]END REPLACE BLOG URL");
        }