Skip to content
Permalink
Browse files

Huge commit! BEWARE! Migrate to ASP.NET Core. Migrate Settings to new…

… modular system.
  • Loading branch information...
da3dsoul committed Apr 16, 2019
1 parent b635151 commit c91145397f5887030e92e5dec0ca8a9aa9a4ddfa
Showing with 9,463 additions and 5,790 deletions.
  1. +1 −1 CloudFileSystem
  2. +250 −130 Shoko.CLI/App.config
  3. +10 −2 Shoko.CLI/Program.cs
  4. +1 −1 Shoko.CLI/Shoko.CLI.csproj
  5. +1 −1 Shoko.Commons
  6. +37 −26 Shoko.Server/API/APIHelper.cs
  7. +15 −0 Shoko.Server/API/Annotations/ApiV3Attribute.cs
  8. +64 −0 Shoko.Server/API/Authentication/CustomAuthHandler.cs
  9. +13 −0 Shoko.Server/API/Authentication/CustomAuthOptions.cs
  10. +30 −0 Shoko.Server/API/Authentication/InitUser.cs
  11. +96 −0 Shoko.Server/API/BaseController.cs
  12. +86 −50 Shoko.Server/API/Bootstrapper.cs
  13. +0 −26 Shoko.Server/API/NancyExtensions.cs
  14. +4 −2 Shoko.Server/API/Negotiation/BinaryProcessor.cs
  15. +4 −2 Shoko.Server/API/Response/ByteArrayResponse.cs
  16. +12 −0 Shoko.Server/API/RestController.cs
  17. +284 −0 Shoko.Server/API/Startup.cs
  18. +23 −0 Shoko.Server/API/UserHandler.cs
  19. +166 −118 Shoko.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation.cs
  20. +384 −307 ...o.Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Entities.cs
  21. +70 −207 ....Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Providers.cs
  22. +76 −36 ....Server/API/v1/Implementations/ShokoServiceImplementation/ShokoServiceImplementation_Utilities.cs
  23. +26 −15 Shoko.Server/API/v1/Implementations/ShokoServiceImplementationImage.cs
  24. +30 −12 Shoko.Server/API/v1/Implementations/ShokoServiceImplementationKodi.cs
  25. +516 −551 Shoko.Server/API/v1/Implementations/ShokoServiceImplementationMetro.cs
  26. +26 −8 Shoko.Server/API/v1/Implementations/ShokoServiceImplementationPlex.cs
  27. +17 −8 Shoko.Server/API/v1/Implementations/ShokoServiceImplementationStream.cs
  28. +3 −6 Shoko.Server/API/v1/Modules/ImageModule.cs
  29. +4 −2 Shoko.Server/API/v1/Modules/KodiModule.cs
  30. +4 −2 Shoko.Server/API/v1/Modules/MainModule.cs
  31. +4 −2 Shoko.Server/API/v1/Modules/MetroModule.cs
  32. +3 −6 Shoko.Server/API/v1/Modules/PlexModule.cs
  33. +4 −2 Shoko.Server/API/v1/Modules/StreamModule.cs
  34. +90 −0 Shoko.Server/API/v1/StreamWithResponse.cs
  35. +5 −4 Shoko.Server/API/v2/Models/common/Episode.cs
  36. +3 −2 Shoko.Server/API/v2/Models/common/Filter.cs
  37. +3 −2 Shoko.Server/API/v2/Models/common/Filters.cs
  38. +10 −4 Shoko.Server/API/v2/Models/common/Group.cs
  39. +4 −3 Shoko.Server/API/v2/Models/common/RawFile.cs
  40. +3 −2 Shoko.Server/API/v2/Models/common/SearchResult.cs
  41. +18 −10 Shoko.Server/API/v2/Models/common/Serie.cs
  42. +15 −2 Shoko.Server/API/v2/Models/core/APIMessage.cs
  43. +3 −1 Shoko.Server/API/v2/Models/core/API_Call_Parameters.cs
  44. +8 −1 Shoko.Server/API/v2/Models/core/AuthUser.cs
  45. +2 −2 Shoko.Server/API/v2/Models/core/Credentials.cs
  46. +3 −1 Shoko.Server/API/v2/Models/core/DatabaseSettings.cs
  47. +1 −1 Shoko.Server/API/v2/Models/core/QueueInfo.cs
  48. +1 −1 Shoko.Server/API/v2/Models/core/Setting.cs
  49. +34 −29 Shoko.Server/API/v2/Modules/Auth.cs
  50. +451 −628 Shoko.Server/API/v2/Modules/Common.cs
  51. +156 −293 Shoko.Server/API/v2/Modules/Core.cs
  52. +15 −18 Shoko.Server/API/v2/Modules/DashboardModules.cs
  53. +13 −17 Shoko.Server/API/v2/Modules/Dev.cs
  54. +59 −78 Shoko.Server/API/v2/Modules/Image.cs
  55. +135 −113 Shoko.Server/API/v2/Modules/Init.cs
  56. +78 −68 Shoko.Server/API/v2/Modules/PlexWebhook.cs
  57. +16 −16 Shoko.Server/API/v2/Modules/Version.cs
  58. +50 −52 Shoko.Server/API/v2/Modules/Webui.cs
  59. +6 −6 Shoko.Server/API/v2/Modules/Webui_Redirect.cs
  60. +23 −0 Shoko.Server/API/v3/Controllers/EpisodeController.cs
  61. +22 −0 Shoko.Server/API/v3/Controllers/FileController.cs
  62. +34 −0 Shoko.Server/API/v3/Controllers/ImageController.cs
  63. +108 −0 Shoko.Server/API/v3/Controllers/ImportFolderController.cs
  64. +73 −0 Shoko.Server/API/v3/Controllers/SeriesController.cs
  65. +36 −0 Shoko.Server/API/v3/Models/Common/BaseModel.cs
  66. +22 −0 Shoko.Server/API/v3/Models/Common/Description.cs
  67. +312 −0 Shoko.Server/API/v3/Models/Common/Image.cs
  68. +38 −0 Shoko.Server/API/v3/Models/Common/Rating.cs
  69. +68 −0 Shoko.Server/API/v3/Models/Common/Role.cs
  70. +47 −0 Shoko.Server/API/v3/Models/Common/Sizes.cs
  71. +35 −0 Shoko.Server/API/v3/Models/Common/Title.cs
  72. +457 −0 Shoko.Server/API/v3/Models/Shoko/Series.cs
  73. +19 −0 Shoko.Server/API/v3/Models/Stubs/BaseStub.cs
  74. +26 −0 Shoko.Server/API/v3/Models/Stubs/SeriesStub.cs
  75. +33 −0 Shoko.Server/API/v3/README.md
  76. +4 −3 Shoko.Server/Analytics.cs
  77. +19 −18 Shoko.Server/AniDBHelper.cs
  78. +3 −2 Shoko.Server/AniDB_API/APIUtils.cs
  79. +2 −1 Shoko.Server/AniDB_API/AniDBHTTPHelper.cs
  80. +3 −2 Shoko.Server/AniDB_API/Commands/AniDBCommand_UpdateFile.cs
  81. +3 −2 Shoko.Server/AniDB_API/Commands/AniDBHTTPCommand_GetMyList.cs
  82. +4 −3 Shoko.Server/AniDB_API/Commands/AniDBUDPCommand.cs
  83. +2 −1 Shoko.Server/AniDB_API/Titles/AniDB_TitleHelper.cs
  84. +13 −12 Shoko.Server/Commands/AniDB/CommandRequest_AddFileToMyList.cs
  85. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_DeleteFileFromMyList.cs
  86. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_GetAnimeHTTP.cs
  87. +156 −155 Shoko.Server/Commands/AniDB/CommandRequest_GetCalendar.cs
  88. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_GetReleaseGroupStatus.cs
  89. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_GetUpdated.cs
  90. +9 −8 Shoko.Server/Commands/AniDB/CommandRequest_SyncMyList.cs
  91. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_SyncMyVotes.cs
  92. +2 −1 Shoko.Server/Commands/AniDB/CommandRequest_UpdateMyListStats.cs
  93. +3 −2 Shoko.Server/Commands/Azure/CommandRequest_Azure_SendAnimeXML.cs
  94. +256 −255 Shoko.Server/Commands/CommandProcessorGeneral.cs
  95. +247 −246 Shoko.Server/Commands/CommandProcessorHasher.cs
  96. +247 −246 Shoko.Server/Commands/CommandProcessorImages.cs
  97. +5 −7 Shoko.Server/Commands/Import/CommandRequest_DownloadAniDBImages.cs
  98. +2 −4 Shoko.Server/Commands/Import/CommandRequest_DownloadImage.cs
  99. +0 −2 Shoko.Server/Commands/Import/CommandRequest_HashFile.cs
  100. +3 −2 Shoko.Server/Commands/Import/CommandRequest_LinkFileManually.cs
  101. +8 −7 Shoko.Server/Commands/Import/CommandRequest_ProcessFile.cs
  102. +9 −8 Shoko.Server/Commands/Import/CommandRequest_ValidateAllImages.cs
  103. +3 −2 Shoko.Server/Commands/MovieDB/CommandRequest_MovieDBSearchAnime.cs
  104. +118 −117 Shoko.Server/Commands/Plex/CommandRequest_PlexSyncWatched.cs
  105. +2 −1 Shoko.Server/Commands/Trakt/CommandRequest_TraktCollectionEpisode.cs
  106. +2 −1 Shoko.Server/Commands/Trakt/CommandRequest_TraktHistoryEpisode.cs
  107. +3 −2 Shoko.Server/Commands/Trakt/CommandRequest_TraktSearchAnime.cs
  108. +3 −2 Shoko.Server/Commands/Trakt/CommandRequest_TraktSyncCollection.cs
  109. +2 −1 Shoko.Server/Commands/Trakt/CommandRequest_TraktSyncCollectionSeries.cs
  110. +2 −1 Shoko.Server/Commands/Trakt/CommandRequest_TraktUpdateAllSeries.cs
  111. +3 −2 Shoko.Server/Commands/TvDB/CommandRequest_TvDBSearchAnime.cs
  112. +3 −3 Shoko.Server/Constants.cs
  113. +11 −10 Shoko.Server/Databases/BaseDatabase.cs
  114. +6 −5 Shoko.Server/Databases/DatabaseFactory.cs
  115. +3 −2 Shoko.Server/Databases/DatabaseFixes.cs
  116. +17 −16 Shoko.Server/Databases/MySQL.cs
  117. +12 −12 Shoko.Server/Databases/SQLServer.cs
  118. +3 −2 Shoko.Server/Databases/SQLite.cs
  119. +1 −6 Shoko.Server/Extensions/ImageResolvers.TOBEMOVETOCOMMONS.cs
  120. +33 −46 Shoko.Server/Extensions/ModelProviders.cs
  121. +5 −5 Shoko.Server/FileHelper/FileHashHelper.cs
  122. +1 −3 Shoko.Server/FileHelper/Hasher.cs
  123. +0 −1 Shoko.Server/FileHelper/MediaInfo/Convert.cs
  124. +406 −408 Shoko.Server/FileHelper/MediaInfo/MediaInfoDLL.cs
  125. +0 −1 Shoko.Server/FileHelper/NativeHasher.cs
  126. +0 −2 Shoko.Server/FileHelper/Subtitles/TextSubtitles.cs
  127. +0 −2 Shoko.Server/FileHelper/Subtitles/VobSubSubtitles.cs
  128. +4 −2 Shoko.Server/ImageDownload/ImageUtils.cs
  129. +36 −35 Shoko.Server/Import/Importer.cs
  130. +3 −2 Shoko.Server/Languages.cs
  131. +13 −7 Shoko.Server/LogRotator.cs
  132. +6 −5 Shoko.Server/Models/SVR_AniDB_Anime.cs
  133. +1 −2 Shoko.Server/Models/SVR_AniDB_File.cs
  134. +2 −1 Shoko.Server/Models/SVR_AnimeGroup.cs
  135. +2 −1 Shoko.Server/Models/SVR_AnimeSeries.cs
  136. +1 −1 Shoko.Server/Models/SVR_ImportFolder.cs
  137. +11 −4 Shoko.Server/Models/SVR_JMMUser.cs
  138. +1 −1 Shoko.Server/Models/SVR_Scan.cs
  139. +9 −7 Shoko.Server/Models/SVR_VideoLocal.cs
  140. +4 −3 Shoko.Server/Models/SVR_VideoLocal_Place.cs
  141. +333 −332 Shoko.Server/Plex/PlexHelper.cs
  142. +1 −1 Shoko.Server/PlexAndKodi/BaseObject.cs
  143. +4 −3 Shoko.Server/PlexAndKodi/CommonImplementation.cs
  144. +11 −10 Shoko.Server/PlexAndKodi/Helper.cs
  145. +12 −17 Shoko.Server/PlexAndKodi/HttpExtensions.cs
  146. +3 −2 Shoko.Server/PlexAndKodi/IProvider.cs
  147. +4 −2 Shoko.Server/PlexAndKodi/Kodi/KodiProvider.cs
  148. +4 −2 Shoko.Server/PlexAndKodi/Plex/PlexProvider.cs
  149. +24 −216 Shoko.Server/Providers/Azure/AzureWebAPI.cs
  150. +5 −4 Shoko.Server/Providers/MovieDB/MovieDBHelper.cs
  151. +38 −37 Shoko.Server/Providers/TraktTV/TraktTVHelper.cs
  152. +10 −9 Shoko.Server/Providers/TvDB/TvDBApiHelper.cs
  153. +1 −1 Shoko.Server/Renamer/LegacyRenamer.cs
  154. +1 −2 Shoko.Server/Renamer/RenameFileHelper.cs
  155. +1 −1 Shoko.Server/Repositories/Cached/AnimeEpisodeRepository.cs
  156. +1 −1 Shoko.Server/Repositories/Cached/ImportFolderRepository.cs
  157. +1 −1 Shoko.Server/Repositories/Cached/VideoLocalRepository.cs
  158. +9 −8 Shoko.Server/ServerState.cs
  159. +63 −0 Shoko.Server/Settings/AniDbSettings.cs
  160. +30 −0 Shoko.Server/Settings/DatabaseSettings.cs
  161. +24 −0 Shoko.Server/Settings/ImportSettings.cs
  162. +9 −0 Shoko.Server/Settings/LinuxSettings.cs
  163. +13 −0 Shoko.Server/Settings/LogRotatorSettings.cs
  164. +32 −22 Shoko.Server/{ServerSettings.cs → Settings/Migration/ServerSettings_Legacy.cs}
  165. +13 −0 Shoko.Server/Settings/MovieDbSettings.cs
  166. +13 −0 Shoko.Server/Settings/PlexSettings.cs
  167. +566 −0 Shoko.Server/Settings/ServerSettings.cs
  168. +21 −0 Shoko.Server/Settings/TraktSettings.cs
  169. +25 −0 Shoko.Server/Settings/TvDBSettings.cs
  170. +25 −0 Shoko.Server/Settings/WebCacheSettings.cs
  171. +721 −31 Shoko.Server/Shoko.Server.csproj
  172. +44 −81 Shoko.Server/ShokoServer.cs
  173. +2 −1 Shoko.Server/Tasks/AnimeGroupCreator.cs
  174. +3 −2 Shoko.Server/Tasks/AutoAnimeGroupCalculator.cs
  175. +2 −1 Shoko.Server/UI/ServerInfo.cs
  176. +6 −10 Shoko.Server/Utilities/AVDumpHelper.cs
  177. +1 −11 Shoko.Server/Utilities/LeanWork/IO/FileSystem/BufferingFileSystemWatcher.cs
  178. +1 −5 Shoko.Server/Utilities/LeanWork/IO/FileSystem/RecoveringFileSystemWatcher.cs
  179. +2 −4 Shoko.Server/Utils.cs
  180. +275 −152 Shoko.Server/app.config
  181. +228 −9 Shoko.Server/packages.config
  182. +243 −119 Shoko.UI/App.config
  183. +1 −0 Shoko.UI/App.xaml.cs
  184. +4 −3 Shoko.UI/Forms/AboutForm.xaml.cs
  185. +14 −7 Shoko.UI/Forms/InitialSetupForm.xaml.cs
  186. +1 −1 Shoko.UI/Forms/LoginForm.xaml.cs
  187. +75 −84 Shoko.UI/MainWindow.xaml.cs
  188. +2 −4 Shoko.UI/Shoko.UI.csproj
  189. +0 −1 Shoko.UI/packages.config
