Exemple #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();
        }
Exemple #2
0
        public static string GetExistingFilePath()
        {
            string path = DragonUtil.RemoveAllQuotes(Console.ReadLine());

            if (File.Exists(path))
            {
                return(path);
            }

            ColoredConsole.WriteLineWithColor("File " + path + " doesn't exist!", ConsoleColor.Red);
            Console.WriteLine("");
            return(null);
        }
Exemple #3
0
        public void ConvertAndUpload(ConvertAndUploadArguments args)
        {
            string htmlPath = ConvertToHtml(args.MarkdownPath, args.HtmlPath, new ConverterOptions(args.FirstImageRightAligned, args.ConvertImageWithAltToCaption));

            Console.WriteLine("Starting image upload...");

            if (UploadImages(args.MarkdownPath, htmlPath, args.OnlyUpdateHtmlFile, args.Username, args.Password))
            {
                Console.WriteLine("Uploaded all images succesfully! The links were also replaced. Enjoy!");
            }
            else
            {
                ColoredConsole.WriteLineWithColor("ERROR: Something went wrong while uploading.", ConsoleColor.Red);
            }

            CoreConsoleShared.PauseAndQuit();
        }
Exemple #4
0
        public static string GetNewFilePath()
        {
            string path = DragonUtil.RemoveAllQuotes(Console.ReadLine());

            var directoryName = Path.GetDirectoryName(path);

            if (Directory.Exists(directoryName) &&
                DragonUtil.CheckFolderWritePermission(directoryName))
            {
                return(path);
            }

            ColoredConsole.WriteLineWithColor("Invalid folder, can't write to to: " + directoryName, ConsoleColor.Red);

            Console.WriteLine("");
            return(null);
        }
        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);
        }
Exemple #6
0
        public string ConvertToHtml(string markdownPath, string htmlPath, ConverterOptions options)
        {
            Console.WriteLine("Converting " + markdownPath + " to RW WordPress ready HTML...");

            if (htmlPath == null)
            {
                htmlPath = DragonUtil.GetFullPathWithoutExtension(markdownPath) + ".html";
            }

            if (DragonUtil.CheckFolderWritePermission(Path.GetDirectoryName(htmlPath)))
            {
                Converter.ConvertMarkdownFileToHtmlFile(markdownPath, htmlPath, options);
                Console.WriteLine("Saved HTML to custom location: " + htmlPath);
            }
            else
            {
                ColoredConsole.WriteLineWithColor("Conversion aborted, can't write to " + htmlPath, ConsoleColor.Red);
            }

            return(htmlPath);
        }
Exemple #7
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);
        }