Skip to content

Commit

Permalink
Removed DeviceDetector.NET
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirx08 committed Dec 6, 2024
1 parent 14654b1 commit 69a802a
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 28 deletions.
23 changes: 23 additions & 0 deletions WeddingShare.UnitTests/Tests/Helpers/DeviceDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using WeddingShare.Enums;
using WeddingShare.Helpers;

namespace WeddingShare.UnitTests.Tests.Helpers
{
public class DeviceDetectorTests
{
[SetUp]
public void Setup()
{
}

[TestCase("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", DeviceType.Desktop)]
[TestCase("Mozilla/5.0 (Linux; Android 7.0; SM-T827R4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.116 Safari/537.36", DeviceType.Tablet)]
[TestCase("Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36", DeviceType.Mobile)]
[TestCase("Googlebot-Image/1.0", DeviceType.Desktop)]
public async Task DeviceDetector_ParseDeviceType(string userAgent, DeviceType expected)
{
var actual = await new DeviceDetector().ParseDeviceType(userAgent);
Assert.That(actual, Is.EqualTo(expected));
}
}
}
11 changes: 10 additions & 1 deletion WeddingShare/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,20 @@ public class AdminController : Controller
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfigHelper _config;
private readonly IDatabaseHelper _database;
private readonly IDeviceDetector _deviceDetector;
private readonly IImageHelper _imageHelper;
private readonly ILogger _logger;
private readonly IStringLocalizer<AdminController> _localizer;

private readonly string UploadsDirectory;
private readonly string ThumbnailsDirectory;

