コード例 #1
0
        public static void StartFileDeletion(List <string> imageIDs)
        {
            Console.WriteLine("------------------------------------------------");

            Console.WriteLine("               _                            __ ");
            Console.WriteLine(" ____         | |_      _____         _    |  |");
            Console.WriteLine("|    \\ ___ ___|_| |_   |  _  |___ ___|_|___|  |");
            Console.WriteLine("|  |  | . |   | |  _|  |   __| .'|   | |  _|__|");
            Console.WriteLine("|____/|___|_|_| |_|    |__|  |__,|_|_|_|___|__|");
            Console.WriteLine("");
            Console.WriteLine("------------------------------------------------");

            Console.WriteLine("Open source Hacked Core Restorative Automatic Program (OH CRAP) v2");
            Console.WriteLine("------------");
            Console.WriteLine("Deleting uploaded images...");

            foreach (string iD in imageIDs)
            {
                var result = WordPressConnector.Delete(Convert.ToInt32(iD));
                if (result)
                {
                    Console.WriteLine("Deleted file with id " + iD);
                }
                else
                {
                    ColoredConsole.WriteLineWithColor("Failed to delete file with id " + iD, ConsoleColor.Red);
                }
            }

            Console.WriteLine("Cleanup complete! Press any key to exit.");
            Console.ReadKey();
        }
コード例 #2
0
        private void btnVerify_Click(object sender, EventArgs e)
        {
            WordPressConnector.InitializeWordPress(txtUsername.Text, txtPassword.Text);

            lblStatus.Text = "Verifying...";
            SetLoginControlsEnabledState(false);

            GetProfileResponse user = WordPressConnector.GetUserProfile();

            if (user != null)
            {
                MessageBox.Show("Thanks " + user.FirstName + "!" + "\nYou're ready to upload.");
                btnUpload.Enabled = true;
            }
            else
            {
                MessageBox.Show("Connection failed",
                                "Can't connect to RW Wordpress!");
                SetLoginControlsEnabledState(true);
                lblStatus.Text = "Can't connect to RW WordPress, please try again.";
                return;
            }

            if (chkSaveCredentials.Checked)
            {
                // Save credentials
                Settings.Default.CredentialsSaved = true;
                Settings.Default.Username         = txtUsername.Text;
                Settings.Default.Password         = txtPassword.Text;
                Settings.Default.Save();
            }
        }
