DocSpace-client/web/ASC.Web.Core/Utility/UrlShortener.cs

102 lines
3.1 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
using System;
using System.Net;
using System.Security.Cryptography;
using System.Text;
2019-08-15 12:04:42 +00:00
using System.Web;
2019-06-07 08:59:07 +00:00
using ASC.FederatedLogin.LoginProviders;
using ASC.Web.Studio.Utility;
2019-09-24 10:32:12 +00:00
using Microsoft.Extensions.Configuration;
2019-06-07 08:59:07 +00:00
namespace ASC.Web.Core.Utility
{
public interface IUrlShortener
{
2019-09-19 15:55:44 +00:00
string GetShortenLink(string shareLink, CommonLinkUtility commonLinkUtility);
2019-06-07 08:59:07 +00:00
}
2019-09-24 10:32:12 +00:00
public class UrlShortener
2019-06-07 08:59:07 +00:00
{
2019-09-24 10:32:12 +00:00
public bool Enabled { get { return !(Instance is NullShortener); } }
2019-06-07 08:59:07 +00:00
2019-09-24 10:32:12 +00:00
private IUrlShortener _instance;
public IUrlShortener Instance
2019-06-07 08:59:07 +00:00
{
get
{
if (_instance == null)
{
if (BitlyLoginProvider.Enabled)
{
_instance = new BitLyShortener();
}
2019-09-24 10:32:12 +00:00
else if (!string.IsNullOrEmpty(Configuration["web:url-shortener"]))
2019-06-07 08:59:07 +00:00
{
2019-09-24 10:32:12 +00:00
_instance = new OnlyoShortener(Configuration);
2019-06-07 08:59:07 +00:00
}
else
{
_instance = new NullShortener();
}
}
return _instance;
}
2019-09-24 10:32:12 +00:00
}
public IConfiguration Configuration { get; }
public UrlShortener(IConfiguration configuration)
{
Configuration = configuration;
2019-06-07 08:59:07 +00:00
}
}
public class BitLyShortener : IUrlShortener
{
2019-09-19 15:55:44 +00:00
public string GetShortenLink(string shareLink, CommonLinkUtility commonLinkUtility)
2019-06-07 08:59:07 +00:00
{
return BitlyLoginProvider.GetShortenLink(shareLink);
}
}
public class OnlyoShortener : IUrlShortener
{
private readonly string url;
private readonly string internalUrl;
private readonly string sKey;
2019-09-24 10:32:12 +00:00
public OnlyoShortener(IConfiguration configuration)
2019-06-07 08:59:07 +00:00
{
2019-09-24 10:32:12 +00:00
url = configuration["web.url-shortener"];
internalUrl = configuration["web.url-shortener.internal"];
sKey = configuration["core.machinekey"];
2019-06-07 08:59:07 +00:00
if (!url.EndsWith("/"))
url += '/';
}
2019-09-19 15:55:44 +00:00
public string GetShortenLink(string shareLink, CommonLinkUtility commonLinkUtility)
2019-06-07 08:59:07 +00:00
{
2019-08-15 15:08:40 +00:00
using var client = new WebClient { Encoding = Encoding.UTF8 };
client.Headers.Add("Authorization", CreateAuthToken());
2019-09-19 15:55:44 +00:00
return commonLinkUtility.GetFullAbsolutePath(url + client.DownloadString(new Uri(internalUrl + "?url=" + HttpUtility.UrlEncode(shareLink))));
2019-06-07 08:59:07 +00:00
}
private string CreateAuthToken(string pkey = "urlShortener")
{
2019-08-15 15:08:40 +00:00
using var hasher = new HMACSHA1(Encoding.UTF8.GetBytes(sKey));
var now = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
var hash = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("\n", now, pkey))));
return string.Format("ASC {0}:{1}:{2}", pkey, now, hash);
2019-06-07 08:59:07 +00:00
}
}
public class NullShortener : IUrlShortener
{
2019-09-19 15:55:44 +00:00
public string GetShortenLink(string shareLink, CommonLinkUtility commonLinkUtility)
2019-06-07 08:59:07 +00:00
{
return null;
}
}
}