public AdminController(IWebHostEnvironment hostingEnvironment, IConfigHelper config, IDatabaseHelper database, IImageHelper imageHelper, ILogger<AdminController> logger, IStringLocalizer<AdminController> localizer)
public AdminController(IWebHostEnvironment hostingEnvironment, IConfigHelper config, IDatabaseHelper database, IDeviceDetector deviceDetector, IImageHelper imageHelper, ILogger<AdminController> logger, IStringLocalizer<AdminController> localizer)
{
_hostingEnvironment = hostingEnvironment;
_config = config;
_database = database;
_deviceDetector = deviceDetector;
_imageHelper = imageHelper;
_logger = logger;
_localizer = localizer;
Expand Down Expand Up @@ -87,6 +89,13 @@ public async Task<IActionResult> Index()

var model = new IndexModel();

var deviceType = HttpContext.Session.GetString("DeviceType");
if (string.IsNullOrWhiteSpace(deviceType))
{
deviceType = (await _deviceDetector.ParseDeviceType(Request.Headers["User-Agent"].ToString())).ToString();
HttpContext.Session.SetString("DeviceType", deviceType ?? "Desktop");
}

try
{
if (!_config.GetOrDefault("Settings", "Single_Gallery_Mode", false))
Expand Down
28 changes: 8 additions & 20 deletions WeddingShare/Controllers/GalleryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ public class GalleryController : Controller
private readonly IConfigHelper _config;
private readonly IDatabaseHelper _database;
private readonly ISecretKeyHelper _secretKey;
private readonly IMemoryCache _memoryCache;
private readonly IDeviceDetector _deviceDetector;
private readonly ILogger _logger;
private readonly IStringLocalizer<GalleryController> _localizer;

private readonly string UploadsDirectory;
private readonly string ThumbnailsDirectory;

public GalleryController(IWebHostEnvironment hostingEnvironment, IConfigHelper config, IDatabaseHelper database, ISecretKeyHelper secretKey, IMemoryCache cache, ILogger<GalleryController> logger, IStringLocalizer<GalleryController> localizer)
public GalleryController(IWebHostEnvironment hostingEnvironment, IConfigHelper config, IDatabaseHelper database, ISecretKeyHelper secretKey, IDeviceDetector deviceDetector, ILogger<GalleryController> logger, IStringLocalizer<GalleryController> localizer)
{
_hostingEnvironment = hostingEnvironment;
_config = config;
_database = database;
_secretKey = secretKey;
_memoryCache = cache;
_deviceDetector = deviceDetector;
_logger = logger;
_localizer = localizer;

Expand All @@ -51,26 +51,14 @@ public async Task<IActionResult> Index(string id = "default", string? key = null
id = "default";
}

try
var deviceType = HttpContext.Session.GetString("DeviceType");
if (string.IsNullOrWhiteSpace(deviceType))
{
var userAgent = Request.Headers["User-Agent"].ToString();
if (!_memoryCache.TryGetValue<bool>(userAgent, out var isMobile))
{
var dd = new DeviceDetectorNET.DeviceDetector(userAgent);
dd.Parse();

isMobile = dd.IsParsed() && dd.IsMobile();

_memoryCache.Set(userAgent, isMobile);
}

ViewBag.IsMobile = isMobile;
}
catch
{
ViewBag.IsMobile = false;
deviceType = (await _deviceDetector.ParseDeviceType(Request.Headers["User-Agent"].ToString())).ToString();
HttpContext.Session.SetString("DeviceType", deviceType ?? "Desktop");
}

ViewBag.IsMobile = !string.Equals("Desktop", deviceType, StringComparison.OrdinalIgnoreCase);
ViewBag.SecretKey = key;

var galleryPath = Path.Combine(UploadsDirectory, id);
Expand Down
14 changes: 12 additions & 2 deletions WeddingShare/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,25 @@ namespace WeddingShare.Controllers
[AllowAnonymous]
public class HomeController : Controller
{
private readonly IDeviceDetector _deviceDetector;
private readonly ILogger _logger;
public HomeController(IConfigHelper config, ILogger<HomeController> logger)

public HomeController(IDeviceDetector deviceDetector, ILogger<HomeController> logger)
{
_deviceDetector = deviceDetector;
_logger = logger;
}

[HttpGet]
public IActionResult Index()
public async Task<IActionResult> Index()
{
var deviceType = HttpContext.Session.GetString("DeviceType");
if (string.IsNullOrWhiteSpace(deviceType))
{
deviceType = (await _deviceDetector.ParseDeviceType(Request.Headers["User-Agent"].ToString())).ToString();
HttpContext.Session.SetString("DeviceType", deviceType ?? "Desktop");
}

return View();
}
}
Expand Down
10 changes: 10 additions & 0 deletions WeddingShare/Enums/DeviceType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace WeddingShare.Enums
{
public enum DeviceType
{
Unknown,
Desktop,
Tablet,
Mobile
}
}
3 changes: 0 additions & 3 deletions WeddingShare/Helpers/Database/SQLiteDatabaseHelper.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
using System.Data;
using System.Globalization;
using Microsoft.Data.Sqlite;
using Mono.TextTemplating;
using WeddingShare.Enums;
using WeddingShare.Models.Database;
using YamlDotNet.Core.Tokens;

namespace WeddingShare.Helpers.Database
{
Expand Down
36 changes: 36 additions & 0 deletions WeddingShare/Helpers/DeviceDetector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Text.RegularExpressions;
using WeddingShare.Enums;

namespace WeddingShare.Helpers
{
public interface IDeviceDetector
{
Task<DeviceType> ParseDeviceType(string userAgent);
}

public class DeviceDetector : IDeviceDetector
{
public async Task<DeviceType> ParseDeviceType(string userAgent)
{
return await Task.Run(() =>
{
if (string.IsNullOrEmpty(userAgent))
{
return DeviceType.Unknown;
}

if (Regex.IsMatch(userAgent, "(tablet|ipad|playbook|silk)|(android(?!.*mobile))", RegexOptions.IgnoreCase))
{
return DeviceType.Tablet;
}

if (Regex.IsMatch(userAgent, "blackberry|iphone|mobile|windows ce|opera mini|htc|sony|palm|symbianos|ipad|ipod|blackberry|bada|kindle|symbian|sonyericsson|android|samsung|nokia|wap|motor", RegexOptions.IgnoreCase))
{
return DeviceType.Mobile;
}

return DeviceType.Desktop;
});
}
}
}
2 changes: 2 additions & 0 deletions WeddingShare/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddSingleton<IEnvironmentWrapper, EnvironmentWrapper>();
services.AddSingleton<ISecretKeyHelper, SecretKeyHelper>();
services.AddSingleton<IImageHelper, ImageHelper>();
services.AddSingleton<IDeviceDetector, DeviceDetector>();

var config = new ConfigHelper(new EnvironmentWrapper(), Configuration, _loggerFactory.CreateLogger<ConfigHelper>());
switch (config.GetOrDefault("Database", "Database_Type", "sqlite")?.ToLower())
Expand Down Expand Up @@ -95,6 +96,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestLocalization();
app.UseSession();

app.UseEndpoints(endpoints =>
{
Expand Down
1 change: 0 additions & 1 deletion WeddingShare/WeddingShare.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

<ItemGroup>
<PackageReference Include="dbup-sqlite" Version="5.0.40" />
<PackageReference Include="DeviceDetector.NET" Version="6.4.1" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion WeddingShare/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"Directory_Scanner_Interval": "*/30 * * * *"
},
"Release": {
"Version": "1.1.1"
"Version": "1.1.2"
},
"AllowedHosts": "*"
}

0 comments on commit 69a802a

Please sign in to comment.