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

150 lines
4.9 KiB
C#
Raw Normal View History

2019-06-07 08:59:07 +00:00
using System;
2021-10-12 10:14:33 +00:00
using System.IO;
using System.Net.Http;
2019-06-07 08:59:07 +00:00
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
2020-02-17 08:58:14 +00:00
using System.Web;
2019-12-20 11:17:01 +00:00
2020-02-17 08:58:14 +00:00
using ASC.Common;
using ASC.Core.Common.Configuration;
2019-06-07 08:59:07 +00:00
using ASC.FederatedLogin.LoginProviders;
2020-09-18 07:59:23 +00:00
using ASC.Security.Cryptography;
2020-02-17 08:58:14 +00:00
using ASC.Web.Studio.Utility;
using Microsoft.Extensions.Configuration;
2019-06-07 08:59:07 +00:00
namespace ASC.Web.Core.Utility
{
public interface IUrlShortener
{
Task<string> GetShortenLinkAsync(string shareLink);
2019-06-07 08:59:07 +00:00
}
2020-10-19 15:53:15 +00:00
[Scope]
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)
{
2019-12-20 11:17:01 +00:00
if (ConsumerFactory.Get<BitlyLoginProvider>().Enabled)
2019-06-07 08:59:07 +00:00
{
2019-12-20 11:17:01 +00:00
_instance = new BitLyShortener(ConsumerFactory);
2019-06-07 08:59:07 +00:00
}
2020-05-22 09:41:14 +00:00
else if (!string.IsNullOrEmpty(Configuration["web:url-shortener:value"]))
2019-06-07 08:59:07 +00:00
{
2022-01-13 11:19:39 +00:00
_instance = new OnlyoShortener(Configuration, CommonLinkUtility, MachinePseudoKeys, ClientFactory);
2019-06-07 08:59:07 +00:00
}
else
{
_instance = new NullShortener();
}
}
return _instance;
2020-09-18 07:59:23 +00:00
}
set
{
_instance = value;
2019-06-07 08:59:07 +00:00
}
2020-02-17 08:58:14 +00:00
}
2020-08-12 09:58:08 +00:00
private IConfiguration Configuration { get; }
private ConsumerFactory ConsumerFactory { get; }
2020-09-18 07:59:23 +00:00
private CommonLinkUtility CommonLinkUtility { get; }
private MachinePseudoKeys MachinePseudoKeys { get; }
2022-01-13 11:19:39 +00:00
private IHttpClientFactory ClientFactory { get; }
2020-09-18 07:59:23 +00:00
public UrlShortener(
IConfiguration configuration,
ConsumerFactory consumerFactory,
CommonLinkUtility commonLinkUtility,
2022-01-13 11:19:39 +00:00
MachinePseudoKeys machinePseudoKeys,
IHttpClientFactory clientFactory)
2020-02-17 08:58:14 +00:00
{
Configuration = configuration;
ConsumerFactory = consumerFactory;
2020-09-18 07:59:23 +00:00
CommonLinkUtility = commonLinkUtility;
MachinePseudoKeys = machinePseudoKeys;
2022-01-13 11:19:39 +00:00
ClientFactory = clientFactory;
2019-06-07 08:59:07 +00:00
}
}
public class BitLyShortener : IUrlShortener
2020-02-17 08:58:14 +00:00
{
public BitLyShortener(ConsumerFactory consumerFactory)
{
ConsumerFactory = consumerFactory;
}
private ConsumerFactory ConsumerFactory { get; }
public Task<string> GetShortenLinkAsync(string shareLink)
2019-06-07 08:59:07 +00:00
{
return Task.FromResult(ConsumerFactory.Get<BitlyLoginProvider>().GetShortenLink(shareLink));
2019-06-07 08:59:07 +00:00
}
}
public class OnlyoShortener : IUrlShortener
{
private readonly string url;
private readonly string internalUrl;
2020-09-18 07:59:23 +00:00
private readonly byte[] sKey;
2019-06-07 08:59:07 +00:00
2022-01-13 11:19:39 +00:00
private CommonLinkUtility CommonLinkUtility { get; }
private IHttpClientFactory ClientFactory { get; }
2019-06-07 08:59:07 +00:00
2020-09-18 07:59:23 +00:00
public OnlyoShortener(
IConfiguration configuration,
CommonLinkUtility commonLinkUtility,
2022-01-13 11:19:39 +00:00
MachinePseudoKeys machinePseudoKeys,
IHttpClientFactory clientFactory)
2019-06-07 08:59:07 +00:00
{
2020-05-22 09:41:14 +00:00
url = configuration["web:url-shortener:value"];
internalUrl = configuration["web:url-shortener:internal"];
2020-09-18 07:59:23 +00:00
sKey = machinePseudoKeys.GetMachineConstant();
2019-06-07 08:59:07 +00:00
2022-01-18 12:40:28 +00:00
if (!url.EndsWith('/'))
2020-02-17 08:58:14 +00:00
url += '/';
CommonLinkUtility = commonLinkUtility;
2022-01-13 11:19:39 +00:00
ClientFactory = clientFactory;
2020-02-17 08:58:14 +00:00
}
public async Task<string> GetShortenLinkAsync(string shareLink)
2021-10-12 10:14:33 +00:00
{
var request = new HttpRequestMessage();
request.RequestUri = new Uri(internalUrl + "?url=" + HttpUtility.UrlEncode(shareLink));
request.Headers.Add("Authorization", CreateAuthToken());
request.Headers.Add("Encoding", Encoding.UTF8.ToString());//todo check
2022-01-13 11:19:39 +00:00
var httpClient = ClientFactory.CreateClient();
using var response = await httpClient.SendAsync(request);
using var stream = await response.Content.ReadAsStreamAsync();
2021-11-24 19:34:39 +00:00
using var rs = new StreamReader(stream);
return CommonLinkUtility.GetFullAbsolutePath(url + await rs.ReadToEndAsync());
2019-06-07 08:59:07 +00:00
}
private string CreateAuthToken(string pkey = "urlShortener")
{
2020-09-18 07:59:23 +00:00
using var hasher = new HMACSHA1(sKey);
2020-02-17 08:58:14 +00:00
var now = DateTime.UtcNow.ToString("yyyyMMddHHmmss");
var hash = Convert.ToBase64String(hasher.ComputeHash(Encoding.UTF8.GetBytes(string.Join("\n", now, pkey))));
2022-01-14 13:12:37 +00:00
return $"ASC {pkey}:{now}:{hash}";
2019-06-07 08:59:07 +00:00
}
}
public class NullShortener : IUrlShortener
{
public Task<string> GetShortenLinkAsync(string shareLink)
2019-06-07 08:59:07 +00:00
{
return null;
}
2020-02-17 08:58:14 +00:00
}
2019-06-07 08:59:07 +00:00
}