diff --git a/src/Kyoo.Abstractions/Models/Exceptions/HealthException.cs b/src/Kyoo.Abstractions/Models/Exceptions/HealthException.cs new file mode 100644 index 00000000..6b4d86e2 --- /dev/null +++ b/src/Kyoo.Abstractions/Models/Exceptions/HealthException.cs @@ -0,0 +1,47 @@ +// Kyoo - A portable and vast media library solution. +// Copyright (c) Kyoo. +// +// See AUTHORS.md and LICENSE file in the project root for full license information. +// +// Kyoo is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// any later version. +// +// Kyoo is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Kyoo. If not, see . + +using System; +using System.Runtime.Serialization; + +namespace Kyoo.Abstractions.Models.Exceptions +{ + /// + /// An exception thrown when a part of the app has a fatal issue. + /// + [Serializable] + public class HealthException : Exception + { + /// + /// Create a new with a custom message. + /// + /// The message to use. + public HealthException(string message) + : base(message) + { } + + /// + /// The serialization constructor + /// + /// Serialization infos + /// The serialization context + protected HealthException(SerializationInfo info, StreamingContext context) + : base(info, context) + { } + } +} diff --git a/src/Kyoo.Core/Controllers/Transcoder.cs b/src/Kyoo.Core/Controllers/Transcoder.cs index dd159608..bff0ddd1 100644 --- a/src/Kyoo.Core/Controllers/Transcoder.cs +++ b/src/Kyoo.Core/Controllers/Transcoder.cs @@ -24,6 +24,7 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; +using Kyoo.Abstractions.Models.Exceptions; using Kyoo.Core.Models.Options; using Kyoo.Core.Models.Watch; using Kyoo.Utils; @@ -177,6 +178,11 @@ namespace Kyoo.Core.Controllers /// private readonly ILogger _logger; + /// + /// if the C library has been checked, otherwise. + /// + private bool _initialized; + /// /// Create a new . /// @@ -190,14 +196,29 @@ namespace Kyoo.Core.Controllers _files = files; _options = options; _logger = logger; + } - if (TranscoderAPI.Init() != Marshal.SizeOf()) - _logger.LogCritical("The transcoder library could not be initialized correctly"); + /// + /// Check if the C library can be used or if there is an issue with it. + /// + /// Should the healthcheck be abborted if the transcoder was already initialized? + /// If the transcoder is corrupted, this exception in thrown. + public void CheckHealth(bool fastStop = false) + { + if (fastStop && _initialized) + return; + if (TranscoderAPI.Init() == Marshal.SizeOf()) + return; + _initialized = true; + _logger.LogCritical("The transcoder library could not be initialized correctly"); + throw new HealthException("The transcoder library is corrupted or invalid."); } /// public async Task> ExtractInfos(Episode episode, bool reExtract) { + CheckHealth(true); + string dir = await _files.GetExtraDirectory(episode); if (dir == null) throw new ArgumentException("Invalid path."); @@ -230,6 +251,8 @@ namespace Kyoo.Core.Controllers /// public IActionResult Transmux(Episode episode) { + CheckHealth(true); + string folder = Path.Combine(_options.Value.TransmuxPath, episode.Slug); string manifest = Path.GetFullPath(Path.Combine(folder, episode.Slug + ".m3u8")); diff --git a/src/Kyoo.Core/Views/Resources/ShowApi.cs b/src/Kyoo.Core/Views/Resources/ShowApi.cs index 6b8c186a..23261724 100644 --- a/src/Kyoo.Core/Views/Resources/ShowApi.cs +++ b/src/Kyoo.Core/Views/Resources/ShowApi.cs @@ -18,7 +18,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; @@ -28,10 +27,8 @@ using Kyoo.Abstractions.Models.Attributes; using Kyoo.Abstractions.Models.Exceptions; using Kyoo.Abstractions.Models.Permissions; using Kyoo.Abstractions.Models.Utils; -using Kyoo.Core.Models.Options; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; -using Microsoft.Extensions.Options; using static Kyoo.Abstractions.Models.Utils.Constants; namespace Kyoo.Core.Api @@ -53,17 +50,6 @@ namespace Kyoo.Core.Api /// private readonly ILibraryManager _libraryManager; - /// - /// The file manager used to send images and fonts. - /// - private readonly IFileSystem _files; - - /// - /// The base URL of Kyoo. This will be used to create links for images and - /// . - /// - private readonly Uri _baseURL; - /// /// Create a new . /// @@ -72,18 +58,12 @@ namespace Kyoo.Core.Api /// /// The file manager used to send images and fonts. /// The thumbnail manager used to retrieve images paths. - /// - /// Options used to retrieve the base URL of Kyoo. - /// public ShowApi(ILibraryManager libraryManager, IFileSystem files, - IThumbnailsManager thumbs, - IOptions options) + IThumbnailsManager thumbs) : base(libraryManager.ShowRepository, files, thumbs) { _libraryManager = libraryManager; - _files = files; - _baseURL = options.Value.PublicUrl; } /// diff --git a/tests/Kyoo.Tests/Transcoder/TranscoderTests.cs b/tests/Kyoo.Tests/Transcoder/TranscoderTests.cs index 63aaee57..3d0a38d3 100644 --- a/tests/Kyoo.Tests/Transcoder/TranscoderTests.cs +++ b/tests/Kyoo.Tests/Transcoder/TranscoderTests.cs @@ -16,15 +16,12 @@ // You should have received a copy of the GNU General Public License // along with Kyoo. If not, see . -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Kyoo.Abstractions.Controllers; using Kyoo.Abstractions.Models; -using Kyoo.Core.Controllers; using Kyoo.Core.Models.Options; -using Kyoo.Utils; using Microsoft.Extensions.Options; using Moq; using Xunit;