From 1c60b9daaf017f0c8de358da61983fca3dbd6645 Mon Sep 17 00:00:00 2001 From: Diego Heras Date: Tue, 12 May 2020 21:58:48 +0200 Subject: [PATCH] core: allow to rename indexer id (part 3 #8355) (#8612) --- .../{nostalgic.yml => vhstapes.yml} | 2 +- .../Services/IndexerConfigurationService.cs | 25 ++++----- .../Services/IndexerManagerService.cs | 52 +++++++++++++++---- .../IIndexerConfigurationService.cs | 1 + src/Jackett.Updater/Program.cs | 1 + 5 files changed, 58 insertions(+), 23 deletions(-) rename src/Jackett.Common/Definitions/{nostalgic.yml => vhstapes.yml} (99%) diff --git a/src/Jackett.Common/Definitions/nostalgic.yml b/src/Jackett.Common/Definitions/vhstapes.yml similarity index 99% rename from src/Jackett.Common/Definitions/nostalgic.yml rename to src/Jackett.Common/Definitions/vhstapes.yml index b47c08535..77f9ca886 100644 --- a/src/Jackett.Common/Definitions/nostalgic.yml +++ b/src/Jackett.Common/Definitions/vhstapes.yml @@ -1,5 +1,5 @@ --- -id: nostalgic +id: vhstapes name: VHSTAPES description: "VHSTAPES (The Archive / Nostalgic) is a Private Torrent Tracker for MOVIES / TV / GENERAL NOSTALGIA" language: en-us diff --git a/src/Jackett.Common/Services/IndexerConfigurationService.cs b/src/Jackett.Common/Services/IndexerConfigurationService.cs index 042d89ca8..9f0d2efcd 100644 --- a/src/Jackett.Common/Services/IndexerConfigurationService.cs +++ b/src/Jackett.Common/Services/IndexerConfigurationService.cs @@ -33,7 +33,7 @@ namespace Jackett.Common.Services public void Delete(IIndexer indexer) { - var configFilePath = GetIndexerConfigFilePath(indexer); + var configFilePath = GetIndexerConfigFilePath(indexer.Id); File.Delete(configFilePath); var configFilePathBak = configFilePath + ".bak"; if (File.Exists(configFilePathBak)) @@ -42,20 +42,20 @@ namespace Jackett.Common.Services } } - public void Load(IIndexer idx) + public void Load(IIndexer indexer) { - var configFilePath = GetIndexerConfigFilePath(idx); + var configFilePath = GetIndexerConfigFilePath(indexer.Id); if (File.Exists(configFilePath)) { try { var fileStr = File.ReadAllText(configFilePath); var jsonString = JToken.Parse(fileStr); - idx.LoadFromSavedConfiguration(jsonString); + indexer.LoadFromSavedConfiguration(jsonString); } catch (Exception ex) { - logger.Error(ex, "Failed loading configuration for {0}, trying backup", idx.DisplayName); + logger.Error(ex, "Failed loading configuration for {0}, trying backup", indexer.DisplayName); var configFilePathBak = configFilePath + ".bak"; if (File.Exists(configFilePathBak)) { @@ -63,18 +63,18 @@ namespace Jackett.Common.Services { var fileStrBak = File.ReadAllText(configFilePathBak); var jsonStringBak = JToken.Parse(fileStrBak); - idx.LoadFromSavedConfiguration(jsonStringBak); - logger.Info("Successfully loaded backup config for {0}", idx.DisplayName); - idx.SaveConfig(); + indexer.LoadFromSavedConfiguration(jsonStringBak); + logger.Info("Successfully loaded backup config for {0}", indexer.DisplayName); + indexer.SaveConfig(); } catch (Exception exbak) { - logger.Error(exbak, "Failed loading backup configuration for {0}, you must reconfigure this indexer", idx.DisplayName); + logger.Error(exbak, "Failed loading backup configuration for {0}, you must reconfigure this indexer", indexer.DisplayName); } } else { - logger.Error(ex, "Failed loading backup configuration for {0} (no backup available), you must reconfigure this indexer", idx.DisplayName); + logger.Error(ex, "Failed loading backup configuration for {0} (no backup available), you must reconfigure this indexer", indexer.DisplayName); } } } @@ -85,7 +85,7 @@ namespace Jackett.Common.Services lock (configWriteLock) { var uID = Guid.NewGuid().ToString("N"); - var configFilePath = GetIndexerConfigFilePath(indexer); + var configFilePath = GetIndexerConfigFilePath(indexer.Id); var configFilePathBak = configFilePath + ".bak"; var configFilePathTmp = configFilePath + "." + uID + ".tmp"; var content = obj.ToString(); @@ -141,7 +141,8 @@ namespace Jackett.Common.Services } } - private string GetIndexerConfigFilePath(IIndexer indexer) => Path.Combine(configService.GetIndexerConfigDir(), indexer.Id + ".json"); + public string GetIndexerConfigFilePath(string indexerId) + => Path.Combine(configService.GetIndexerConfigDir(), indexerId + ".json"); private readonly IConfigurationService configService; private readonly Logger logger; diff --git a/src/Jackett.Common/Services/IndexerManagerService.cs b/src/Jackett.Common/Services/IndexerManagerService.cs index 09d9237f9..7690b80e6 100644 --- a/src/Jackett.Common/Services/IndexerManagerService.cs +++ b/src/Jackett.Common/Services/IndexerManagerService.cs @@ -30,6 +30,15 @@ namespace Jackett.Common.Services private readonly Dictionary indexers = new Dictionary(); private AggregateIndexer aggregateIndexer; + // this map is used to maintain backward compatibility when renaming the id of an indexer + // (the id is used in the torznab/download/search urls and in the indexer configuration file) + // if the indexer is removed, remove it from this list too + // use: {"", ""} + private readonly Dictionary renamedIndexers = new Dictionary + { + {"nostalgic", "vhstapes"} + }; + public IndexerManagerService(IIndexerConfigurationService config, IProtectionService protectionService, WebClient webClient, Logger l, ICacheService cache, IProcessService processService, IConfigurationService globalConfigService, ServerConfig serverConfig) { configService = config; @@ -44,11 +53,29 @@ namespace Jackett.Common.Services public void InitIndexers(IEnumerable path) { + MigrateRenamedIndexers(); InitIndexers(); InitCardigannIndexers(path); InitAggregateIndexer(); } + private void MigrateRenamedIndexers() + { + foreach (var oldId in renamedIndexers.Keys) + { + var oldPath = configService.GetIndexerConfigFilePath(oldId); + if (File.Exists(oldPath)) + { + // if the old configuration exists, we rename it to be used by the renamed indexer + var newPath = configService.GetIndexerConfigFilePath(renamedIndexers[oldId]); + File.Move(oldPath, newPath); + if (File.Exists(oldPath + ".bak")) + File.Move(oldPath + ".bak", newPath + ".bak"); + logger.Info($"Configuration renamed: {oldPath} => {newPath}"); + } + } + } + private void InitIndexers() { logger.Info("Using HTTP Client: " + webClient.GetType().Name); @@ -179,19 +206,24 @@ namespace Jackett.Common.Services public IIndexer GetIndexer(string name) { - if (indexers.ContainsKey(name)) + // old id of renamed indexer is used to maintain backward compatibility + // both, the old id and the new one can be used until we remove it from renamedIndexers + var realName = name; + if (renamedIndexers.ContainsKey(name)) { - return indexers[name]; + realName = renamedIndexers[name]; + logger.Warn($"Indexer {name} has been renamed to {realName}. Please, update the URL of the feeds. " + + "This may stop working in the future."); } - else if (name == "all") - { + + if (indexers.ContainsKey(realName)) + return indexers[realName]; + + if (realName == "all") return aggregateIndexer; - } - else - { - logger.Error("Request for unknown indexer: " + name); - throw new Exception("Unknown indexer: " + name); - } + + logger.Error("Request for unknown indexer: " + realName); + throw new Exception("Unknown indexer: " + realName); } public IWebIndexer GetWebIndexer(string name) diff --git a/src/Jackett.Common/Services/Interfaces/IIndexerConfigurationService.cs b/src/Jackett.Common/Services/Interfaces/IIndexerConfigurationService.cs index 030f9e04f..a4fd486e9 100644 --- a/src/Jackett.Common/Services/Interfaces/IIndexerConfigurationService.cs +++ b/src/Jackett.Common/Services/Interfaces/IIndexerConfigurationService.cs @@ -8,5 +8,6 @@ namespace Jackett.Common.Services.Interfaces void Load(IIndexer indexer); void Save(IIndexer indexer, JToken config); void Delete(IIndexer indexer); + string GetIndexerConfigFilePath(string indexerId); } } diff --git a/src/Jackett.Updater/Program.cs b/src/Jackett.Updater/Program.cs index e32d37e64..e02b488e7 100644 --- a/src/Jackett.Updater/Program.cs +++ b/src/Jackett.Updater/Program.cs @@ -337,6 +337,7 @@ namespace Jackett.Updater "Definitions/music-master.yml", "Definitions/nachtwerk.yml", "Definitions/nexttorrent.yml", + "Definitions/nostalgic.yml", "Definitions/nyaa.yml", "Definitions/nyoo.yml", "Definitions/passionetorrent.yml",