コード例 #3
0
        private void TryToDeleteImages()
        {
            lblStatus.Text = "Rolling back...";

            WordPressConfig config = new WordPressConfig("https://www.raywenderlich.com", txtUsername.Text, txtPassword.Text);

            try
            {
                // Something went wrong, delete all uploaded images
                for (var i = 0; i < _imageIdList.Count; i++)
                {
                    int  id      = _imageIdList[i];
                    bool deleted = WordPressConnector.Delete(id);
                    progressUpload.Value--;

                    if (!deleted)
                    {
                        MessageBox.Show("Couldn't delete image: " + ImageUploadData.ImageUrls[i]);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
                MessageBox.Show("Image rollback failed. Please delete the remaining images manually.");
                return;
            }

            MessageBox.Show("Rollback succesfull!");
        }
コード例 #4
0
        static void Main(string[] args)
        {
            WordPressConnector wordpress = new WordPressConnector("http://localhost/wordpress");
            var task = wordpress.GetAuthorAsync(1);

            task.Wait();
            var result = task.Result;
        }
コード例 #5
0
        private static GetProfileResponse AskForCredentials(GetProfileResponse user)
        {
            Console.WriteLine("Username:"******"Pasword:");
            string password = Console.ReadLine();

            WordPressConnector.InitializeWordPress(username, password);
            user = WordPressConnector.GetUserProfile();

            if (user == null)
            {
                ColoredConsole.WriteLineWithColor("Incorrect credentials / can't connect to RW WordPress.", ConsoleColor.Red);
                Console.WriteLine("Please try again.");
            }
            return(user);
        }
コード例 #6
0
        private bool UploadImages(string markdownPath, string htmlPath, bool onlyUpdateHtml, string username, string password)
        {
            Console.WriteLine("Logging into RW WordPress..");
            WordPressConnector.InitializeWordPress(username, password);
            GetProfileResponse user = WordPressConnector.GetUserProfile();

            if (user == null)
            {
                ColoredConsole.WriteLineWithColor("Login failed. Please check your credentials and internet connection.", ConsoleColor.Red);
            }

            Console.WriteLine("");
            Console.WriteLine("Login succesful! Thanks for using the app, " + user.FirstName + "!");
            Console.WriteLine("Gathering files...");
            // Get image paths
            List <string> fullImagePaths  = new List <string>();
            List <string> localImagePaths = new List <string>();


            string markdownText = "";
            string htmlText     = "";

            using (StreamReader sr = new StreamReader(markdownPath))
            {
                markdownText = sr.ReadToEnd();
            }

            using (StreamReader sr = new StreamReader(htmlPath))
            {
                htmlText = sr.ReadToEnd();
            }

            var links = Converter.FindAllImageLinksInHtml(htmlText, Path.GetDirectoryName(htmlPath));

            if (links.Count == 0)
            {
                Console.WriteLine("No images found. Aborting upload");
                return(false);
            }

            foreach (ImageLinkData link in links)
            {
                fullImagePaths.Add(link.FullImagePath);
                localImagePaths.Add(link.LocalImagePath);
            }

            Console.WriteLine("");
            Console.WriteLine(fullImagePaths.Count + " image paths found:");

            foreach (string path in fullImagePaths)
            {
                Console.WriteLine(path + " (" + new FileInfo(path).Length / 1024 + " kb)");
            }

            List <string> imageUrls = new List <string>();
            List <string> imageIDs  = new List <string>();

            // Upload images
            for (var i = 0; i < fullImagePaths.Count; i++)
            {
                string path = fullImagePaths[i];

                Console.WriteLine("Uploading: " + " (" + (i + 1) + "/" + fullImagePaths.Count + ")" + path + "...");

                var result = WordPressConnector.UploadFile(path);

                if (result != null)
                {
                    imageUrls.Add(result.FileResponseStruct.Url);
                    imageIDs.Add(result.FileResponseStruct.Id.ToString());
                }
                else
                {
                    Console.WriteLine("Image upload failed! Aborting upload and going into file cleanup mode...");
                    CoreConsoleShared.StartFileDeletion(imageIDs);
                    return(false);
                }
            }

            // Update markdown & html
            Console.WriteLine("Starting link replacer...");
            Converter.ReplaceLocalImageLinksWithUrls(markdownPath, markdownText, htmlPath, htmlText, onlyUpdateHtml, localImagePaths, imageUrls);
            return(true);
        }
コード例 #7
0
 public HomeController()
 {
     wpc = new WordPressConnector();
 }
コード例 #8
0
        private void UploadImages()
        {
            lblStatus.Text = "Starting Upload...";
            _imageIdList.Clear();
            progressUpload.Maximum = ImageUploadData.FullImagePaths.Count;

            try
            {
                foreach (string path in ImageUploadData.FullImagePaths)
                {
                    lblStatus.Text = "Uploading " + path + "...";
                    lblStatus.Refresh();
                    UploadFileResponse result = null;

                    if (chkOptimizeImages.Checked && path.ToLower().EndsWith("png"))
                    {
                        var quantisizer = new WuQuantizer();
                        var bitmap      = new Bitmap(path);

                        using (var quantized = quantisizer.QuantizeImage(bitmap))
                        {
                            quantized.Save(Application.StartupPath + "/" + Path.GetFileName(path));
                        }

                        result = WordPressConnector.UploadFile(Application.StartupPath + "/" + Path.GetFileName(path));
                    }
                    else
                    {
                        result = WordPressConnector.UploadFile(path);
                    }

                    if (result != null)
                    {
                        ImageUploadData.ImageUrls.Add(result.FileResponseStruct.Url);
                        _imageIdList.Add(result.FileResponseStruct.Id);
                    }
                    else
                    {
                        _errorInUpload = true;
                        MessageBox.Show(
                            "Something went wrong while uploading. Press OK to attempt rollback, make sure you're connected to the internet and can access the RW WordPress before continuing.",
                            "Error while uploading");
                        TryToDeleteImages();
                    }

                    progressUpload.Value++;
                }

                if (chkOptimizeImages.Checked)
                {
                    //Remove optimized images
                    foreach (string path in ImageUploadData.FullImagePaths)
                    {
                        if (File.Exists(Application.StartupPath + "/" + Path.GetFileName(path)))
                        {
                            File.Delete(Application.StartupPath + "/" + Path.GetFileName(path));
                        }
                    }
                }

                lblStatus.Text = "Uploading complete!";
            }
            catch (Exception e)
            {
                _errorInUpload = true;
                Console.WriteLine(e);
                TryToDeleteImages();
            }
        }
コード例 #9
0
 private void CheckConnectionToRW()
 {
     WordPressConnector.CanConnectToRW();
 }