public static Dictionary <string, string> GetKindleList() { /* If Kindle is not connected, it will use the list of IDs stored from the last time it was. * Returns a dictionary in the format: * Dictionary * { * MD5 hash: Filepath * } */ Dictionary <string, string> fileHashes = new Dictionary <string, string>(); var progressWindow = new ProgressWindow(); progressWindow.Show(); progressWindow.StopProgress(); Task.Factory.StartNew(() => { foreach (DriveInfo drive in DriveInfo.GetDrives()) { string driveName = drive.IsReady ? drive.VolumeLabel : null; // Makes sure we only try to query mounted drives if (driveName != null && driveName.Contains("Kindle")) // TODO more robust Kindle detection { // Kindle is connected, get current library progressWindow.ChangeText("Locating Kindle contents..."); string[] kindleFiles = FileTools.Walker(new List <string> { drive.Name }); progressWindow.SetProgressBarMax(kindleFiles.Length); progressWindow.StartProgress(); progressWindow.ChangeText("Processing Kindle files..."); foreach (string file in kindleFiles) { string ext = Path.GetExtension(file); if (ext.StartsWith(".")) { ext = ext.Substring(1); } if (Constants.AllowedExt.Contains(ext)) { string workHash = WorkIdent.Hash(file); fileHashes[workHash] = file; } progressWindow.UpdateProgressBar(); } MiddleDude.StoreKindle(fileHashes); } } }); progressWindow.Close(); Console.WriteLine("Kindle fetch complete!"); try { fileHashes = MiddleDude.LoadKindle(); return(fileHashes); } catch (InvalidOperationException) { return(fileHashes); } }
private string DownloadEbook(String downloadUrl) { if (!CreateFolder(userSettings.DownloadLocation)) { return(null); } int attempts = 0; bool fileDownloaded = false; string filename; Uri uri = new Uri(downloadUrl); string work_id = uri.Segments[2]; work_id = work_id.Remove(work_id.Length - 1); string downloadUrlClean = uri.GetLeftPart(UriPartial.Path); filename = FileTools.ToAllowedFileName(uri.Segments.Last()); filename = $"{work_id}-{filename}"; filename = $"{userSettings.DownloadLocation}/{filename}"; if (File.Exists(filename)) { // TODO popup window to allow for replacing existing file? checkbox to enable/disable feature? Log("This file already exists, skipping..."); return(null); } do { var doneEvent = new AutoResetEvent(false); // TODO dispose using (var w = new WebClient()) { w.DownloadFileCompleted += (s, e) => { AttemptCooldown(attempts); attempts++; if (!e.Cancelled && e.Error == null) { fileDownloaded = true; } else if (!e.Cancelled && e.Error != null) { if (attempts < userSettings.DownloadMaxAttempts) { Log($"Failed to download {downloadUrl}, retrying..."); } else { Log($"Failed to download {downloadUrl}, max retries reached."); } } doneEvent.Set(); }; lock (this.pendingDownloads) { if (this.userCancel) { return(null); } this.pendingDownloads.Add(w); w.DownloadFileAsync(new Uri(downloadUrl), filename); } doneEvent.WaitOne(); lock (this.pendingDownloads) { downloadedFiles.Add(filename); this.pendingDownloads.Remove(w); } } }while (!fileDownloaded && attempts < userSettings.DownloadMaxAttempts); return(filename); }