Submodule CloudFileSystem updated 25 files
+1 −1 NutzCode.CloudFileSystem.Plugins.AmazonCloudDrive/AmazonObject.cs
+0 −3 NutzCode.CloudFileSystem.Plugins.AmazonCloudDrive/NutzCode.CloudFileSystem.Plugins.AmazonCloudDrive.csproj
+0 −1 NutzCode.CloudFileSystem.Plugins.AmazonCloudDrive/packages.config
+1 −1 NutzCode.CloudFileSystem.Plugins.GoogleDrive/GoogleDriveObject.cs
+0 −3 NutzCode.CloudFileSystem.Plugins.GoogleDrive/NutzCode.CloudFileSystem.Plugins.GoogleDrive.csproj
+0 −1 NutzCode.CloudFileSystem.Plugins.GoogleDrive/packages.config
+1 −5 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/DirectoryImplementation.cs
+1 −4 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalDirectory.cs
+1 −6 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalDrive.cs
+2 −7 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalFile.cs
+1 −5 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalFileSystem.cs
+1 −7 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalObject.cs
+1 −6 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/LocalRoot.cs
+0 −3 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/NutzCode.CloudFileSystem.Plugins.LocalFileSystem.csproj
+0 −1 NutzCode.CloudFileSystem.Plugins.LocalFileSystem/packages.config
+0 −3 NutzCode.CloudFileSystem.Plugins.OneDrive/NutzCode.CloudFileSystem.Plugins.OneDrive.csproj
+1 −2 NutzCode.CloudFileSystem.Plugins.OneDrive/OneDriveFile.cs
+0 −1 NutzCode.CloudFileSystem.Plugins.OneDrive/packages.config
+2 −1 NutzCode.CloudFileSystem/AuthorizationFactory.cs
+1 −3 NutzCode.CloudFileSystem/BaseObject.cs
+2 −1 NutzCode.CloudFileSystem/CloudFileSystemPluginFactory.cs
+1 −6 NutzCode.CloudFileSystem/Extensions.cs
+1 −5 NutzCode.CloudFileSystem/MemoryFile.cs
+0 −3 NutzCode.CloudFileSystem/NutzCode.CloudFileSystem.csproj
+0 −1 NutzCode.CloudFileSystem/packages.config

