mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-21 13:53:19 -07:00
All Sln changes
This commit is contained in:
parent
b5855f2644
commit
796f0fc188
615 changed files with 68 additions and 747 deletions
46
Ombi.Helpers/Analytics/Action.cs
Normal file
46
Ombi.Helpers/Analytics/Action.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Action.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Helpers.Analytics
|
||||
{
|
||||
public enum Action
|
||||
{
|
||||
Donate,
|
||||
ClickButton,
|
||||
Delete,
|
||||
Create,
|
||||
Save,
|
||||
Update,
|
||||
Start,
|
||||
View,
|
||||
Movie,
|
||||
TvShow,
|
||||
Album,
|
||||
Request,
|
||||
Language,
|
||||
Finish
|
||||
}
|
||||
}
|
294
Ombi.Helpers/Analytics/Analytics.cs
Normal file
294
Ombi.Helpers/Analytics/Analytics.cs
Normal file
|
@ -0,0 +1,294 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Analytics.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
using NLog;
|
||||
using HttpUtility = Nancy.Helpers.HttpUtility;
|
||||
|
||||
namespace Ombi.Helpers.Analytics
|
||||
{
|
||||
public class Analytics : IAnalytics
|
||||
{
|
||||
private const string AnalyticsUri = "http://www.google-analytics.com/collect";
|
||||
private const string RequestMethod = "POST";
|
||||
private const string TrackingId = "UA-77083919-2";
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
|
||||
public void TrackEvent(Category category, Action action, string label, string username, string clientId, int? value = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var cat = category.ToString();
|
||||
var act = action.ToString();
|
||||
Track(HitType.@event, username, cat, act, label, clientId, value);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cat = category.ToString();
|
||||
var act = action.ToString();
|
||||
await TrackAsync(HitType.@event, username, cat, act, clientId, label, value);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void TrackPageview(Category category, Action action, string label, string username, string clientId, int? value = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cat = category.ToString();
|
||||
var act = action.ToString();
|
||||
Track(HitType.@pageview, username, cat, act, clientId, label, value);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
public async Task TrackPageviewAsync(Category category, Action action, string label, string username, string clientId, int? value = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var cat = category.ToString();
|
||||
var act = action.ToString();
|
||||
await TrackAsync(HitType.@pageview, username, cat, act, clientId, label, value);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void TrackException(string message, string username, string clientId, bool fatal)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var fatalInt = fatal ? 1 : 0;
|
||||
Track(HitType.exception, message, fatalInt, username, clientId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task TrackExceptionAsync(string message, string username, string clientId, bool fatal)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
var fatalInt = fatal ? 1 : 0;
|
||||
await TrackAsync(HitType.exception, message, fatalInt, username, clientId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void Track(HitType type, string username, string category, string action, string clientId, string label, int? value = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(category)) throw new ArgumentNullException(nameof(category));
|
||||
if (string.IsNullOrEmpty(action)) throw new ArgumentNullException(nameof(action));
|
||||
|
||||
var postData = BuildRequestData(type, username, category, action, clientId, label, value, null, null);
|
||||
|
||||
var postDataString = postData
|
||||
.Aggregate("", (data, next) => string.Format($"{data}&{next.Key}={HttpUtility.UrlEncode(next.Value)}"))
|
||||
.TrimEnd('&');
|
||||
|
||||
SendRequest(postDataString);
|
||||
}
|
||||
|
||||
private async Task TrackAsync(HitType type, string username, string category, string action, string clientId, string label, int? value = null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(category)) throw new ArgumentNullException(nameof(category));
|
||||
if (string.IsNullOrEmpty(action)) throw new ArgumentNullException(nameof(action));
|
||||
|
||||
var postData = BuildRequestData(type, username, category, action, clientId, label, value, null, null);
|
||||
|
||||
var postDataString = postData
|
||||
.Aggregate("", (data, next) => string.Format($"{data}&{next.Key}={HttpUtility.UrlEncode(next.Value)}"))
|
||||
.TrimEnd('&');
|
||||
|
||||
await SendRequestAsync(postDataString);
|
||||
}
|
||||
private async Task TrackAsync(HitType type, string message, int fatal, string username, string clientId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(message)) throw new ArgumentNullException(nameof(message));
|
||||
|
||||
var postData = BuildRequestData(type, username, null, null, null, clientId, null, message, fatal);
|
||||
|
||||
var postDataString = postData
|
||||
.Aggregate("", (data, next) => string.Format($"{data}&{next.Key}={HttpUtility.UrlEncode(next.Value)}"))
|
||||
.TrimEnd('&');
|
||||
|
||||
await SendRequestAsync(postDataString);
|
||||
}
|
||||
|
||||
private void Track(HitType type, string message, int fatal, string username, string clientId)
|
||||
{
|
||||
if (string.IsNullOrEmpty(message)) throw new ArgumentNullException(nameof(message));
|
||||
if (string.IsNullOrEmpty(username)) throw new ArgumentNullException(nameof(username));
|
||||
|
||||
var postData = BuildRequestData(type, username, null, null, null, clientId, null, message, fatal);
|
||||
|
||||
var postDataString = postData
|
||||
.Aggregate("", (data, next) => string.Format($"{data}&{next.Key}={HttpUtility.UrlEncode(next.Value)}"))
|
||||
.TrimEnd('&');
|
||||
|
||||
SendRequest(postDataString);
|
||||
}
|
||||
|
||||
private void SendRequest(string postDataString)
|
||||
{
|
||||
var request = (HttpWebRequest)WebRequest.Create(AnalyticsUri);
|
||||
request.Method = RequestMethod;
|
||||
// set the Content-Length header to the correct value
|
||||
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
|
||||
#if !DEBUG
|
||||
// write the request body to the request
|
||||
using (var writer = new StreamWriter(request.GetRequestStream()))
|
||||
{
|
||||
writer.Write(postDataString);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var webResponse = (HttpWebResponse)request.GetResponse();
|
||||
if (webResponse.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
throw new HttpException((int)webResponse.StatusCode, "Google Analytics tracking did not return OK 200");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Analytics tracking failed");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
private async Task SendRequestAsync(string postDataString)
|
||||
{
|
||||
var request = (HttpWebRequest)WebRequest.Create(AnalyticsUri);
|
||||
request.Method = RequestMethod;
|
||||
// set the Content-Length header to the correct value
|
||||
request.ContentLength = Encoding.UTF8.GetByteCount(postDataString);
|
||||
|
||||
#if !DEBUG
|
||||
// write the request body to the request
|
||||
using (var writer = new StreamWriter(await request.GetRequestStreamAsync()))
|
||||
{
|
||||
await writer.WriteAsync(postDataString);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var webResponse = (HttpWebResponse)await request.GetResponseAsync();
|
||||
if (webResponse.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
throw new HttpException((int)webResponse.StatusCode, "Google Analytics tracking did not return OK 200");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error(ex, "Analytics tracking failed");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
|
||||
|
||||
private Dictionary<string, string> BuildRequestData(HitType type, string username, string category, string action, string clientId, string label, int? value, string exceptionDescription, int? fatal)
|
||||
{
|
||||
var postData = new Dictionary<string, string>
|
||||
{
|
||||
{ "v", "1" },
|
||||
{ "tid", TrackingId },
|
||||
{ "t", type.ToString() }
|
||||
};
|
||||
|
||||
if (!string.IsNullOrEmpty(username))
|
||||
{
|
||||
postData.Add("uid", username);
|
||||
}
|
||||
|
||||
postData.Add("cid", !string.IsNullOrEmpty(clientId)
|
||||
? clientId
|
||||
: Guid.NewGuid().ToString());
|
||||
|
||||
if (!string.IsNullOrEmpty(label))
|
||||
{
|
||||
postData.Add("el", label);
|
||||
}
|
||||
if (value.HasValue)
|
||||
{
|
||||
postData.Add("ev", value.ToString());
|
||||
}
|
||||
if (!string.IsNullOrEmpty(category))
|
||||
{
|
||||
postData.Add("ec", category);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(action))
|
||||
{
|
||||
postData.Add("ea", action);
|
||||
}
|
||||
if (!string.IsNullOrEmpty(exceptionDescription))
|
||||
{
|
||||
postData.Add("exd", exceptionDescription);
|
||||
}
|
||||
if (fatal.HasValue)
|
||||
{
|
||||
postData.Add("exf", fatal.ToString());
|
||||
}
|
||||
return postData;
|
||||
}
|
||||
}
|
||||
}
|
44
Ombi.Helpers/Analytics/Category.cs
Normal file
44
Ombi.Helpers/Analytics/Category.cs
Normal file
|
@ -0,0 +1,44 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Category.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Helpers.Analytics
|
||||
{
|
||||
public enum Category
|
||||
{
|
||||
Wizard,
|
||||
Startup,
|
||||
Search,
|
||||
Requests,
|
||||
Admin,
|
||||
LandingPage,
|
||||
Api,
|
||||
Issues,
|
||||
UserLogin,
|
||||
Services,
|
||||
Navbar,
|
||||
UserManagement
|
||||
}
|
||||
}
|
36
Ombi.Helpers/Analytics/HitType.cs
Normal file
36
Ombi.Helpers/Analytics/HitType.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: HitType.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
// ReSharper disable InconsistentNaming
|
||||
namespace Ombi.Helpers.Analytics
|
||||
{
|
||||
internal enum HitType
|
||||
{
|
||||
@event,
|
||||
@pageview,
|
||||
@exception
|
||||
}
|
||||
}
|
99
Ombi.Helpers/Analytics/IAnalytics.cs
Normal file
99
Ombi.Helpers/Analytics/IAnalytics.cs
Normal file
|
@ -0,0 +1,99 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: IAnalytics.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ombi.Helpers.Analytics
|
||||
{
|
||||
public interface IAnalytics
|
||||
{
|
||||
/// <summary>
|
||||
/// Tracks the event.
|
||||
/// </summary>
|
||||
/// <param name="category">The category.</param>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="label">The label.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
void TrackEvent(Category category, Action action, string label, string username, string clientId, int? value = null);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks the event asynchronous.
|
||||
/// </summary>
|
||||
/// <param name="category">The category.</param>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="label">The label.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns></returns>
|
||||
void TrackEventAsync(Category category, Action action, string label, string username, string clientId, int? value = null);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks the page view.
|
||||
/// </summary>
|
||||
/// <param name="category">The category.</param>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="label">The label.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
void TrackPageview(Category category, Action action, string label, string username, string clientId, int? value = null);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks the page view asynchronous.
|
||||
/// </summary>
|
||||
/// <param name="category">The category.</param>
|
||||
/// <param name="action">The action.</param>
|
||||
/// <param name="label">The label.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns></returns>
|
||||
Task TrackPageviewAsync(Category category, Action action, string label, string username, string clientId, int? value = null);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks the exception.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="fatal">if set to <c>true</c> [fatal].</param>
|
||||
void TrackException(string message, string username, string clientId, bool fatal);
|
||||
|
||||
/// <summary>
|
||||
/// Tracks the exception asynchronous.
|
||||
/// </summary>
|
||||
/// <param name="message">The message.</param>
|
||||
/// <param name="username">The username.</param>
|
||||
/// <param name="clientId">The client identifier.</param>
|
||||
/// <param name="fatal">if set to <c>true</c> [fatal].</param>
|
||||
/// <returns></returns>
|
||||
Task TrackExceptionAsync(string message, string username, string clientId, bool fatal);
|
||||
}
|
||||
}
|
58
Ombi.Helpers/AssemblyHelper.cs
Normal file
58
Ombi.Helpers/AssemblyHelper.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: AssemblyHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class AssemblyHelper
|
||||
{
|
||||
public static string GetAssemblyVersion()
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
||||
return fvi.FileVersion;
|
||||
}
|
||||
|
||||
public static string GetProductVersion()
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
||||
var retVersion = fvi.ProductVersion;
|
||||
return retVersion;
|
||||
}
|
||||
|
||||
public static string GetFileVersion()
|
||||
{
|
||||
var assembly = Assembly.GetExecutingAssembly();
|
||||
var fvi = FileVersionInfo.GetVersionInfo(assembly.Location);
|
||||
var retVersion = fvi.FileVersion;
|
||||
return retVersion;
|
||||
}
|
||||
}
|
||||
}
|
54
Ombi.Helpers/ByteConverterHelper.cs
Normal file
54
Ombi.Helpers/ByteConverterHelper.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ByteConverterHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class ByteConverterHelper
|
||||
{
|
||||
public static byte[] ReturnBytes(object obj)
|
||||
{
|
||||
var json = JsonConvert.SerializeObject(obj);
|
||||
var bytes = Encoding.UTF8.GetBytes(json);
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static T ReturnObject<T>(byte[] bytes)
|
||||
{
|
||||
var json = Encoding.UTF8.GetString(bytes);
|
||||
var model = JsonConvert.DeserializeObject<T>(json);
|
||||
return model;
|
||||
}
|
||||
public static string ReturnFromBytes(byte[] bytes)
|
||||
{
|
||||
return Encoding.UTF8.GetString(bytes);
|
||||
}
|
||||
}
|
||||
}
|
58
Ombi.Helpers/CookieHelper.cs
Normal file
58
Ombi.Helpers/CookieHelper.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: CookieHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class CookieHelper
|
||||
{
|
||||
private const string GaCookie = "_ga";
|
||||
|
||||
/// <summary>
|
||||
/// Gets the analytic client identifier.
|
||||
/// <para>Example: Value = "GA1.1.306549087.1464005217"</para>
|
||||
/// </summary>
|
||||
/// <param name="cookies">The cookies.</param>
|
||||
/// <returns></returns>
|
||||
public static string GetAnalyticClientId(IDictionary<string, string> cookies)
|
||||
{
|
||||
string outString;
|
||||
|
||||
if (cookies.TryGetValue(GaCookie, out outString))
|
||||
{
|
||||
var split = outString.Split(new[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
return split.Length < 4
|
||||
? string.Empty
|
||||
: $"{split[2]}.{split[3]}";
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
50
Ombi.Helpers/DateTimeHelper.cs
Normal file
50
Ombi.Helpers/DateTimeHelper.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class DateTimeHelper
|
||||
{
|
||||
public static DateTimeOffset OffsetUTCDateTime(DateTime utcDateTime, int minuteOffset)
|
||||
{
|
||||
//TimeSpan ts = TimeSpan.FromMinutes(-minuteOffset);
|
||||
//return new DateTimeOffset(utcDateTime).ToOffset(ts);
|
||||
|
||||
// this is a workaround below to work with MONO
|
||||
var tzi = FindTimeZoneFromOffset(minuteOffset);
|
||||
var utcOffset = tzi.GetUtcOffset(utcDateTime);
|
||||
var newDate = utcDateTime + utcOffset;
|
||||
return new DateTimeOffset(newDate.Ticks, utcOffset);
|
||||
}
|
||||
|
||||
public static void CustomParse(string date, out DateTime dt)
|
||||
{
|
||||
// Try and parse it
|
||||
if (DateTime.TryParse(date, out dt))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Maybe it's only a year?
|
||||
if (DateTime.TryParseExact(date, "yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out dt))
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private static TimeZoneInfo FindTimeZoneFromOffset(int minuteOffset)
|
||||
{
|
||||
var tzc = TimeZoneInfo.GetSystemTimeZones();
|
||||
return tzc.FirstOrDefault(x => x.BaseUtcOffset.TotalMinutes == -minuteOffset);
|
||||
}
|
||||
|
||||
public static DateTime UnixTimeStampToDateTime(this int unixTimeStamp)
|
||||
{
|
||||
// Unix timestamp is seconds past epoch
|
||||
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
|
||||
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
|
||||
return dtDateTime;
|
||||
}
|
||||
}
|
||||
}
|
54
Ombi.Helpers/EnumExtensions.cs
Normal file
54
Ombi.Helpers/EnumExtensions.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: EnumExtensions.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class EnumExtensions
|
||||
{
|
||||
public static IEnumerable<Enum> GetUniqueFlags(this Enum flags)
|
||||
{
|
||||
ulong flag = 1;
|
||||
foreach (var value in Enum.GetValues(flags.GetType()).Cast<Enum>())
|
||||
{
|
||||
var bits = Convert.ToUInt64(value);
|
||||
while (flag < bits)
|
||||
{
|
||||
flag <<= 1;
|
||||
}
|
||||
|
||||
if (flag == bits && flags.HasFlag(value))
|
||||
{
|
||||
yield return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
117
Ombi.Helpers/EnumHelper.cs
Normal file
117
Ombi.Helpers/EnumHelper.cs
Normal file
|
@ -0,0 +1,117 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: EnumHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class EnumHelper<T>
|
||||
{
|
||||
public static IList<T> GetValues(Enum value)
|
||||
{
|
||||
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => (T) Enum.Parse(value.GetType(), fi.Name, false)).ToList();
|
||||
}
|
||||
|
||||
public static T Parse(string value)
|
||||
{
|
||||
return (T)Enum.Parse(typeof(T), value, true);
|
||||
}
|
||||
|
||||
public static IList<string> GetNames(Enum value)
|
||||
{
|
||||
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
|
||||
}
|
||||
|
||||
public static IList<string> GetDisplayValues(Enum value)
|
||||
{
|
||||
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
|
||||
}
|
||||
|
||||
private static string LookupResource(Type resourceManagerProvider, string resourceKey)
|
||||
{
|
||||
foreach (var staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
|
||||
{
|
||||
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
|
||||
{
|
||||
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
|
||||
return resourceManager.GetString(resourceKey);
|
||||
}
|
||||
}
|
||||
|
||||
return resourceKey; // Fallback with the key name
|
||||
}
|
||||
|
||||
public static string GetDisplayValue(T value)
|
||||
{
|
||||
var fieldInfo = value.GetType().GetField(value.ToString());
|
||||
|
||||
var descriptionAttributes = fieldInfo.GetCustomAttributes(
|
||||
typeof(DisplayAttribute), false) as DisplayAttribute[];
|
||||
|
||||
if (descriptionAttributes[0].ResourceType != null)
|
||||
return LookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
|
||||
|
||||
if (descriptionAttributes == null) return string.Empty;
|
||||
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
|
||||
}
|
||||
|
||||
public static T GetValueFromName(string name)
|
||||
{
|
||||
var type = typeof(T);
|
||||
if (!type.IsEnum) throw new InvalidOperationException();
|
||||
|
||||
foreach (var field in type.GetFields())
|
||||
{
|
||||
var attribute = Attribute.GetCustomAttribute(field,
|
||||
typeof(DisplayAttribute)) as DisplayAttribute;
|
||||
if (attribute != null)
|
||||
{
|
||||
if (attribute.Name == name)
|
||||
{
|
||||
return (T)field.GetValue(null);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (field.Name == name)
|
||||
return (T)field.GetValue(null);
|
||||
}
|
||||
}
|
||||
|
||||
throw new ArgumentOutOfRangeException(nameof(name));
|
||||
}
|
||||
|
||||
public static int All()
|
||||
{
|
||||
return Enum.GetValues(typeof(T)).Cast<int>().Sum();
|
||||
}
|
||||
}
|
||||
}
|
43
Ombi.Helpers/Exceptions/ApiRequestException.cs
Normal file
43
Ombi.Helpers/Exceptions/ApiRequestException.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApplicationSettingsException.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace Ombi.Helpers.Exceptions
|
||||
{
|
||||
public class ApiRequestException : Exception
|
||||
{
|
||||
public ApiRequestException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
public ApiRequestException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
49
Ombi.Helpers/Exceptions/ApplicationSettingsException.cs
Normal file
49
Ombi.Helpers/Exceptions/ApplicationSettingsException.cs
Normal file
|
@ -0,0 +1,49 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ApplicationSettingsException.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace Ombi.Helpers.Exceptions
|
||||
{
|
||||
public class ApplicationSettingsException : Exception
|
||||
{
|
||||
|
||||
public ApplicationSettingsException(string message) : base(message)
|
||||
{
|
||||
|
||||
}
|
||||
public ApplicationSettingsException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public ApplicationSettingsException()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
45
Ombi.Helpers/HtmlRemover.cs
Normal file
45
Ombi.Helpers/HtmlRemover.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: HtmlRemover.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class HtmlRemover
|
||||
{
|
||||
public static string RemoveHtml(this string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
var step1 = Regex.Replace(value, @"<[^>]+>| ", "").Trim();
|
||||
var step2 = Regex.Replace(step1, @"\s{2,}", " ");
|
||||
return step2;
|
||||
}
|
||||
}
|
||||
}
|
69
Ombi.Helpers/ICacheProvider.cs
Normal file
69
Ombi.Helpers/ICacheProvider.cs
Normal file
|
@ -0,0 +1,69 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ICacheProvider.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public interface ICacheProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the item from the cache, if the item is not present
|
||||
/// then we will get that item and store it in the cache.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to store in the cache</typeparam>
|
||||
/// <param name="key">The key</param>
|
||||
/// <param name="itemCallback">The item callback. This will be called if the item is not present in the cache. </param>
|
||||
/// <param name="cacheTime">The amount of time we want to cache the object</param>
|
||||
/// <returns><see cref="T"/></returns>
|
||||
T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class;
|
||||
Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified item from the cache.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to get from the cache</typeparam>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <returns><see cref="T"/></returns>
|
||||
T Get<T>(string key) where T : class;
|
||||
|
||||
/// <summary>
|
||||
/// Set/Store the specified object in the cache
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="data">The object we want to store.</param>
|
||||
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
||||
void Set(string key, object data, int cacheTime = 20);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the specified object from the cache.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
void Remove(string key);
|
||||
}
|
||||
}
|
54
Ombi.Helpers/JsonConvertHelper.cs
Normal file
54
Ombi.Helpers/JsonConvertHelper.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: JsonConvertHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class JsonConvertHelper
|
||||
{
|
||||
public static T[] ParseObjectToArray<T>(object ambiguousObject)
|
||||
{
|
||||
var json = ambiguousObject.ToString();
|
||||
if (string.IsNullOrWhiteSpace(json))
|
||||
{
|
||||
return new T[0]; // Could return null here instead.
|
||||
}
|
||||
if (json.TrimStart().StartsWith("["))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T[]>(json);
|
||||
}
|
||||
if (json.TrimStart().Equals("{}"))
|
||||
{
|
||||
return new T[0];
|
||||
}
|
||||
|
||||
return new T[1] { JsonConvert.DeserializeObject<T>(json) };
|
||||
|
||||
}
|
||||
}
|
||||
}
|
178
Ombi.Helpers/LoggingHelper.cs
Normal file
178
Ombi.Helpers/LoggingHelper.cs
Normal file
|
@ -0,0 +1,178 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: LoggingHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using NLog.Config;
|
||||
using NLog.Targets;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class LoggingHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// WARNING, This method uses up a LOT of memory and can lead to leaks.
|
||||
/// </summary>
|
||||
/// <param name="value">The value.</param>
|
||||
/// <returns></returns>
|
||||
public static string DumpJson(this object value)
|
||||
{
|
||||
object dumpTarget;
|
||||
//if this is a string that contains a JSON object, do a round-trip serialization to format it:
|
||||
var stringValue = value as string;
|
||||
if (stringValue != null)
|
||||
{
|
||||
dumpTarget = stringValue.Trim().StartsWith("{", StringComparison.Ordinal)
|
||||
? JsonConvert.SerializeObject(JsonConvert.DeserializeObject(stringValue), Formatting.Indented)
|
||||
: stringValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
dumpTarget = JsonConvert.SerializeObject(value, Formatting.Indented);
|
||||
}
|
||||
return dumpTarget.ToString();
|
||||
}
|
||||
|
||||
public static void ConfigureLogging(string connectionString)
|
||||
{
|
||||
LogManager.ThrowExceptions = true;
|
||||
// Step 1. Create configuration object
|
||||
var config = new LoggingConfiguration();
|
||||
|
||||
// Step 2. Create targets and add them to the configuration
|
||||
var databaseTarget = new DatabaseTarget
|
||||
{
|
||||
CommandType = CommandType.Text,
|
||||
ConnectionString = connectionString,
|
||||
DBProvider = "Mono.Data.Sqlite.SqliteConnection, Mono.Data.Sqlite, Version=4.0.0.0, Culture=neutral, PublicKeyToken=0738eb9f132ed756",
|
||||
Name = "database",
|
||||
|
||||
};
|
||||
|
||||
var messageParam = new DatabaseParameterInfo { Name = "@Message", Layout = "${message}" };
|
||||
var callsiteParam = new DatabaseParameterInfo { Name = "@Callsite", Layout = "${callsite}" };
|
||||
var levelParam = new DatabaseParameterInfo { Name = "@Level", Layout = "${level}" };
|
||||
var dateParam = new DatabaseParameterInfo { Name = "@Date", Layout = "${date}" };
|
||||
var loggerParam = new DatabaseParameterInfo { Name = "@Logger", Layout = "${logger}" };
|
||||
var exceptionParam = new DatabaseParameterInfo { Name = "@Exception", Layout = "${exception:tostring}" };
|
||||
|
||||
databaseTarget.Parameters.Add(messageParam);
|
||||
databaseTarget.Parameters.Add(callsiteParam);
|
||||
databaseTarget.Parameters.Add(levelParam);
|
||||
databaseTarget.Parameters.Add(dateParam);
|
||||
databaseTarget.Parameters.Add(loggerParam);
|
||||
databaseTarget.Parameters.Add(exceptionParam);
|
||||
|
||||
databaseTarget.CommandText = "INSERT INTO Logs (Date,Level,Logger, Message, Callsite, Exception) VALUES(@Date,@Level,@Logger, @Message, @Callsite, @Exception);";
|
||||
config.AddTarget("database", databaseTarget);
|
||||
|
||||
// Step 4. Define rules
|
||||
var rule1 = new LoggingRule("*", LogLevel.Debug, databaseTarget);
|
||||
config.LoggingRules.Add(rule1);
|
||||
var currentPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath) ?? string.Empty, "logs/${shortdate}.log");
|
||||
|
||||
var fileTarget = new FileTarget
|
||||
{
|
||||
Name = "file",
|
||||
FileName = currentPath,
|
||||
Layout = "${date} ${logger} ${level}: ${message} ${exception:tostring}",
|
||||
CreateDirs = true
|
||||
};
|
||||
config.AddTarget(fileTarget);
|
||||
var rule2 = new LoggingRule("*", LogLevel.Debug, fileTarget);
|
||||
config.LoggingRules.Add(rule2);
|
||||
|
||||
// Step 5. Activate the configuration
|
||||
LogManager.Configuration = config;
|
||||
}
|
||||
|
||||
public static void ReconfigureLogLevel(LogLevel level)
|
||||
{
|
||||
|
||||
foreach (var rule in LogManager.Configuration.LoggingRules)
|
||||
{
|
||||
// Remove all levels
|
||||
rule.DisableLoggingForLevel(LogLevel.Trace);
|
||||
rule.DisableLoggingForLevel(LogLevel.Info);
|
||||
rule.DisableLoggingForLevel(LogLevel.Debug);
|
||||
rule.DisableLoggingForLevel(LogLevel.Warn);
|
||||
rule.DisableLoggingForLevel(LogLevel.Error);
|
||||
rule.DisableLoggingForLevel(LogLevel.Fatal);
|
||||
|
||||
|
||||
if (level == LogLevel.Trace)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Trace);
|
||||
rule.EnableLoggingForLevel(LogLevel.Info);
|
||||
rule.EnableLoggingForLevel(LogLevel.Debug);
|
||||
rule.EnableLoggingForLevel(LogLevel.Warn);
|
||||
rule.EnableLoggingForLevel(LogLevel.Error);
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
if (level == LogLevel.Info)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Info);
|
||||
rule.EnableLoggingForLevel(LogLevel.Warn);
|
||||
rule.EnableLoggingForLevel(LogLevel.Error);
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
if (level == LogLevel.Debug)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Debug);
|
||||
rule.EnableLoggingForLevel(LogLevel.Warn);
|
||||
rule.EnableLoggingForLevel(LogLevel.Error);
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
if (level == LogLevel.Warn)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Warn);
|
||||
rule.EnableLoggingForLevel(LogLevel.Error);
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
if (level == LogLevel.Error)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Error);
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
if (level == LogLevel.Fatal)
|
||||
{
|
||||
rule.EnableLoggingForLevel(LogLevel.Fatal);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//Call to update existing Loggers created with GetLogger() or
|
||||
//GetCurrentClassLogger()
|
||||
LogManager.ReconfigExistingLoggers();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
134
Ombi.Helpers/MemoryCacheProvider.cs
Normal file
134
Ombi.Helpers/MemoryCacheProvider.cs
Normal file
|
@ -0,0 +1,134 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: MemoryCacheProvider.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Runtime.Caching;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class MemoryCacheProvider : ICacheProvider
|
||||
{
|
||||
private ObjectCache Cache => MemoryCache.Default;
|
||||
private readonly object _lock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the item from the cache, if the item is not present
|
||||
/// then we will get that item and store it in the cache.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to store in the cache.</typeparam>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="itemCallback">The item callback. This will be called if the item is not present in the cache.
|
||||
/// </param>
|
||||
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
||||
/// <returns>A copy of the cached object.</returns>
|
||||
/// <remarks>If the <c><![CDATA[Func<T>]]> itemCallback</c> is null and the item is not in the cache it will throw a <see cref="NullReferenceException"/>.
|
||||
/// <para>If you do not want to change the object in the cache (since it's a copy returned and not a reference) you will need to <see cref="Remove"/>
|
||||
/// the cached item and then <see cref="Set"/> it, or just call this method.</para></remarks>
|
||||
public T GetOrSet<T>(string key, Func<T> itemCallback, int cacheTime = 20) where T : class
|
||||
{
|
||||
var item = Get<T>(key);
|
||||
if (item == null)
|
||||
{
|
||||
item = itemCallback();
|
||||
if (item != null)
|
||||
{
|
||||
Set(key, item, cacheTime);
|
||||
}
|
||||
}
|
||||
|
||||
// Return a copy, not the stored cache reference
|
||||
// The cached object will not change
|
||||
// If we
|
||||
return item.CloneJson();
|
||||
}
|
||||
|
||||
public async Task<T> GetOrSetAsync<T>(string key, Func<Task<T>> itemCallback, int cacheTime = 20) where T : class
|
||||
{
|
||||
var item = Get<T>(key);
|
||||
if (item == null)
|
||||
{
|
||||
item = await itemCallback();
|
||||
if (item != null)
|
||||
{
|
||||
Set(key, item, cacheTime);
|
||||
}
|
||||
}
|
||||
|
||||
// Return a copy, not the stored cache reference
|
||||
// The cached object will not change
|
||||
return item.CloneJson();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the specified item from the cache.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type to get from the cache</typeparam>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <returns></returns>
|
||||
public T Get<T>(string key) where T : class
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
return Cache.Get(key) as T;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set/Store the specified object in the cache
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="data">The object we want to store.</param>
|
||||
/// <param name="cacheTime">The amount of time we want to cache the object.</param>
|
||||
public void Set(string key, object data, int cacheTime = 20)
|
||||
{
|
||||
var policy = new CacheItemPolicy { AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime) };
|
||||
lock (_lock)
|
||||
{
|
||||
Cache.Remove(key);
|
||||
Cache.Add(new CacheItem(key, data), policy);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the specified object from the cache.
|
||||
/// </summary>
|
||||
/// <param name="key">The key.</param>
|
||||
public void Remove(string key)
|
||||
{
|
||||
var keys = Cache.Where(x => x.Key.Contains(key));
|
||||
foreach (var k in keys)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Cache.Remove(k.Key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
58
Ombi.Helpers/ObjectCopier.cs
Normal file
58
Ombi.Helpers/ObjectCopier.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: ObjectCopier.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a method for performing a deep copy of an object.
|
||||
/// Binary Serialization is used to perform the copy.
|
||||
/// </summary>
|
||||
public static class ObjectCopier
|
||||
{
|
||||
/// <summary>
|
||||
/// <para>Initialize inner objects individually</para>
|
||||
/// For example in default constructor some list property initialized with some values,
|
||||
/// but in 'source' these items are cleaned -
|
||||
/// without <c>ObjectCreationHandling.Replace</c> default constructor values will be added to result
|
||||
/// </summary>
|
||||
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace };
|
||||
|
||||
/// <summary>
|
||||
/// Perform a deep Copy of the object, using Json as a serialisation method.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of object being copied.</typeparam>
|
||||
/// <param name="source">The object instance to copy.</param>
|
||||
/// <returns>The copied object.</returns>
|
||||
public static T CloneJson<T>(this T source)
|
||||
{
|
||||
// Don't serialize a null object, simply return the default for that object
|
||||
return ReferenceEquals(source, null) ? default(T) : JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(source), Settings);
|
||||
}
|
||||
}
|
||||
}
|
116
Ombi.Helpers/Ombi.Helpers.csproj
Normal file
116
Ombi.Helpers/Ombi.Helpers.csproj
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{1252336D-42A3-482A-804C-836E60173DFA}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Ombi.Helpers</RootNamespace>
|
||||
<AssemblyName>Ombi.Helpers</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<TargetFrameworkProfile />
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Nancy, Version=1.4.2.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Nancy.1.4.3\lib\net40\Nancy.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Ninject, Version=3.2.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Ninject.3.2.0.0\lib\net45-full\Ninject.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NLog.4.3.6\lib\net45\NLog.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Runtime.Caching" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Analytics\Action.cs" />
|
||||
<Compile Include="Analytics\Analytics.cs" />
|
||||
<Compile Include="Analytics\Category.cs" />
|
||||
<Compile Include="Analytics\HitType.cs" />
|
||||
<Compile Include="Analytics\IAnalytics.cs" />
|
||||
<Compile Include="AssemblyHelper.cs" />
|
||||
<Compile Include="ByteConverterHelper.cs" />
|
||||
<Compile Include="CookieHelper.cs" />
|
||||
<Compile Include="DateTimeHelper.cs" />
|
||||
<Compile Include="EnumHelper.cs" />
|
||||
<Compile Include="Exceptions\ApiRequestException.cs" />
|
||||
<Compile Include="Exceptions\ApplicationSettingsException.cs" />
|
||||
<Compile Include="HtmlRemover.cs" />
|
||||
<Compile Include="ICacheProvider.cs" />
|
||||
<Compile Include="JsonConvertHelper.cs" />
|
||||
<Compile Include="LoggingHelper.cs" />
|
||||
<Compile Include="MemoryCacheProvider.cs" />
|
||||
<Compile Include="ObjectCopier.cs" />
|
||||
<Compile Include="OperatingSystemHelper.cs" />
|
||||
<Compile Include="PasswordHasher.cs" />
|
||||
<Compile Include="EnumExtensions.cs" />
|
||||
<Compile Include="Permissions\Features.cs" />
|
||||
<Compile Include="Permissions\Permissions.cs" />
|
||||
<Compile Include="PlexHelper.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SerializerSettings.cs" />
|
||||
<Compile Include="SessionKeys.cs" />
|
||||
<Compile Include="StringCipher.cs" />
|
||||
<Compile Include="StringHasher.cs" />
|
||||
<Compile Include="StringHelper.cs" />
|
||||
<Compile Include="TypeHelper.cs" />
|
||||
<Compile Include="UriHelper.cs" />
|
||||
<Compile Include="UserClaims.cs" />
|
||||
<Compile Include="UserType.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
76
Ombi.Helpers/OperatingSystemHelper.cs
Normal file
76
Ombi.Helpers/OperatingSystemHelper.cs
Normal file
|
@ -0,0 +1,76 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: OperatingSystemHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class OperatingSystemHelper
|
||||
{
|
||||
public static string GetOs()
|
||||
{
|
||||
var os = System.Environment.OSVersion;
|
||||
var osVersion = os.Version;
|
||||
if (osVersion.Major.Equals(10))
|
||||
{
|
||||
return "Windows 10/Windows Server 2016";
|
||||
}
|
||||
if (osVersion.Major.Equals(6))
|
||||
{
|
||||
if (osVersion.Minor.Equals(3))
|
||||
{
|
||||
return "Windows 8.1/Windows Server 2012 R2";
|
||||
}
|
||||
if (osVersion.Minor.Equals(2))
|
||||
{
|
||||
return "Windows 8/Windows Server 2012";
|
||||
}
|
||||
if (osVersion.Minor.Equals(1))
|
||||
{
|
||||
return "Windows 7/Windows Server 2008 R2";
|
||||
}
|
||||
if (osVersion.Minor.Equals(0))
|
||||
{
|
||||
return "Windows Vista/Windows Server 2008";
|
||||
}
|
||||
}
|
||||
if (osVersion.Major.Equals(5))
|
||||
{
|
||||
if (osVersion.Minor.Equals(2))
|
||||
{
|
||||
return "Windows XP 64-Bit Edition/Windows Server 2003";
|
||||
}
|
||||
if (osVersion.Minor.Equals(1))
|
||||
{
|
||||
return "Windows XP";
|
||||
}
|
||||
if (osVersion.Minor.Equals(0))
|
||||
{
|
||||
return "Windows 2000";
|
||||
}
|
||||
}
|
||||
return os.VersionString;
|
||||
}
|
||||
}
|
||||
}
|
71
Ombi.Helpers/PasswordHasher.cs
Normal file
71
Ombi.Helpers/PasswordHasher.cs
Normal file
|
@ -0,0 +1,71 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PasswordHasher.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
|
||||
public static class PasswordHasher
|
||||
{
|
||||
// 24 = 192 bits
|
||||
private const int SaltByteSize = 24;
|
||||
private const int HashByteSize = 24;
|
||||
private const int HasingIterationsCount = 10101;
|
||||
|
||||
public static byte[] ComputeHash(string password, byte[] salt, int iterations = HasingIterationsCount, int hashByteSize = HashByteSize)
|
||||
{
|
||||
var hashGenerator = new Rfc2898DeriveBytes(password, salt) { IterationCount = iterations };
|
||||
return hashGenerator.GetBytes(hashByteSize);
|
||||
}
|
||||
|
||||
public static byte[] GenerateSalt(int saltByteSize = SaltByteSize)
|
||||
{
|
||||
var saltGenerator = new RNGCryptoServiceProvider();
|
||||
var salt = new byte[saltByteSize];
|
||||
saltGenerator.GetBytes(salt);
|
||||
return salt;
|
||||
}
|
||||
|
||||
public static bool VerifyPassword(string password, byte[] passwordSalt, byte[] passwordHash)
|
||||
{
|
||||
var computedHash = ComputeHash(password, passwordSalt);
|
||||
return AreHashesEqual(computedHash, passwordHash);
|
||||
}
|
||||
|
||||
//Length constant verification - prevents timing attack
|
||||
private static bool AreHashesEqual(IReadOnlyList<byte> firstHash, IReadOnlyList<byte> secondHash)
|
||||
{
|
||||
var minHashLength = firstHash.Count <= secondHash.Count ? firstHash.Count : secondHash.Count;
|
||||
var xor = firstHash.Count ^ secondHash.Count;
|
||||
for (var i = 0; i < minHashLength; i++)
|
||||
xor |= firstHash[i] ^ secondHash[i];
|
||||
return 0 == xor;
|
||||
}
|
||||
}
|
||||
}
|
42
Ombi.Helpers/Permissions/Features.cs
Normal file
42
Ombi.Helpers/Permissions/Features.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Features.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Ombi.Helpers.Permissions
|
||||
{
|
||||
[Flags]
|
||||
public enum Features
|
||||
{
|
||||
[Display(Name = "Newsletter")]
|
||||
Newsletter = 1,
|
||||
[Display(Name = "Request Added Notification")]
|
||||
RequestAddedNotification = 2,
|
||||
|
||||
}
|
||||
}
|
78
Ombi.Helpers/Permissions/Permissions.cs
Normal file
78
Ombi.Helpers/Permissions/Permissions.cs
Normal file
|
@ -0,0 +1,78 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Permissions.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Ombi.Helpers.Permissions
|
||||
{
|
||||
[Flags]
|
||||
////
|
||||
//// NOTE if any are added, make sure we change the UserManagementHelper
|
||||
////
|
||||
public enum Permissions
|
||||
{
|
||||
[Display(Name = "Access Administration Settings")]
|
||||
Administrator = 1,
|
||||
|
||||
[Display(Name = "Request Movie")]
|
||||
RequestMovie = 2,
|
||||
|
||||
[Display(Name = "Request TV Show")]
|
||||
RequestTvShow = 4,
|
||||
|
||||
[Display(Name = "Request Music")]
|
||||
RequestMusic = 8,
|
||||
|
||||
[Display(Name = "Report Issue")]
|
||||
ReportIssue = 16,
|
||||
|
||||
[Display(Name = "Read Only User")]
|
||||
ReadOnlyUser = 32,
|
||||
|
||||
[Display(Name = "Auto Approve Movie Requests")]
|
||||
AutoApproveMovie = 64,
|
||||
|
||||
[Display(Name = "Auto Approve TV Show Requests")]
|
||||
AutoApproveTv = 128,
|
||||
|
||||
[Display(Name = "Auto Approve Album Requests")]
|
||||
AutoApproveAlbum = 256,
|
||||
|
||||
[Display(Name = "Manage Requests")]
|
||||
ManageRequests = 512,
|
||||
|
||||
[Display(Name = "Users can only view their own requests")]
|
||||
UsersCanViewOnlyOwnRequests = 1024,
|
||||
|
||||
[Display(Name = "Users can only view their own issues")]
|
||||
UsersCanViewOnlyOwnIssues = 2048,
|
||||
|
||||
[Display(Name = "Bypass the request limit")]
|
||||
BypassRequestLimit = 4096
|
||||
}
|
||||
}
|
119
Ombi.Helpers/PlexHelper.cs
Normal file
119
Ombi.Helpers/PlexHelper.cs
Normal file
|
@ -0,0 +1,119 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: PlexHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using NLog;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class PlexHelper
|
||||
{
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public static string GetProviderIdFromPlexGuid(string guid)
|
||||
{
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
return guid;
|
||||
|
||||
var guidSplit = guid.Split(new[] { '/', '?' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (guidSplit.Length > 1)
|
||||
{
|
||||
return guidSplit[1];
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static EpisodeModelHelper GetSeasonsAndEpisodesFromPlexGuid(string guid)
|
||||
{
|
||||
var ep = new EpisodeModelHelper();
|
||||
//guid="com.plexapp.agents.thetvdb://269586/2/8?lang=en"
|
||||
if (string.IsNullOrEmpty(guid))
|
||||
return null;
|
||||
try
|
||||
{
|
||||
var guidSplit = guid.Split(new[] { '/', '?' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (guidSplit.Length > 2)
|
||||
{
|
||||
ep.ProviderId = guidSplit[1];
|
||||
ep.SeasonNumber = int.Parse(guidSplit[2]);
|
||||
ep.EpisodeNumber = int.Parse(guidSplit[3]);
|
||||
}
|
||||
return ep;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Error(e);
|
||||
Log.Error(guid);
|
||||
return ep;
|
||||
}
|
||||
}
|
||||
|
||||
public static int GetSeasonNumberFromTitle(string title)
|
||||
{
|
||||
if (string.IsNullOrEmpty(title))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var split = title.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
if (split.Length < 2)
|
||||
{
|
||||
// Cannot get the season number, it's not in the usual format
|
||||
return 0;
|
||||
}
|
||||
|
||||
int season;
|
||||
if (int.TryParse(split[1], out season))
|
||||
{
|
||||
return season;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static string GetPlexMediaUrl(string machineId, string mediaId)
|
||||
{
|
||||
var url =
|
||||
$"https://app.plex.tv/web/app#!/server/{machineId}/details/%2Flibrary%2Fmetadata%2F{mediaId}";
|
||||
return url;
|
||||
}
|
||||
|
||||
public static string FormatGenres(string tags)
|
||||
{
|
||||
var split = tags.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
|
||||
return string.Join(", ", split);
|
||||
}
|
||||
}
|
||||
|
||||
public class EpisodeModelHelper
|
||||
{
|
||||
public string ProviderId { get; set; }
|
||||
public int SeasonNumber { get; set; }
|
||||
public int EpisodeNumber { get; set; }
|
||||
}
|
||||
}
|
37
Ombi.Helpers/Properties/AssemblyInfo.cs
Normal file
37
Ombi.Helpers/Properties/AssemblyInfo.cs
Normal file
|
@ -0,0 +1,37 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Ombi.Helpers")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("Ombi.Helpers")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2016")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("1252336d-42a3-482a-804c-836e60173dfa")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
[assembly: AssemblyInformationalVersionAttribute("1.0.0.0")]
|
45
Ombi.Helpers/SerializerSettings.cs
Normal file
45
Ombi.Helpers/SerializerSettings.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SerializerSettings.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Runtime.Serialization.Formatters;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class SerializerSettings
|
||||
{
|
||||
|
||||
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
||||
{
|
||||
Formatting = Formatting.None,
|
||||
TypeNameHandling = TypeNameHandling.Objects,
|
||||
TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple,
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
|
||||
}
|
||||
}
|
36
Ombi.Helpers/SessionKeys.cs
Normal file
36
Ombi.Helpers/SessionKeys.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: SessionKeys.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class SessionKeys
|
||||
{
|
||||
public const string UsernameKey = "Username";
|
||||
public const string ClientDateTimeOffsetKey = "ClientDateTimeOffset";
|
||||
public const string UserWizardPlexAuth = nameof(UserWizardPlexAuth);
|
||||
public const string UserWizardMachineId = nameof(UserWizardMachineId);
|
||||
}
|
||||
}
|
146
Ombi.Helpers/StringCipher.cs
Normal file
146
Ombi.Helpers/StringCipher.cs
Normal file
|
@ -0,0 +1,146 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: StringCipher.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class StringCipher
|
||||
{
|
||||
// This constant determines the number of iterations for the password bytes generation function.
|
||||
private const int DerivationIterations = 1000;
|
||||
// This constant is used to determine the keysize of the encryption algorithm in bits.
|
||||
// We divide this by 8 within the code below to get the equivalent number of bytes.
|
||||
private const int Keysize = 256;
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts the specified cipher text.
|
||||
/// </summary>
|
||||
/// <param name="cipherText">The cipher text.</param>
|
||||
/// <param name="passPhrase">The pass phrase.</param>
|
||||
/// <returns></returns>
|
||||
public static string Decrypt(string cipherText, string passPhrase)
|
||||
{
|
||||
// Get the complete stream of bytes that represent:
|
||||
// [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
|
||||
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
|
||||
// Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
|
||||
var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
|
||||
// Get the IV bytes by extracting the next 32 bytes from the supplied cipherText bytes.
|
||||
var ivStringBytes = cipherTextBytesWithSaltAndIv.Skip(Keysize / 8).Take(Keysize / 8).ToArray();
|
||||
// Get the actual cipher text bytes by removing the first 64 bytes from the cipherText string.
|
||||
var cipherTextBytes = cipherTextBytesWithSaltAndIv.Skip((Keysize / 8) * 2).Take(cipherTextBytesWithSaltAndIv.Length - ((Keysize / 8) * 2)).ToArray();
|
||||
|
||||
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
|
||||
{
|
||||
var keyBytes = password.GetBytes(Keysize / 8);
|
||||
using (var symmetricKey = new RijndaelManaged())
|
||||
{
|
||||
symmetricKey.BlockSize = 256;
|
||||
symmetricKey.Mode = CipherMode.CBC;
|
||||
symmetricKey.Padding = PaddingMode.PKCS7;
|
||||
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, ivStringBytes))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream(cipherTextBytes))
|
||||
{
|
||||
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
|
||||
{
|
||||
var plainTextBytes = new byte[cipherTextBytes.Length];
|
||||
var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
|
||||
memoryStream.Close();
|
||||
cryptoStream.Close();
|
||||
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the specified plain text.
|
||||
/// </summary>
|
||||
/// <param name="plainText">The plain text.</param>
|
||||
/// <param name="passPhrase">The pass phrase.</param>
|
||||
/// <returns></returns>
|
||||
public static string Encrypt(string plainText, string passPhrase)
|
||||
{
|
||||
// Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
|
||||
// so that the same Salt and IV values can be used when decrypting.
|
||||
var saltStringBytes = Generate256BitsOfRandomEntropy();
|
||||
var ivStringBytes = Generate256BitsOfRandomEntropy();
|
||||
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
||||
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
|
||||
{
|
||||
var keyBytes = password.GetBytes(Keysize / 8);
|
||||
using (var symmetricKey = new RijndaelManaged())
|
||||
{
|
||||
symmetricKey.BlockSize = 256;
|
||||
symmetricKey.Mode = CipherMode.CBC;
|
||||
symmetricKey.Padding = PaddingMode.PKCS7;
|
||||
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
|
||||
{
|
||||
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
|
||||
cryptoStream.FlushFinalBlock();
|
||||
// Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
|
||||
var cipherTextBytes = saltStringBytes;
|
||||
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
|
||||
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
|
||||
memoryStream.Close();
|
||||
cryptoStream.Close();
|
||||
return Convert.ToBase64String(cipherTextBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generate256s the bits of random entropy.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private static byte[] Generate256BitsOfRandomEntropy()
|
||||
{
|
||||
var randomBytes = new byte[32]; // 32 Bytes will give us 256 bits.
|
||||
using (var rngCsp = new RNGCryptoServiceProvider())
|
||||
{
|
||||
// Fill the array with cryptographically secure random bytes.
|
||||
rngCsp.GetBytes(randomBytes);
|
||||
}
|
||||
return randomBytes;
|
||||
}
|
||||
}
|
||||
}
|
53
Ombi.Helpers/StringHasher.cs
Normal file
53
Ombi.Helpers/StringHasher.cs
Normal file
|
@ -0,0 +1,53 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: StringHasher.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public class StringHasher
|
||||
{
|
||||
public static string CalcuateMd5Hash(string input)
|
||||
{
|
||||
using (var md5 = MD5.Create())
|
||||
{
|
||||
var inputBytes = Encoding.UTF8.GetBytes(input);
|
||||
var hash = md5.ComputeHash(inputBytes);
|
||||
|
||||
var sb = new StringBuilder();
|
||||
|
||||
foreach (var t in hash)
|
||||
{
|
||||
sb.Append(t.ToString("x2"));
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
101
Ombi.Helpers/StringHelper.cs
Normal file
101
Ombi.Helpers/StringHelper.cs
Normal file
|
@ -0,0 +1,101 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: StringHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class StringHelper
|
||||
{
|
||||
public static string FirstCharToUpper(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return input;
|
||||
|
||||
var firstUpper = char.ToUpper(input[0]);
|
||||
return firstUpper + string.Join("", input.Skip(1));
|
||||
}
|
||||
|
||||
public static string ToCamelCaseWords(this string input)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
return input;
|
||||
return Regex.Replace(input.FirstCharToUpper(), "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 ");
|
||||
}
|
||||
|
||||
public static string AddPrefix(this string[] values, string prefix, string separator)
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
var len = values.Length;
|
||||
for (var i = 0; i < len; i++)
|
||||
{
|
||||
sb.Append(prefix).Append(values[i]);
|
||||
|
||||
// If it's not the last item in the collection, then add a separator
|
||||
if (i < len - 1)
|
||||
{
|
||||
sb.Append(separator);
|
||||
}
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
public static IEnumerable<string> SplitEmailsByDelimiter(this string input, char delimiter)
|
||||
{
|
||||
if (string.IsNullOrEmpty(input))
|
||||
{
|
||||
yield return string.Empty;
|
||||
}
|
||||
var startIndex = 0;
|
||||
var delimiterIndex = 0;
|
||||
|
||||
while (delimiterIndex >= 0)
|
||||
{
|
||||
delimiterIndex = input.IndexOf(delimiter, startIndex);
|
||||
string substring = input;
|
||||
if (delimiterIndex > 0)
|
||||
{
|
||||
substring = input.Substring(0, delimiterIndex).Trim();
|
||||
}
|
||||
|
||||
if (!substring.Contains("\"") || substring.IndexOf("\"") != substring.LastIndexOf("\""))
|
||||
{
|
||||
yield return substring;
|
||||
input = input.Substring(delimiterIndex + 1).Trim();
|
||||
startIndex = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
startIndex = delimiterIndex + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
56
Ombi.Helpers/TypeHelper.cs
Normal file
56
Ombi.Helpers/TypeHelper.cs
Normal file
|
@ -0,0 +1,56 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: TypeHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class TypeHelper
|
||||
{
|
||||
public static string[] GetPropertyNames(this Type t)
|
||||
{
|
||||
return t.GetProperties().Select(x => x.Name).ToArray();
|
||||
}
|
||||
|
||||
public static IEnumerable<FieldInfo> GetConstants(this Type type)
|
||||
{
|
||||
var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
|
||||
|
||||
return fieldInfos.Where(fi => fi.IsLiteral && !fi.IsInitOnly);
|
||||
}
|
||||
|
||||
public static IEnumerable<T> GetConstantsValues<T>(this Type type) where T : class
|
||||
{
|
||||
var fieldInfos = GetConstants(type);
|
||||
|
||||
return fieldInfos.Select(fi => fi.GetRawConstantValue() as T);
|
||||
}
|
||||
}
|
||||
}
|
115
Ombi.Helpers/UriHelper.cs
Normal file
115
Ombi.Helpers/UriHelper.cs
Normal file
|
@ -0,0 +1,115 @@
|
|||
using System;
|
||||
using Ombi.Helpers.Exceptions;
|
||||
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
public static class UriHelper
|
||||
{
|
||||
|
||||
public static Uri ReturnUri(this string val)
|
||||
{
|
||||
if (val == null)
|
||||
{
|
||||
throw new ApplicationSettingsException("The URI is null, please check your settings to make sure you have configured the applications correctly.");
|
||||
}
|
||||
try
|
||||
{
|
||||
var uri = new UriBuilder();
|
||||
|
||||
if (val.StartsWith("http://", StringComparison.Ordinal))
|
||||
{
|
||||
uri = new UriBuilder(val);
|
||||
}
|
||||
else if (val.StartsWith("https://", StringComparison.Ordinal))
|
||||
{
|
||||
uri = new UriBuilder(val);
|
||||
}
|
||||
else if (val.Contains(":"))
|
||||
{
|
||||
var split = val.Split(':', '/');
|
||||
int port;
|
||||
int.TryParse(split[1], out port);
|
||||
|
||||
uri = split.Length == 3
|
||||
? new UriBuilder(Uri.UriSchemeHttp, split[0], port, "/" + split[2])
|
||||
: new UriBuilder(Uri.UriSchemeHttp, split[0], port);
|
||||
}
|
||||
else
|
||||
{
|
||||
uri = new UriBuilder(Uri.UriSchemeHttp, val);
|
||||
}
|
||||
|
||||
return uri.Uri;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception(exception.Message, exception);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the URI.
|
||||
/// </summary>
|
||||
/// <param name="val">The value.</param>
|
||||
/// <param name="port">The port.</param>
|
||||
/// <param name="ssl">if set to <c>true</c> [SSL].</param>
|
||||
/// <param name="subdir">The subdir.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ApplicationSettingsException">The URI is null, please check your settings to make sure you have configured the applications correctly.</exception>
|
||||
/// <exception cref="System.Exception"></exception>
|
||||
public static Uri ReturnUri(this string val, int port, bool ssl = default(bool))
|
||||
{
|
||||
if (val == null)
|
||||
{
|
||||
throw new ApplicationSettingsException("The URI is null, please check your settings to make sure you have configured the applications correctly.");
|
||||
}
|
||||
try
|
||||
{
|
||||
var uri = new UriBuilder();
|
||||
|
||||
if (val.StartsWith("http://", StringComparison.Ordinal))
|
||||
{
|
||||
var split = val.Split('/');
|
||||
uri = split.Length >= 4 ? new UriBuilder(Uri.UriSchemeHttp, split[2], port, "/" + split[3]) : new UriBuilder(new Uri($"{val}:{port}"));
|
||||
}
|
||||
else if (val.StartsWith("https://", StringComparison.Ordinal))
|
||||
{
|
||||
var split = val.Split('/');
|
||||
uri = split.Length >= 4
|
||||
? new UriBuilder(Uri.UriSchemeHttps, split[2], port, "/" + split[3])
|
||||
: new UriBuilder(Uri.UriSchemeHttps, split[2], port);
|
||||
}
|
||||
else if(ssl)
|
||||
{
|
||||
uri = new UriBuilder(Uri.UriSchemeHttps, val, port);
|
||||
}
|
||||
else
|
||||
{
|
||||
uri = new UriBuilder(Uri.UriSchemeHttp, val, port);
|
||||
}
|
||||
|
||||
return uri.Uri;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
throw new Exception(exception.Message, exception);
|
||||
}
|
||||
}
|
||||
|
||||
public static Uri ReturnUriWithSubDir(this string val, int port, bool ssl, string subDir)
|
||||
{
|
||||
var uriBuilder = new UriBuilder(val);
|
||||
if (ssl)
|
||||
{
|
||||
uriBuilder.Scheme = Uri.UriSchemeHttps;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(subDir))
|
||||
{
|
||||
uriBuilder.Path = subDir;
|
||||
}
|
||||
uriBuilder.Port = port;
|
||||
|
||||
return uriBuilder.Uri;
|
||||
}
|
||||
}
|
||||
}
|
12
Ombi.Helpers/UserClaims.cs
Normal file
12
Ombi.Helpers/UserClaims.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
namespace Ombi.Helpers
|
||||
{
|
||||
public class UserClaims
|
||||
{
|
||||
public const string Admin = nameof(Admin); // Can do everything including creating new users and editing settings
|
||||
public const string PowerUser = nameof(PowerUser); // Can only manage the requests, approve etc.
|
||||
public const string RegularUser = nameof(RegularUser); // Can only request
|
||||
public const string ReadOnlyUser = nameof(ReadOnlyUser); // Can only view stuff
|
||||
public const string Newsletter = nameof(Newsletter); // Has newsletter feature enabled
|
||||
}
|
||||
}
|
||||
|
35
Ombi.Helpers/UserType.cs
Normal file
35
Ombi.Helpers/UserType.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: UserType.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
namespace Ombi.Helpers
|
||||
{
|
||||
|
||||
public enum UserType
|
||||
{
|
||||
PlexUser,
|
||||
LocalUser
|
||||
}
|
||||
}
|
11
Ombi.Helpers/app.config
Normal file
11
Ombi.Helpers/app.config
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
</configuration>
|
8
Ombi.Helpers/packages.config
Normal file
8
Ombi.Helpers/packages.config
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Nancy" version="1.4.3" targetFramework="net45" />
|
||||
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net45" />
|
||||
<package id="Ninject" version="3.2.0.0" targetFramework="net45" />
|
||||
<package id="NLog" version="4.3.6" targetFramework="net45" />
|
||||
<package id="Owin" version="1.0" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Add table
Add a link
Reference in a new issue