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");
        }