Large diffs are not rendered by default.

@@ -1,6 +1,7 @@
using Shoko.Server;
using System;
using NLog;
using Shoko.Server.Settings;

namespace Shoko.CLI
{
@@ -19,14 +20,14 @@ static void Main(string[] args)
}
}
}

ServerSettings.LoadSettings();
ServerState.Instance.LoadSettings();
ShokoServer.Instance.StartUpServer();

// Ensure that the AniDB socket is initialized. Try to Login, then start the server if successful.
ShokoServer.Instance.RestartAniDBSocket();
if (!ServerSettings.FirstRun)
if (!ServerSettings.Instance.FirstRun)
ShokoServer.RunWorkSetupDB();
else logger.Warn("The Server is NOT STARTED. It needs to be configured via webui or the settings.json");

@@ -38,6 +39,13 @@ static void Main(string[] args)
e.Cancel = true;
};

ServerState.Instance.PropertyChanged += (sender, e) =>
{
if (e.PropertyName == "StartupFailedMessage" && ServerState.Instance.StartupFailed)
{
Console.WriteLine("Startup failed! Error message: " + ServerState.Instance.StartupFailedMessage);
}
};
ShokoService.CmdProcessorGeneral.OnQueueStateChangedEvent +=
ev => Console.WriteLine($"Queue state change: {ev.QueueState.formatMessage()}");

