Enable plugin updating from website

This commit is contained in:
Brant Martin
2019-03-03 18:46:11 -05:00
parent 2a64151f67
commit 3d3769cf5a
2 changed files with 40 additions and 45 deletions

View File

@@ -79,21 +79,22 @@ namespace Torch.API.WebAPI
return response; return response;
} }
public async Task<bool> DownloadPlugin(Guid guid) public async Task<bool> DownloadPlugin(Guid guid, string path = null)
{ {
return await DownloadPlugin(guid.ToString()); return await DownloadPlugin(guid.ToString(), path);
} }
public async Task<bool> DownloadPlugin(string guid) public async Task<bool> DownloadPlugin(string guid, string path = null)
{ {
var item = await QueryOne(guid); var item = await QueryOne(guid);
return await DownloadPlugin(item); return await DownloadPlugin(item, path);
} }
public async Task<bool> DownloadPlugin(PluginFullItem item) public async Task<bool> DownloadPlugin(PluginFullItem item, string path = null)
{ {
try try
{ {
path = path ?? $"Plugins\\{item.Name}.zip";
var h = await _client.GetAsync(string.Format(PLUGIN_QUERY, item.ID)); var h = await _client.GetAsync(string.Format(PLUGIN_QUERY, item.ID));
string res = await h.Content.ReadAsStringAsync(); string res = await h.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<PluginFullItem>(res); var response = JsonConvert.DeserializeObject<PluginFullItem>(res);
@@ -109,7 +110,7 @@ namespace Torch.API.WebAPI
return false; return false;
} }
var s = await _client.GetStreamAsync(version.URL); var s = await _client.GetStreamAsync(version.URL);
using (var f = new FileStream($"Plugins\\{item.Name}.zip", FileMode.Create)) using (var f = new FileStream(path, FileMode.Create))
{ {
await s.CopyToAsync(f); await s.CopyToAsync(f);
await f.FlushAsync(); await f.FlushAsync();

View File

@@ -14,6 +14,7 @@ using Torch.API;
using Torch.API.Managers; using Torch.API.Managers;
using Torch.API.Plugins; using Torch.API.Plugins;
using Torch.API.Session; using Torch.API.Session;
using Torch.API.WebAPI;
using Torch.Collections; using Torch.Collections;
using Torch.Commands; using Torch.Commands;
using Torch.Utils; using Torch.Utils;
@@ -133,55 +134,48 @@ namespace Torch.Managers
private void DownloadPluginUpdates() private void DownloadPluginUpdates()
{ {
//TODO
_log.Warn("Automatic plugin updates are disabled in this build of Torch while the system is reworked.");
return;
_log.Info("Checking for plugin updates..."); _log.Info("Checking for plugin updates...");
var count = 0; var count = 0;
var pluginItems = Directory.EnumerateFiles(PluginDir, "*.zip").Union(Directory.EnumerateDirectories(PluginDir)); var pluginItems = Directory.EnumerateFiles(PluginDir, "*.zip").Union(Directory.EnumerateDirectories(PluginDir));
Parallel.ForEach(pluginItems, async item => Parallel.ForEach(pluginItems, async item =>
{ {
PluginManifest manifest = null; PluginManifest manifest = null;
//try try
//{ {
// var path = Path.Combine(PluginDir, item); var path = Path.Combine(PluginDir, item);
// var isZip = item.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase); var isZip = item.EndsWith(".zip", StringComparison.CurrentCultureIgnoreCase);
// manifest = isZip ? GetManifestFromZip(path) : GetManifestFromDirectory(path); manifest = isZip ? GetManifestFromZip(path) : GetManifestFromDirectory(path);
// if (manifest == null) if (manifest == null)
// { {
// _log.Warn($"Item '{item}' is missing a manifest, skipping update check."); _log.Warn($"Item '{item}' is missing a manifest, skipping update check.");
// return; return;
// } }
// manifest.Version.TryExtractVersion(out Version currentVersion); manifest.Version.TryExtractVersion(out Version currentVersion);
// var latest = await GetLatestArchiveAsync(manifest.Repository).ConfigureAwait(false); var latest = await PluginQuery.Instance.QueryOne(manifest.Guid);
latest.LatestVersion.TryExtractVersion(out Version newVersion);
// if (currentVersion == null || latest.Item1 == null) if (currentVersion == null || newVersion == null)
// { {
// _log.Error($"Error parsing version from manifest or GitHub for plugin '{manifest.Name}.'"); _log.Error($"Error parsing version from manifest or website for plugin '{manifest.Name}.'");
// return; return;
// } }
// if (latest.Item1 <= currentVersion) if (newVersion <= currentVersion)
// { {
// _log.Debug($"{manifest.Name} {manifest.Version} is up to date."); _log.Debug($"{manifest.Name} {manifest.Version} is up to date.");
// return; return;
// } }
// _log.Info($"Updating plugin '{manifest.Name}' from {currentVersion} to {latest.Item1}."); _log.Info($"Updating plugin '{manifest.Name}' from {currentVersion} to {newVersion}.");
// await UpdatePluginAsync(path, latest.Item2).ConfigureAwait(false); await PluginQuery.Instance.DownloadPlugin(latest, path);
// count++; count++;
//} }
//catch (NotFoundException) catch (Exception e)
//{ {
// _log.Warn($"GitHub repository not found for {manifest.Name}"); _log.Warn($"An error occurred updating the plugin {manifest?.Name ?? item}.");
//} _log.Warn(e);
//catch (Exception e) }
//{
// _log.Warn($"An error occurred updating the plugin {manifest.Name}.");
// _log.Warn(e);
//}
}); });
_log.Info($"Updated {count} plugins."); _log.Info($"Updated {count} plugins.");