mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-14 18:47:15 -07:00
First pass of the updater working. #29
This commit is contained in:
parent
eafa0486f8
commit
030c013862
8 changed files with 270 additions and 121 deletions
|
@ -31,6 +31,7 @@
|
|||
<Reference Include="System" />
|
||||
<Reference Include="System.IO.Compression" />
|
||||
<Reference Include="System.IO.Compression.FileSystem" />
|
||||
<Reference Include="System.Windows.Forms" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.cs" />
|
||||
|
@ -50,5 +51,6 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="app.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -7,7 +7,8 @@ namespace PlexRequests.Updater
|
|||
public static void Main (string[] args)
|
||||
{
|
||||
Console.WriteLine ("Starting PlexRequests .Net updater");
|
||||
|
||||
var s = new Updater();
|
||||
s.Start(args[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,42 +1,201 @@
|
|||
using System;
|
||||
using PlexRequests.Core;
|
||||
using System.Net;
|
||||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2016 Jamie Rees
|
||||
// File: Updater.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Compression;
|
||||
using System.Net;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace PlexRequests.Updater
|
||||
{
|
||||
public class Updater
|
||||
{
|
||||
public void Start(){
|
||||
var c = new StatusChecker ();
|
||||
public class Updater
|
||||
{
|
||||
private string BackupPath { get; set; }
|
||||
private bool Error { get; set; }
|
||||
private string TempPath { get; set; }
|
||||
|
||||
try {
|
||||
public void RestoreBackup()
|
||||
{
|
||||
Console.WriteLine("Update failed, restoring backup");
|
||||
using (var archive = ZipFile.OpenRead(BackupPath))
|
||||
{
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
var fullPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)), entry.FullName);
|
||||
|
||||
var release = c.GetStatus ();
|
||||
if(!release.UpdateAvailable)
|
||||
{
|
||||
Console.WriteLine ("No Update availble, shutting down");
|
||||
}
|
||||
if (string.IsNullOrEmpty(entry.Name))
|
||||
{
|
||||
Directory.CreateDirectory(fullPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (entry.Name.Contains("PlexRequests.Updater"))
|
||||
{
|
||||
entry.ExtractToFile(fullPath + "_Updated", true);
|
||||
continue;
|
||||
}
|
||||
|
||||
using(var client = new WebClient())
|
||||
using(var ms = new MemoryStream(client.DownloadData(release.DownloadUri), false))
|
||||
using(var gz = new GZipStream(ms, CompressionLevel.Optimal))
|
||||
{
|
||||
// TODO decompress stream
|
||||
}
|
||||
entry.ExtractToFile(fullPath, true);
|
||||
Console.WriteLine("Update failed, restoring backup");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Start(string downloadPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
BackupCurrentVersion();
|
||||
var dir = CreateTempPath();
|
||||
TempPath = Path.Combine(dir.FullName, "PlexRequestsUpdate.zip");
|
||||
|
||||
CheckAndDelete(TempPath);
|
||||
Console.WriteLine("Downloading new version");
|
||||
using (var client = new WebClient())
|
||||
{
|
||||
client.DownloadFile(downloadPath, TempPath);
|
||||
}
|
||||
Console.WriteLine("Downloaded!");
|
||||
|
||||
|
||||
} catch (Exception ex) {
|
||||
|
||||
Console.WriteLine (ex.Message);
|
||||
Console.WriteLine ("Oops... Looks like we cannot update!");
|
||||
Console.ReadLine ();
|
||||
}
|
||||
}
|
||||
// Replace files
|
||||
using (var archive = ZipFile.OpenRead(TempPath))
|
||||
{
|
||||
foreach (var entry in archive.Entries)
|
||||
{
|
||||
var fullname = string.Empty;
|
||||
if (entry.FullName.Contains("Release/")) // Don't extract the release folder, we are already in there
|
||||
{
|
||||
fullname = entry.FullName.Replace("Release/", string.Empty);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
var fullPath = Path.Combine(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath)), fullname);
|
||||
|
||||
if (string.IsNullOrEmpty(entry.Name))
|
||||
{
|
||||
Directory.CreateDirectory(fullPath);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (entry.Name.Contains("PlexRequests.Updater"))
|
||||
{
|
||||
entry.ExtractToFile(fullPath + "_Updated", true);
|
||||
continue;
|
||||
}
|
||||
|
||||
entry.ExtractToFile(fullPath, true);
|
||||
Console.WriteLine("Restored {0}", entry.FullName);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
Console.WriteLine("Oops... Looks like we cannot update!");
|
||||
Console.ReadLine();
|
||||
Error = true;
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(TempPath);
|
||||
if (Error)
|
||||
{
|
||||
RestoreBackup();
|
||||
}
|
||||
|
||||
FinishUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void BackupCurrentVersion()
|
||||
{
|
||||
Console.WriteLine("Backing up the current version");
|
||||
try
|
||||
{
|
||||
var dir = Directory.CreateDirectory("BackupSystem");
|
||||
var applicationPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath));
|
||||
|
||||
var allfiles = Directory.GetFiles(applicationPath, "*.*", SearchOption.AllDirectories);
|
||||
BackupPath = Path.Combine(dir.FullName, "PlexRequestsBackup.zip");
|
||||
|
||||
CheckAndDelete(BackupPath);
|
||||
using (var fileStream = new FileStream(BackupPath, FileMode.CreateNew))
|
||||
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var file in allfiles)
|
||||
{
|
||||
if (file.Contains("BackupSystem"))
|
||||
continue;
|
||||
var info = Path.GetFileName(file);
|
||||
archive.CreateEntryFromFile(file, info);
|
||||
}
|
||||
}
|
||||
Console.WriteLine("All backed up!");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Console.WriteLine();
|
||||
Environment.Exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void CheckAndDelete(string filePath)
|
||||
{
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
File.Delete(filePath);
|
||||
}
|
||||
}
|
||||
|
||||
private DirectoryInfo CreateTempPath()
|
||||
{
|
||||
try
|
||||
{
|
||||
return Directory.CreateDirectory("UpdateTemp");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
Console.WriteLine();
|
||||
Environment.Exit(1);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private void FinishUpdate()
|
||||
{
|
||||
var startInfo = new ProcessStartInfo("PlexRequests.exe") { Arguments = Error ? "-u false" : "-u true" };
|
||||
Process.Start(startInfo);
|
||||
|
||||
Environment.Exit(0);
|
||||
}
|
||||
}
|
||||
}
|
4
PlexRequests.Updater/packages.config
Normal file
4
PlexRequests.Updater/packages.config
Normal file
|
@ -0,0 +1,4 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Polly-Signed" version="4.2.0" targetFramework="net45" />
|
||||
</packages>
|
Loading…
Add table
Add a link
Reference in a new issue