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