Skip to content

Commit

Permalink
Added paste and drop for URLs including .url shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Feb 1, 2019
1 parent 6a7c53c commit ad6ae80
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 25 deletions.
3 changes: 3 additions & 0 deletions OnlyM.Core/OnlyM.Core.csproj
Expand Up @@ -185,8 +185,11 @@
<Compile Include="Subtitles\SubtitleStatus.cs" />
<Compile Include="Subtitles\SubtitleTiming.cs" />
<Compile Include="Utils\FFmpegUtils.cs" />
<Compile Include="Utils\FileDownloader.cs" />
<Compile Include="Utils\FileUtils.cs" />
<Compile Include="Subtitles\SubtitleFile.cs" />
<Compile Include="Utils\WebPageTitleHelper.cs" />
<Compile Include="Utils\WebUtils.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Properties\Resources.ca-ES.resx" />
Expand Down
10 changes: 10 additions & 0 deletions OnlyM.Core/Services/WebShortcuts/WebShortcutHelper.cs
Expand Up @@ -26,6 +26,16 @@ public string Uri
}
}

public static void Generate(string localFile, Uri remoteUri)
{
using (var writer = new StreamWriter(localFile))
{
writer.WriteLine("[InternetShortcut]");
writer.WriteLine($"URL={remoteUri}");
writer.Flush();
}
}

private void Init()
{
if (!_initialised)
Expand Down
4 changes: 2 additions & 2 deletions OnlyM.Core/Utils/FaviconHelper.cs
Expand Up @@ -23,7 +23,7 @@ private static byte[] GetIconImage(string websiteUrl, string iconUrl)

try
{
using (var wc = new WebClient())
using (var wc = WebUtils.CreateWebClient())
{
var uri = new Uri(iconUrl, UriKind.RelativeOrAbsolute);
if (!uri.IsAbsoluteUri)
Expand Down Expand Up @@ -68,7 +68,7 @@ private static string GetFaviconUrlFromRoot(string websiteUrl)

private static string GetFaviconUrlFromHtml(string websiteUrl)
{
using (var wc = new WebClient())
using (var wc = WebUtils.CreateWebClient())
{
var pageHtml = wc.DownloadString(websiteUrl);
var htmlDocument = new HtmlDocument();
Expand Down
32 changes: 32 additions & 0 deletions OnlyM.Core/Utils/FileDownloader.cs
@@ -0,0 +1,32 @@
namespace OnlyM.Core.Utils
{
using System;
using System.IO;
using Serilog;

public class FileDownloader
{
public bool Download(Uri remoteUri, string localFile, bool overwrite)
{
if (File.Exists(localFile) && !overwrite)
{
return false;
}

try
{
using (var wc = WebUtils.CreateWebClient())
{
wc.DownloadFile(remoteUri, localFile);
return true;
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, $"Could not download {remoteUri}");
}

return false;
}
}
}
34 changes: 33 additions & 1 deletion OnlyM.Core/Utils/FileUtils.cs
@@ -1,4 +1,6 @@
namespace OnlyM.Core.Utils
using System.Text.RegularExpressions;

namespace OnlyM.Core.Utils
{
using System;
using System.IO;
Expand Down Expand Up @@ -174,5 +176,35 @@ public static string GetUsersTempFolder()
{
return Path.GetTempPath();
}

/// <summary>
/// Strip illegal chars and reserved words from a candidate filename (should not include the directory path)
/// </summary>
/// <param name="filename">File name to sanitize.</param>
/// <returns>Sanitized file name.</returns>
/// <remarks>
/// http://stackoverflow.com/questions/309485/c-sharp-sanitize-file-name
/// </remarks>
public static string CoerceValidFileName(string filename)
{
var invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
var invalidReStr = $@"[{invalidChars}]+";

var reservedWords = new[]
{
"CON", "PRN", "AUX", "CLOCK$", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4",
"COM5", "COM6", "COM7", "COM8", "COM9", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4",
"LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
};

var sanitisedNamePart = Regex.Replace(filename, invalidReStr, "_");
foreach (var reservedWord in reservedWords)
{
var reservedWordPattern = $"^{reservedWord}\\.";
sanitisedNamePart = Regex.Replace(sanitisedNamePart, reservedWordPattern, "_reservedWord_.", RegexOptions.IgnoreCase);
}

return sanitisedNamePart;
}
}
}
24 changes: 24 additions & 0 deletions OnlyM.Core/Utils/WebPageTitleHelper.cs
@@ -0,0 +1,24 @@
namespace OnlyM.Core.Utils
{
using System;
using HtmlAgilityPack;

public static class WebPageTitleHelper
{
public static string Get(Uri webPageAddress)
{
using (var wc = WebUtils.CreateWebClient())
{
var pageHtml = wc.DownloadString(webPageAddress);
if (string.IsNullOrEmpty(pageHtml))
{
return null;
}

var document = new HtmlDocument();
document.LoadHtml(pageHtml);
return document.DocumentNode?.SelectSingleNode("html/head/title")?.InnerText;
}
}
}
}
17 changes: 17 additions & 0 deletions OnlyM.Core/Utils/WebUtils.cs
@@ -0,0 +1,17 @@
namespace OnlyM.Core.Utils
{
using System.Net;
using System.Text;

public static class WebUtils
{
private const string UserAgent = "SoundBox (+https://soundboxsoftware.com)";

public static WebClient CreateWebClient()
{
var wc = new WebClient { Encoding = Encoding.UTF8 };
wc.Headers.Add("user-agent", UserAgent);
return wc;
}
}
}
1 change: 1 addition & 0 deletions OnlyM.sln.DotSettings
Expand Up @@ -3,6 +3,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Behaviours/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Corbett/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Deciseconds/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Downloader/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=FFME/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=ffmpeg/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Fmpeg/@EntryIndexedValue">True</s:Boolean>
Expand Down
16 changes: 14 additions & 2 deletions OnlyM/Models/MediaItem.cs
Expand Up @@ -41,7 +41,8 @@ public sealed class MediaItem : ObservableObject
private bool _isRollingSlideshow;
private bool _allowUseMirror;
private string _miscText;

private string _fileNameAsSubTitle;

public event EventHandler PlaybackPositionChangedEvent;

public Guid Id { get; set; }
Expand Down Expand Up @@ -201,7 +202,18 @@ public string SortKey

public string FilePath { get; set; }

public string FileNameAsSubTitle { get; set; }
public string FileNameAsSubTitle
{
get => _fileNameAsSubTitle;
set
{
if (_fileNameAsSubTitle != value)
{
_fileNameAsSubTitle = value;
RaisePropertyChanged();
}
}
}

public long LastChanged { get; set; }

Expand Down

0 comments on commit ad6ae80

Please sign in to comment.