Skip to content

Commit

Permalink
fix: broken logging
Browse files Browse the repository at this point in the history
  • Loading branch information
hexxone committed May 4, 2024
1 parent 77d8ae4 commit c6698f0
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion TeleJelly/Assets/Config/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<!-- Title and Infos -->
<div class="sectionTitleContainer flex align-items-center">
<h2 class="sectionTitle">TeleJelly Configuration</h2>
<h3>Version: <b id="TeleJellyVersion">1.0.5.0</b></h3>
<h3>Version: <b id="TeleJellyVersion">1.0.6.0</b></h3>
<a href="https://github.com/hexxone/TeleJelly"
class="button-link emby-button headerHelpButton"
is="emby-linkbutton"
Expand Down
17 changes: 8 additions & 9 deletions TeleJelly/Controller/TelegramController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

#pragma warning disable CA2254

Expand All @@ -30,7 +29,7 @@ namespace Jellyfin.Plugin.TeleJelly.Controller;
[Route("sso/{Controller}")]
public class TelegramController : ControllerBase
{
private readonly ILogger _logger;
//private readonly ILogger _logger;
private readonly TeleJellyPlugin _instance;

private readonly TelegramHelper _telegramHelper;
Expand All @@ -47,13 +46,13 @@ public class TelegramController : ControllerBase
/// <param name="configurationManager">Instance of the <see cref="IConfigurationManager" /> interface.</param>
/// <exception cref="Exception">if plugin was not properly initialized before usage.</exception>
public TelegramController(
ILogger<TelegramController> logger,
//ILogger logger,
ISessionManager sessionManager,
IUserManager userManager,
ICryptoProvider cryptoProvider,
IConfigurationManager configurationManager)
{
_logger = logger;
//_logger = logger;

if (TeleJellyPlugin.Instance == null)
{
Expand All @@ -62,12 +61,12 @@ public TelegramController(

_instance = TeleJellyPlugin.Instance;

_telegramHelper = new TelegramHelper(_instance, sessionManager, userManager, cryptoProvider, logger);
_telegramHelper = new TelegramHelper(_instance, sessionManager, userManager, cryptoProvider);

// stolen from https://github.com/jellyfin/jellyfin/blob/master/Jellyfin.Api/Controllers/BrandingController.cs
_brandingOptions = configurationManager.GetConfiguration<BrandingOptions>("branding");

_logger.LogDebug("Telegram Controller initialized");
//_logger.LogDebug("Telegram Controller initialized");
}

/// <summary>
Expand All @@ -89,7 +88,7 @@ public async Task<IActionResult> Login()

if (stream == null)
{
_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
//_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
return NotFound();
}

Expand Down Expand Up @@ -143,7 +142,7 @@ public async Task<ActionResult<SsoAuthenticationResult>> Authenticate([FromBody]
}
catch (Exception ex)
{
_logger.LogError(ex.ToString());
//_logger.LogError(ex.ToString());

return StatusCode(500, new SsoAuthenticationResult { ServerAddress = requestBase, ErrorMessage = ex.ToString() });
}
Expand All @@ -170,7 +169,7 @@ public async Task<IActionResult> ExtraFiles([FromRoute] string fileName)
var stream = _instance.GetType().Assembly.GetManifestResourceStream(view.EmbeddedResourcePath);
if (stream == null)
{
_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
//_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
return StatusCode(500, $"Resource not found: {view.EmbeddedResourcePath}");
}

Expand Down
2 changes: 1 addition & 1 deletion TeleJelly/ILRepack.targets
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<InputAssemblies Include="$(TargetPath)" />
<!-- Telegram.Bot requires Newtonsoft.Json, ILogger requires Logging.Abstractions -->
<InputAssemblies Include="@(ReferencePathWithRefAssemblies)"
Condition="'%(filename)' == 'Newtonsoft.Json' Or '%(filename)' == 'Telegram.Bot' Or '%(filename)' == 'Microsoft.Extensions.Logging.Abstractions' Or '%(filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'"/>
Condition="'%(filename)' == 'Newtonsoft.Json' Or '%(filename)' == 'Telegram.Bot' Or '%(filename)' == 'Microsoft.Extensions.DependencyInjection.Abstractions'"/>
</ItemGroup>

<ILRepack
Expand Down
7 changes: 3 additions & 4 deletions TeleJelly/TeleJelly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Jellyfin.Plugin.TeleJelly</RootNamespace>
<AssemblyVersion>1.0.5.0</AssemblyVersion>
<FileVersion>1.0.5.0</FileVersion>
<Version>1.0.5.0</Version>
<AssemblyVersion>1.0.6.0</AssemblyVersion>
<FileVersion>1.0.6.0</FileVersion>
<Version>1.0.6.0</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<MSBuildArgs>/property:GenerateFullPaths=true /consoleloggerparameters:NoSummary</MSBuildArgs>
Expand Down Expand Up @@ -34,7 +34,6 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Jellyfin.Controller" Version="10.8.13" />
<PackageReference Include="Jellyfin.Model" Version="10.8.13" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
<PackageReference Include="MinVer" Version="5.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion TeleJelly/TeleJellyPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public TeleJellyPlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSer
/// - {{JELLYFIN_DEFAULT_LOGIN}} = Fallback Login url
/// - {{TELEGRAM_BOT_NAME}} = Bot Username.
/// </summary>
public PluginPageInfo TelegramLoginPage => new() { Name = "login", EmbeddedResourcePath = $"{GetType().Namespace}.Pages.telegram.login.html" };
public PluginPageInfo TelegramLoginPage => new() { Name = "login", EmbeddedResourcePath = $"{GetType().Namespace}.Assets.Login.login.html" };

/// <summary>
/// Returns the available internal web pages of this plugin.
Expand Down
41 changes: 20 additions & 21 deletions TeleJelly/Telegram/TelegramHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Cryptography;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Net.Http.Headers;

#pragma warning disable CA2254
Expand All @@ -34,40 +33,40 @@ public class TelegramHelper
/// </summary>
private const long AllowedTimeOffset = 30;

private readonly PluginConfiguration _config;
private readonly ICryptoProvider _cryptoProvider;
private static readonly DateTime _unixStart = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private readonly HMACSHA256 _hmac;
private readonly TeleJellyPlugin _instance;
private readonly ILogger _logger;
//private readonly ILogger _logger;
private readonly PluginConfiguration _config;

private readonly ISessionManager _sessionManager;
private readonly IUserManager _userManager;
private readonly ICryptoProvider _cryptoProvider;

private static readonly DateTime _unixStart = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly HMACSHA256 _hmac;

/// <summary>
/// Initializes a new instance of the <see cref="TelegramHelper" /> class.
/// </summary>
/// <param name="instance">of the Plugin.</param>
/// <param name="logger">for outputting errors.</param>
/// <param name="sessionManager">for manual sign-in.</param>
/// <param name="userManager">for getting and creating users.</param>
/// <param name="cryptoProvider">for hashing passwords.</param>
/// <param name="logger">for outputting errors.</param>
internal TelegramHelper(TeleJellyPlugin instance, ISessionManager sessionManager, IUserManager userManager, ICryptoProvider cryptoProvider, ILogger logger)
internal TelegramHelper(TeleJellyPlugin instance, ISessionManager sessionManager, IUserManager userManager, ICryptoProvider cryptoProvider)
{
_instance = instance;
_config = instance.Configuration;
//_logger = logger;

_sessionManager = sessionManager;
_userManager = userManager;
_logger = logger;
_cryptoProvider = cryptoProvider;

using var sha256 = SHA256.Create();
_hmac = new HMACSHA256(sha256.ComputeHash(Encoding.ASCII.GetBytes(_config.BotToken)));

_logger.LogDebug("Telegram Helper initialized");
//_logger.LogDebug("Telegram Helper initialized");
}

/// <summary>
Expand Down Expand Up @@ -105,7 +104,7 @@ public async Task<User> GetOrCreateJellyUser(SortedDictionary<string, string> au
var user = _userManager.GetUserByName(userName);
if (user == null)
{
_logger.LogInformation($"Telegram user '{userName}' doesn't exist, creating...");
//_logger.LogInformation($"Telegram user '{userName}' doesn't exist, creating...");
user = await _userManager.CreateUserAsync(userName).ConfigureAwait(false);

// use a secure random password, can be changed later?
Expand Down Expand Up @@ -168,7 +167,7 @@ public async Task<User> GetOrCreateJellyUser(SortedDictionary<string, string> au
Username = user.Username
};

_logger.LogInformation("Auth request created...");
//_logger.LogInformation("Auth request created...");

return await _sessionManager.AuthenticateDirect(authRequest).ConfigureAwait(false);
}
Expand Down Expand Up @@ -278,7 +277,7 @@ public async Task<bool> DownloadUserImage(User user, SortedDictionary<string, st
var cleanedUrl = HttpUtility.UrlDecode(userPhotoUrl);

var userImgPath = Path.Combine(_instance.ApplicationPaths.PluginsPath, Constants.PluginName, Constants.PluginDataFolder, Constants.UserImageFolder);
_logger.LogDebug("Trying to download image for '{Username}' into '{UserImgPath}'", user.Username, userImgPath);
//_logger.LogDebug("Trying to download image for '{Username}' into '{UserImgPath}'", user.Username, userImgPath);

try
{
Expand All @@ -298,7 +297,7 @@ public async Task<bool> DownloadUserImage(User user, SortedDictionary<string, st
{
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Failed to download user image for {Username} from {PhotoUrl}. StatusCode: {StatusCode}", user.Username, cleanedUrl, response.StatusCode);
//_logger.LogError("Failed to download user image for {Username} from {PhotoUrl}. StatusCode: {StatusCode}", user.Username, cleanedUrl, response.StatusCode);
return false;
}

Expand All @@ -318,13 +317,13 @@ public async Task<bool> DownloadUserImage(User user, SortedDictionary<string, st
user.ProfileImage.LastModified = DateTime.UtcNow;
}

_logger.LogInformation("Successfully downloaded telegram image for '{Username}'.", user.Username);
//_logger.LogInformation("Successfully downloaded telegram image for '{Username}'.", user.Username);
return true;
}
catch (Exception ex)
{
// Log error and return false.
_logger.LogError(ex, "Failed to download telegram image for '{Username}' from '{PhotoUrl}'.", user.Username, cleanedUrl);
//_logger.LogError(ex, "Failed to download telegram image for '{Username}' from '{PhotoUrl}'.", user.Username, cleanedUrl);
return false;
}
}
Expand All @@ -339,19 +338,19 @@ public async Task<bool> SetDefaultUserImage(User user)
var view = _instance.GetExtraFiles().FirstOrDefault(extra => extra.Name == Constants.DefaultUserImageExtraFile);
if (view == null)
{
_logger.LogError("Failed to get DefaultUserImageExtraFile {Resource}", Constants.DefaultUserImageExtraFile);
//_logger.LogError("Failed to get DefaultUserImageExtraFile {Resource}", Constants.DefaultUserImageExtraFile);
return false;
}

var stream = _instance.GetType().Assembly.GetManifestResourceStream(view.EmbeddedResourcePath);
if (stream == null)
{
_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
//_logger.LogError("Failed to get resource {Resource}", view.EmbeddedResourcePath);
return false;
}

var userImgPath = Path.Combine(_instance.ApplicationPaths.PluginsPath, Constants.PluginName, Constants.PluginDataFolder, Constants.UserImageFolder);
_logger.LogDebug("Trying to save default image for '{Username}' into '{UserImgPath}'", user.Username, userImgPath);
//_logger.LogDebug("Trying to save default image for '{Username}' into '{UserImgPath}'", user.Username, userImgPath);

try
{
Expand Down Expand Up @@ -380,13 +379,13 @@ public async Task<bool> SetDefaultUserImage(User user)
user.ProfileImage.LastModified = DateTime.UtcNow;
}

_logger.LogInformation("Successfully set default telegram user image for '{Username}'.", user.Username);
//_logger.LogInformation("Successfully set default telegram user image for '{Username}'.", user.Username);
return true;
}
catch (Exception ex)
{
// Log error and return false.
_logger.LogError(ex, "Failed to set default telegram user image for '{Username}' from '{PhotoUrl}'.", user.Username, view.EmbeddedResourcePath);
//_logger.LogError(ex, "Failed to set default telegram user image for '{Username}' from '{PhotoUrl}'.", user.Username, view.EmbeddedResourcePath);
return false;
}
}
Expand Down

0 comments on commit c6698f0

Please sign in to comment.