Skip to content

Commit

Permalink
Merge pull request #6 from mikeMoreno/feature/changes
Browse files Browse the repository at this point in the history
Feature/changes
  • Loading branch information
mikeMoreno authored Feb 15, 2022
2 parents 42a8f06 + 88e4e54 commit 00da172
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 117 deletions.
13 changes: 13 additions & 0 deletions Waffle.Lib/HtmlResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Waffle.Lib
{
public class HtmlResponse : Response
{
public string Url { get; set; }
}
}
101 changes: 101 additions & 0 deletions Waffle.Lib/LinkLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,106 @@ public LinkLine(string line) : base(line)
DisplayString = Raw;
ItemType = GetItemType(Raw);
}

private static ItemType GetItemType(string absoluteUrl)
{
absoluteUrl = absoluteUrl.Trim();

if (absoluteUrl.StartsWith("http://") || absoluteUrl.StartsWith("https://"))
{
return ItemType.HTML;
}

var parsedUrl = UrlValidator.ParseUrl(absoluteUrl);

var itemType = ParseItemType(parsedUrl);

if (itemType == "0")
{
return ItemType.Text;
}
else if (itemType == "1")
{
return ItemType.Menu;
}
else if (itemType == "p")
{
return ItemType.PNG;
}
else if (itemType == "I")
{
return ItemType.Image;
}
else if (itemType == "9")
{
return ItemType.BinaryFile;
}
else
{
return GetItemTypeFromFileExtension(absoluteUrl);
}
}

private static string ParseItemType(string absoluteUrl)
{
string path = null;

if (absoluteUrl.Contains('/'))
{
path = absoluteUrl[(absoluteUrl.IndexOf('/') + 1)..];
}

if (path == null)
{
return null;
}

if (path.Length <= 1)
{
return path;
}

if (!path.Contains('/'))
{
return path;
}

if (path[..path.IndexOf('/')].Length == 1)
{
path = path[..path.IndexOf('/')];
}

return path;
}

private static ItemType GetItemTypeFromFileExtension(string absoluteUrl)
{
if (absoluteUrl.EndsWith(".jpg"))
{
return ItemType.Image;
}

if (absoluteUrl.EndsWith(".png"))
{
return ItemType.PNG;
}

if (absoluteUrl.EndsWith(".tar.gz"))
{
return ItemType.BinaryFile;
}

if (absoluteUrl.EndsWith(".zip"))
{
return ItemType.BinaryFile;
}

if (absoluteUrl.EndsWith(".txt"))
{
return ItemType.Text;
}

return ItemType.Unknown;
}
}
}
119 changes: 22 additions & 97 deletions Waffle.Lib/SelectorLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,31 @@ public string GetUserFriendlyName()

public virtual string GetLink()
{
if(string.IsNullOrWhiteSpace(HostName) && string.IsNullOrWhiteSpace(Selector))
if (ItemType == ItemType.HTML)
{
return Raw;
var url = Selector;

if (url != null && url.StartsWith("URL:"))
{
url = url[4..];
}

if(string.IsNullOrWhiteSpace(url))
{
url = Raw;
}

return url;
}
else
{
if (string.IsNullOrWhiteSpace(HostName) && string.IsNullOrWhiteSpace(Selector))
{
return Raw;
}

return $"gopher://{HostName}{Selector}";
return $"gopher://{HostName}{Selector}";
}
}

private static ItemType MapItemType(string itemTypePrefix)
Expand Down Expand Up @@ -120,99 +139,5 @@ private static string StripNewline(string line)

return line;
}

private static ItemType GetItemTypeFromFileExtension(string absoluteUrl)
{
if (absoluteUrl.EndsWith(".jpg"))
{
return ItemType.Image;
}

if (absoluteUrl.EndsWith(".png"))
{
return ItemType.PNG;
}

if (absoluteUrl.EndsWith(".tar.gz"))
{
return ItemType.BinaryFile;
}

if (absoluteUrl.EndsWith(".zip"))
{
return ItemType.BinaryFile;
}

if (absoluteUrl.EndsWith(".txt"))
{
return ItemType.Text;
}

return ItemType.Unknown;
}

private static string ParseItemType(string absoluteUrl)
{
string path = null;

if (absoluteUrl.Contains('/'))
{
path = absoluteUrl[(absoluteUrl.IndexOf('/') + 1)..];
}

if (path == null)
{
return null;
}

if (path.Length <= 1)
{
return path;
}

if (!path.Contains('/'))
{
return path;
}

if (path[..path.IndexOf('/')].Length == 1)
{
path = path[..path.IndexOf('/')];
}

return path;
}

protected static ItemType GetItemType(string absoluteUrl)
{
var parsedUrl = UrlValidator.ParseUrl(absoluteUrl);

var itemType = ParseItemType(parsedUrl);

if (itemType == "0")
{
return ItemType.Text;
}
else if (itemType == "1")
{
return ItemType.Menu;
}
else if (itemType == "p")
{
return ItemType.PNG;
}
else if (itemType == "I")
{
return ItemType.Image;
}
else if (itemType == "9")
{
return ItemType.BinaryFile;
}
else
{
return GetItemTypeFromFileExtension(absoluteUrl);
}
}
}
}
9 changes: 9 additions & 0 deletions Waffle.Lib/WaffleLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public async Task<Response> GetAsync(SelectorLine selectorLine)
ItemType.PNG => await GetPngFileAsync(link),
ItemType.Image => await GetImageFileAsync(link),
ItemType.BinaryFile => await GetBinaryFile(link),
ItemType.HTML => GetHtmlLink(link),
_ => await GetMenuAsync(link),
};
}
Expand Down Expand Up @@ -150,5 +151,13 @@ private async Task<BinaryResponse> GetBinaryFile(string absoluteUrl)
return Response.Error<BinaryResponse>(e);
}
}

private HtmlResponse GetHtmlLink(string absoluteUrl)
{
return new HtmlResponse()
{
Url = absoluteUrl,
};
}
}
}
Loading

0 comments on commit 00da172

Please sign in to comment.