private int ResolveIndirectURL(string baseURL, string path, string filename) { // Since we're here this is probably the mode you want. saveMode = SaveFileConflictMode.IncrementAlways; int downloads = 0; //URL provided is not a direct link to a zoomify folder. //Scan page for potential zoomify links. //Build relative links into fully formed URLs. //Batch download all found zoomify links. Console.WriteLine("Start scanning for zoomify links on this page : " + baseURL); // Check that the url is an absolute path. if (Dezoomify.isAbsoluteURL(baseURL)) { Uri datapath = new Uri(baseURL); // Now find all zoomify links. string htmlCode = Dezoomify.DownloadFile(baseURL); List<string> urls = new List<string>(); urls.AddRange(Dezoomify.ExtractURLs(baseURL, htmlCode)); foreach (string i in urls) { // Download & Save DownloadDirectURL(i, textBox2.Text, FinalizeMask(textBox3.Text, baseURL), true); downloads++; } } else { Console.WriteLine("Warning, URL was either relative, or malformed."); } return downloads; }
private void DownloadDirectURL(string url, string path, string filename, bool batchMode) { // Check for unwanted file replacement first. // Check if the filename already has the right file extension. string fileExtension = "."+outFormat.ToString().ToLowerInvariant(); string saveFilePath = path + "\\" + filename; if (!Dezoomify.StringIsLast(filename, fileExtension)) { saveFilePath += fileExtension; } if (File.Exists(saveFilePath) && saveMode == SaveFileConflictMode.Ask) { if (batchMode) { saveMode = QueryConflictDialog(saveFilePath, SaveFileConflictMode.ReplaceAlways, SaveFileConflictMode.IncrementAlways); } else { saveMode = QueryConflictDialog(saveFilePath, SaveFileConflictMode.Replace, SaveFileConflictMode.Increment); } } //Open Zoomify Path & Download Bitmap download = Dezoomify.Download(url); //Save Image on Success if (download != null) { lastSavedFile = Dezoomify.Save(download, outFormat, path, filename, saveMode); } download.Dispose(); //Check if the user has specified to always use one conflict resolving method for the rest of the program session. if (saveMode != SaveFileConflictMode.IncrementAlways && saveMode != SaveFileConflictMode.ReplaceAlways) { saveMode = SaveFileConflictMode.Ask; } }
//Shows a message dialog asking if the user would like to replace files that already exist. protected SaveFileConflictMode QueryConflictDialog(string filename, SaveFileConflictMode yesMode, SaveFileConflictMode noMode) { //Do Message Dialog string messageBoxText = "A file with the name: " + filename + " already exists. Do you want to replace it?"; string caption = "Warning - Potential Unwanted File Replacement"; MessageBoxButtons button = MessageBoxButtons.YesNoCancel; MessageBoxIcon icon = MessageBoxIcon.Warning; // Display message box DialogResult result = MessageBox.Show(messageBoxText, caption, button, icon); // Process message box results switch (result) { case DialogResult.Yes: // User pressed Yes button // ... return yesMode; case DialogResult.No: default: // User pressed No button // ... return noMode; case DialogResult.Cancel: // User pressed Cancel button // ... return SaveFileConflictMode.DoNothing; } }
// Starts download. private void button1_click(object sender, EventArgs e) { ResetStats(); //Start Timer opStartTime = System.DateTime.Now; SaveSettings(SettingsFile); // Downloads using a single URL. if (radioButton1.Checked) { if (isDirectURL(textBox1.Text)) { //URL provided is a direct link to a zoomify image folder. DownloadDirectURL(textBox1.Text, textBox2.Text, FinalizeMask(textBox3.Text, textBox1.Text)); itemsDownloaded++; } else { itemsDownloaded += ResolveIndirectURL(textBox1.Text, textBox2.Text, FinalizeMask(textBox3.Text, textBox1.Text)); } } else if (radioButton2.Checked) { // Batch download using a list of URL that point directly to the zoomify folder. // Open File StreamReader streamReader = new StreamReader(textBox4.Text); string text = streamReader.ReadToEnd(); streamReader.Close(); List<string> commands = new List<string>(); // Extract Paths string[] filter = { "\r\n", "\r", "\n" }; commands.AddRange(text.Split(filter, StringSplitOptions.RemoveEmptyEntries)); // Validate that each item is a url. foreach (string i in commands) { // Is it a URL? // Is the line a Direct URL? if ( Dezoomify.isAbsoluteURL(i) ) { if (isDirectURL(i)) { // Download & Save DownloadDirectURL(i, textBox2.Text, FinalizeMask(textBox3.Text, i), true); itemsDownloaded++; } else { itemsDownloaded += ResolveIndirectURL(i, textBox2.Text, FinalizeMask(textBox3.Text, i)); } } else { // Or is it a new parameter command? //Check if it is a batch command. string labrat = i.ToLowerInvariant().Trim(); if (labrat.IndexOf( batchCommandNewMask ) == 0) { //Then any text that comes after the command is now the new renaming mask. textBox3.Text = i.Substring(batchCommandNewMask.Length).Trim(); } else if (labrat.IndexOf(batchCommandNewFolder) == 0) { textBox2.Text = i.Substring(batchCommandNewFolder.Length).Trim(); } else if (labrat.IndexOf(batchCommandNewFormat) == 0) { outFormat = StringToImageFormat( labrat.Substring(batchCommandNewFormat.Length) ); } else if (labrat.IndexOf(batchCommandNewConflict) == 0) { saveMode = StringToConflictMode(labrat.Substring(batchCommandNewConflict.Length)); } else { Console.WriteLine("Bad line input :" + i); } } } } if (checkBox1.Checked) { OpenFolder(this, null); } //End Timer opEndTime = System.DateTime.Now; saveMode = SaveFileConflictMode.Ask; label4.Text = "Time Taken : " + (opEndTime - opStartTime).ToString() + " seconds. Items Downloaded : " + itemsDownloaded; }
// Save an image to file with file replacement protection settings. public static string Save(Bitmap image, ImageFormat format, string path, string filename, SaveFileConflictMode method) { // Is the textbox3 an absolute or relative path? string fileExtension = "." + format.ToString().ToLowerInvariant(); if (Dezoomify.StringIsLast(filename, fileExtension)) { filename = filename.Remove(filename.Length - fileExtension.Length); } string saveFilePath = path + "\\" + filename + fileExtension; switch (method) { case SaveFileConflictMode.Replace: case SaveFileConflictMode.ReplaceAlways: break; case SaveFileConflictMode.Increment: case SaveFileConflictMode.IncrementAlways: default: int fileCount = 0; while (File.Exists(saveFilePath)) { fileCount++; saveFilePath = path + "\\" + filename + " (" + fileCount.ToString() + ")" + fileExtension; } break; case SaveFileConflictMode.DoNothing: Console.WriteLine("Canceled download"); return ""; } Console.WriteLine("Finished download: " + saveFilePath); image.Save(saveFilePath, format); return saveFilePath; }