Skip to content

Commit

Permalink
Fix Errors in the clipboard monitor
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
thomas694 committed Jun 5, 2022
1 parent f4b4024 commit 983e04e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/TumblThree/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ private async Task AddBlog()
{
try
{
await AddBlogAsync(null);
await AddBlogAsync(null, false);
}
catch (WebException we)
{
Expand All @@ -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())
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -878,10 +878,11 @@ private string GetIndexFolderPath(int CollectionId = 0)
return Path.Combine(downloadLocation, "Index");
}

private async Task<IBlog> CheckIfCrawlableBlog(string blogUrl)
private async Task<IBlog> 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!");
Expand Down Expand Up @@ -910,6 +911,8 @@ private async Task<IBlog> CheckIfBlogIsHiddenTumblrBlogAsync(IBlog blog)
return blog;
}

private static string oldContent;

private void OnClipboardContentChanged(object sender, EventArgs e)
{
try
Expand All @@ -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)
{
Expand All @@ -930,15 +934,15 @@ private void OnClipboardContentChanged(object sender, EventArgs e)
}
}

private async Task AddBlogBatchedAsync(IEnumerable<string> urls)
private async Task AddBlogBatchedAsync(IEnumerable<string> urls, bool fromClipboard)
{
var semaphoreSlim = new SemaphoreSlim(25);

await _addBlogSemaphoreSlim.WaitAsync();
QueueOnDispatcher.CheckBeginInvokeOnUI(() => Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait);
try
{
IEnumerable<Task> tasks = urls.Select(async url => await AddBlogsAsync(semaphoreSlim, url));
IEnumerable<Task> tasks = urls.Select(async url => await AddBlogsAsync(semaphoreSlim, url, fromClipboard));
await Task.WhenAll(tasks);
}
finally
Expand All @@ -949,17 +953,20 @@ private async Task AddBlogBatchedAsync(IEnumerable<string> 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
{
Expand Down
2 changes: 1 addition & 1 deletion src/TumblThree/TumblThree.Domain/Models/UrlValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 983e04e

Please sign in to comment.