From 983e04ef38e24bd70d4690428ffb88257c21cfbd Mon Sep 17 00:00:00 2001 From: Thomas <71355143+thomas694@users.noreply.github.com> Date: Sun, 5 Jun 2022 15:56:20 +0200 Subject: [PATCH] Fix Errors in the clipboard monitor - The clipboard monitor was sometimes called twice. Some clipboard content not containing a valid blog URL was used to do a request. - Prevent duplicate calls "clipboard content updated" and check that content is a recognized blog URL before requesting it. Also improved the custom URL validation. --- src/TumblThree/SharedAssemblyInfo.cs | 4 +-- .../Controllers/ManagerController.cs | 35 +++++++++++-------- .../TumblThree.Domain/Models/UrlValidator.cs | 2 +- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/TumblThree/SharedAssemblyInfo.cs b/src/TumblThree/SharedAssemblyInfo.cs index c7b13c84..1cf812c0 100644 --- a/src/TumblThree/SharedAssemblyInfo.cs +++ b/src/TumblThree/SharedAssemblyInfo.cs @@ -12,5 +12,5 @@ [assembly: ComVisible(false)] [assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)] -[assembly: AssemblyVersion("2.6.2.0")] -[assembly: AssemblyFileVersion("2.6.2.0")] +[assembly: AssemblyVersion("2.6.3.0")] +[assembly: AssemblyFileVersion("2.6.3.0")] diff --git a/src/TumblThree/TumblThree.Applications/Controllers/ManagerController.cs b/src/TumblThree/TumblThree.Applications/Controllers/ManagerController.cs index ded1202a..2fd534b5 100644 --- a/src/TumblThree/TumblThree.Applications/Controllers/ManagerController.cs +++ b/src/TumblThree/TumblThree.Applications/Controllers/ManagerController.cs @@ -563,7 +563,7 @@ private async Task AddBlog() { try { - await AddBlogAsync(null); + await AddBlogAsync(null, false); } catch (WebException we) { @@ -590,7 +590,7 @@ private void CleanFailedAddBlog() { try { - IBlog blog = CheckIfCrawlableBlog(_crawlerService.NewBlogUrl).GetAwaiter().GetResult(); + IBlog blog = CheckIfCrawlableBlog(_crawlerService.NewBlogUrl, false).GetAwaiter().GetResult(); if (Directory.Exists(Path.Combine(Directory.GetParent(blog.Location).FullName, blog.Name)) && !Directory.EnumerateFileSystemEntries(Path.Combine(Directory.GetParent(blog.Location).FullName, blog.Name)).Any()) { @@ -635,7 +635,7 @@ private async Task ImportBlogs() var blogUris = fileContent.Split().Select(x => x.Trim()).Where(x => !string.IsNullOrWhiteSpace(x)); - await Task.Run(() => AddBlogBatchedAsync(blogUris)); + await Task.Run(() => AddBlogBatchedAsync(blogUris, false)); } catch (Exception ex) { @@ -794,14 +794,14 @@ private void CopyUrl() private bool CanCheckStatus() => ManagerViewModel.SelectedBlogFile != null; - private async Task AddBlogAsync(string blogUrl) + private async Task AddBlogAsync(string blogUrl, bool fromClipboard) { if (string.IsNullOrEmpty(blogUrl)) { blogUrl = _crawlerService.NewBlogUrl; } - IBlog blog = await CheckIfCrawlableBlog(blogUrl); + IBlog blog = await CheckIfCrawlableBlog(blogUrl, fromClipboard); blog = await CheckIfBlogIsHiddenTumblrBlogAsync(blog); @@ -878,10 +878,11 @@ private string GetIndexFolderPath(int CollectionId = 0) return Path.Combine(downloadLocation, "Index"); } - private async Task CheckIfCrawlableBlog(string blogUrl) + private async Task CheckIfCrawlableBlog(string blogUrl, bool fromClipboard) { if (!_blogFactory.IsValidBlogUrl(blogUrl)) { + if (fromClipboard) throw new Exception(); if (_blogFactory.IsValidUrl(blogUrl) && await _tumblrBlogDetector.IsTumblrBlogWithCustomDomainAsync(blogUrl)) return TumblrBlog.Create(blogUrl, GetIndexFolderPath(_shellService.Settings.ActiveCollectionId), _shellService.Settings.FilenameTemplate, true); throw new Exception($"The url '{blogUrl}' cannot be recognized as valid blog!"); @@ -910,6 +911,8 @@ private async Task CheckIfBlogIsHiddenTumblrBlogAsync(IBlog blog) return blog; } + private static string oldContent; + private void OnClipboardContentChanged(object sender, EventArgs e) { try @@ -918,10 +921,11 @@ private void OnClipboardContentChanged(object sender, EventArgs e) // Count each whitespace as new url string content = Clipboard.GetText(); - if (content == null) return; + if (content == null || oldContent == content) return; + oldContent = content; string[] urls = content.Split(); - Task.Run(() => AddBlogBatchedAsync(urls)); + Task.Run(() => AddBlogBatchedAsync(urls, true)); } catch (Exception ex) { @@ -930,7 +934,7 @@ private void OnClipboardContentChanged(object sender, EventArgs e) } } - private async Task AddBlogBatchedAsync(IEnumerable urls) + private async Task AddBlogBatchedAsync(IEnumerable urls, bool fromClipboard) { var semaphoreSlim = new SemaphoreSlim(25); @@ -938,7 +942,7 @@ private async Task AddBlogBatchedAsync(IEnumerable urls) QueueOnDispatcher.CheckBeginInvokeOnUI(() => Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait); try { - IEnumerable tasks = urls.Select(async url => await AddBlogsAsync(semaphoreSlim, url)); + IEnumerable tasks = urls.Select(async url => await AddBlogsAsync(semaphoreSlim, url, fromClipboard)); await Task.WhenAll(tasks); } finally @@ -949,17 +953,20 @@ private async Task AddBlogBatchedAsync(IEnumerable urls) } } - private async Task AddBlogsAsync(SemaphoreSlim semaphoreSlim, string url) + private async Task AddBlogsAsync(SemaphoreSlim semaphoreSlim, string url, bool fromClipboard) { try { await semaphoreSlim.WaitAsync(); - await AddBlogAsync(url); + await AddBlogAsync(url, fromClipboard); } catch (Exception e) { - Logger.Error("ManagerController.AddBlogsAsync: {0}", e); - _shellService.ShowError(e, Resources.CouldNotAddBlog, e.Message); + if (!fromClipboard) + { + Logger.Error("ManagerController.AddBlogsAsync: {0}", e); + _shellService.ShowError(e, Resources.CouldNotAddBlog, e.Message); + } } finally { diff --git a/src/TumblThree/TumblThree.Domain/Models/UrlValidator.cs b/src/TumblThree/TumblThree.Domain/Models/UrlValidator.cs index 33fb85f6..91920ec1 100644 --- a/src/TumblThree/TumblThree.Domain/Models/UrlValidator.cs +++ b/src/TumblThree/TumblThree.Domain/Models/UrlValidator.cs @@ -9,7 +9,7 @@ namespace TumblThree.Domain.Models public class UrlValidator : IUrlValidator { private readonly Regex tumbexRegex = new Regex("(http[A-Za-z0-9_/:.]*www.tumbex.com[A-Za-z0-9_/:.-]*tumblr/)"); - private readonly Regex urlRegex = new Regex("(^https?://[A-Za-z0-9.-]*[/]?$)"); + private readonly Regex urlRegex = new Regex(@"^(?:http(s)?:\/\/){1}?[\w.-]+(?:\.[\w\.-]+)+[/]??$"); private readonly Regex twitterRegex = new Regex("(^https?://twitter.com/[A-Za-z0-9_]+$)"); public bool IsValidTumblrUrl(string url)