@@ -8,7 +8,7 @@
<OutputType>Exe</OutputType>
<RootNamespace>Shoko.CLI</RootNamespace>
<AssemblyName>Shoko.CLI</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.Net;
using Nancy;
using System.Security.Principal;
using Microsoft.AspNetCore.Http;
using Shoko.Models.Enums;
using Shoko.Models.PlexAndKodi;
using Shoko.Server.API.v2.Models.common;
using Shoko.Server.API.v3;
using Shoko.Server.ImageDownload;
using Shoko.Server.Models;
using Shoko.Server.PlexAndKodi;
@@ -15,47 +18,47 @@ public static class APIHelper
{
#region Contructors

public static string ConstructUnsortUrl(NancyContext ctx, bool short_url = false)
public static string ConstructUnsortUrl(HttpContext ctx, bool short_url = false)
{
return ProperURL(ctx, "/api/file/unsort", short_url);
}

[Obsolete]
public static string ConstructGroupIdUrl(NancyContext ctx, string gid, bool short_url = false)
public static string ConstructGroupIdUrl(HttpContext ctx, string gid, bool short_url = false)
{
return ProperURL(ctx, "__TEST__" + (int) JMMType.Group + "/" + gid, short_url);
}

[Obsolete]
public static string ConstructSerieIdUrl(NancyContext ctx, string sid, bool short_url = false)
public static string ConstructSerieIdUrl(HttpContext ctx, string sid, bool short_url = false)
{
return ProperURL(ctx, "__TEST__" + (int) JMMType.Serie + " / " + sid, short_url);
}

[Obsolete]
public static string ConstructVideoUrl(NancyContext ctx, string vid, JMMType type, bool short_url = false)
public static string ConstructVideoUrl(HttpContext ctx, string vid, JMMType type, bool short_url = false)
{
return ProperURL(ctx, "__TEST__" + (int) type + "/" + vid, short_url);
}

public static string ConstructFilterIdUrl(NancyContext ctx, int groupfilter_id, bool short_url = false)
public static string ConstructFilterIdUrl(HttpContext ctx, int groupfilter_id, bool short_url = false)
{
return ProperURL(ctx, "/api/filter?id=" + groupfilter_id, short_url);
}

public static string ConstructFilterUrl(NancyContext ctx, bool short_url = false)
public static string ConstructFilterUrl(HttpContext ctx, bool short_url = false)
{
return ProperURL(ctx, "/api/filter", short_url);
}

[Obsolete]
public static string ConstructFiltersUrl(NancyContext ctx, bool short_url = false)
public static string ConstructFiltersUrl(HttpContext ctx, bool short_url = false)
{
return ProperURL(ctx, "__TEST__", short_url);
}

[Obsolete]
public static string ConstructSearchUrl(NancyContext ctx, string limit, string query, bool searchTag, bool short_url = false)
public static string ConstructSearchUrl(HttpContext ctx, string limit, string query, bool searchTag, bool short_url = false)
{
if (searchTag)
{
@@ -68,33 +71,33 @@ public static string ConstructSearchUrl(NancyContext ctx, string limit, string q
}

[Obsolete]
public static string ConstructPlaylistUrl(NancyContext ctx, bool short_url = false)
public static string ConstructPlaylistUrl(HttpContext ctx, bool short_url = false)
{
return ProperURL(ctx, "/api/metadata/" + (int) JMMType.Playlist + "/0", short_url);
}

[Obsolete]
public static string ConstructPlaylistIdUrl(NancyContext ctx, int pid, bool short_url = false)
public static string ConstructPlaylistIdUrl(HttpContext ctx, int pid, bool short_url = false)
{
return ProperURL(ctx, "/api/metadata/" + (int) JMMType.Playlist + "/" + pid, short_url);
}

public static string ConstructSupportImageLink(NancyContext ctx, string name, bool short_url = true)
public static string ConstructSupportImageLink(HttpContext ctx, string name, bool short_url = true)
{
return ProperURL(ctx, "/api/image/support/" + name, short_url);
}

public static string ConstructImageLinkFromRest(NancyContext ctx, string path, bool short_url = true)
public static string ConstructImageLinkFromRest(HttpContext ctx, string path, bool short_url = true)
{
return ConvertRestImageToNonRestUrl(ctx, path, short_url);
}

public static string ConstructImageLinkFromTypeAndId(NancyContext ctx, int type, int id, bool short_url = true)
public static string ConstructImageLinkFromTypeAndId(HttpContext ctx, int type, int id, bool short_url = true)
{
return ProperURL(ctx, "/api/image/" + type + "/" + id, short_url);
return APIHelper.ProperURL(ctx, "/apiv3/image/" + Image.GetSourceAndTypeFromImageType((ImageEntityType) type) + id, short_url);
}

public static string ConstructVideoLocalStream(NancyContext ctx, int userid, string vid, string name, bool autowatch)
public static string ConstructVideoLocalStream(HttpContext ctx, int userid, string vid, string name, bool autowatch)
{
return ProperURL(ctx, "/Stream/" + vid + "/" + userid + "/" + autowatch + "/" + name);
}
@@ -103,7 +106,7 @@ public static string ConstructVideoLocalStream(NancyContext ctx, int userid, str

#region Converters

private static string ConvertRestImageToNonRestUrl(NancyContext ctx, string url, bool short_url)
private static string ConvertRestImageToNonRestUrl(HttpContext ctx, string url, bool short_url)
{
// Rest URLs should always end in either type/id or type/id/ratio
// Regardless of ',' or '.', ratio will not parse as int
@@ -128,7 +131,7 @@ private static string ConvertRestImageToNonRestUrl(NancyContext ctx, string url,
return null; // invalid url, which did not end in type/id[/ratio]
}

public static Filter FilterFromGroupFilter(NancyContext ctx, SVR_GroupFilter gg, int uid)
public static Filter FilterFromGroupFilter(HttpContext ctx, SVR_GroupFilter gg, int uid)
{
Filter ob = new Filter
{
@@ -164,7 +167,7 @@ public static Filter FilterFromGroupFilter(NancyContext ctx, SVR_GroupFilter gg,
return ob;
}

public static Filter FilterFromAnimeGroup(NancyContext ctx, SVR_AnimeGroup grp, int uid)
public static Filter FilterFromAnimeGroup(HttpContext ctx, SVR_AnimeGroup grp, int uid)
{
Filter ob = new Filter
{
@@ -216,20 +219,28 @@ public static Filter FilterFromAnimeGroup(NancyContext ctx, SVR_AnimeGroup grp,

#endregion

private static string ProperURL(NancyContext ctx, string path, bool short_url = false)
{
return ProperURL(ctx, ctx.Request.Url.Port, path, short_url);
}

private static string ProperURL(NancyContext ctx, int? port, string path, bool short_url = false)
private static string ProperURL(HttpContext ctx, string path, bool short_url = false)
{
if (!string.IsNullOrEmpty(path))
{
return !short_url
? ctx.Request.Url.Scheme + "://" + ctx.Request.Url.HostName + ":" + port + path
? ctx.Request.Scheme + "://" + ctx.Request.Host.Host + ":" + ctx.Request.Host.Port + path
: path;
}
return string.Empty;
}

public static SVR_JMMUser GetUser(this IIdentity identity)
{
if (!(identity?.IsAuthenticated ?? false)) return null;
return RepoFactory.JMMUser.GetByUsername(identity.Name);
}

public static SVR_JMMUser GetUser(this HttpContext ctx)
{
var identity = ctx?.User?.Identity;
if (!(identity?.IsAuthenticated ?? false)) return null;
return RepoFactory.JMMUser.GetByUsername(identity.Name);
}
}
}
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.AspNetCore.Mvc;

namespace Shoko.Server.API.Annotations
{
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public sealed class ApiV3Attribute : ApiVersionAttribute
{
public ApiV3Attribute() : base(new ApiVersion(3, 0))
{
}
}
}
@@ -0,0 +1,64 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using Shoko.Models.Server;
using Shoko.Server.Models;
using Shoko.Server.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

namespace Shoko.Server.API.Authentication
{
public class CustomAuthHandler : AuthenticationHandler<CustomAuthOptions>
{
public CustomAuthHandler(IOptionsMonitor<CustomAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
if (!ServerState.Instance.ServerOnline)
{
return Task.FromResult(
AuthenticateResult.Success(
new AuthenticationTicket(new ClaimsPrincipal(InitUser.Instance), Options.Scheme)));
}


// Get Authorization header value and join with the query
var authkeys = Request.Headers["apikey"].Union(Request.Query["apikey"]).ToList();

if (authkeys.Count == 0)
{
return Task.FromResult(AuthenticateResult.Fail("Cannot read authorization header or query."));
}

//Find authenticated user.
var user = authkeys.Select(GetUserForKey).FirstOrDefault(s => s != null);

if (user == null) return Task.FromResult(AuthenticateResult.Fail("Invalid Authentication key"));

var ticket = new AuthenticationTicket(new ClaimsPrincipal(user), Options.Scheme);

return Task.FromResult(AuthenticateResult.Success(ticket));
}

private static SVR_JMMUser GetUserForKey(string ctx)
{
if (!(ServerState.Instance?.ServerOnline ?? false)) return null;
string apikey = ctx?.Trim();
if (string.IsNullOrEmpty(apikey)) return null;

AuthTokens auth = RepoFactory.AuthTokens.GetByToken(apikey);
return auth != null ? RepoFactory.JMMUser.GetByID(auth.UserID) : null;
}
}
}
@@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Authentication;
using System;
using System.Collections.Generic;
using System.Text;

namespace Shoko.Server.API.Authentication
{
public class CustomAuthOptions : AuthenticationSchemeOptions
{
public const string DefaultScheme = "ShokoServer";
public string Scheme => DefaultScheme;
}
}
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Security.Principal;
using Shoko.Server.Models;

namespace Shoko.Server.API.Authentication
{
// This is a fake user to aid in Authentication during first run
public class InitUser : SVR_JMMUser, IIdentity
{
public static InitUser Instance { get; } = new InitUser();

private InitUser()
{
JMMUserID = 0;
Username = "init";
Password = "";
IsAdmin = 1;
HideCategories = "";
CanEditServerSettings = 1;

}

[NotMapped] string IIdentity.AuthenticationType => "API";

[NotMapped] bool IIdentity.IsAuthenticated => true;

[NotMapped] string IIdentity.Name => Username;
}
}

0 comments on commit c911453

Please sign in to comment.
You can’t perform that action at this time.