mirror of
https://github.com/wabbajack-tools/wabbajack.git
synced 2024-08-30 18:42:17 +00:00
start of rework of download routines
This commit is contained in:
parent
f6305985e4
commit
bd885477a1
@ -167,6 +167,16 @@ namespace Wabbajack.Common
|
||||
return new DynamicIniData(new FileIniDataParser().ReadFile(file));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a INI from the given string
|
||||
/// </summary>
|
||||
/// <param name="file"></param>
|
||||
/// <returns></returns>
|
||||
public static dynamic LoadIniString(this string file)
|
||||
{
|
||||
return new DynamicIniData(new FileIniDataParser().ReadData(new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(file)))));
|
||||
}
|
||||
|
||||
public static void ToJSON<T>(this T obj, string filename)
|
||||
{
|
||||
File.WriteAllText(filename,
|
||||
@ -194,7 +204,7 @@ namespace Wabbajack.Common
|
||||
public static string ToJSON<T>(this T obj)
|
||||
{
|
||||
return JsonConvert.SerializeObject(obj, Formatting.Indented,
|
||||
new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.Auto});
|
||||
new JsonSerializerSettings {TypeNameHandling = TypeNameHandling.All});
|
||||
}
|
||||
|
||||
public static T FromJSON<T>(this string filename)
|
||||
@ -529,5 +539,17 @@ namespace Wabbajack.Common
|
||||
}
|
||||
return outarr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Roundtrips the value throught the JSON routines
|
||||
/// </summary>
|
||||
/// <typeparam name="TV"></typeparam>
|
||||
/// <typeparam name="TR"></typeparam>
|
||||
/// <param name="tv"></param>
|
||||
/// <returns></returns>
|
||||
public static T ViaJSON<T>(this T tv)
|
||||
{
|
||||
return tv.ToJSON().FromJSONString<T>();
|
||||
}
|
||||
}
|
||||
}
|
43
Wabbajack.Test/DownloaderTests.cs
Normal file
43
Wabbajack.Test/DownloaderTests.cs
Normal file
@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Wabbajack.Common;
|
||||
using Wabbajack.Downloaders;
|
||||
using Wabbajack.Validation;
|
||||
using File = Alphaleonis.Win32.Filesystem.File;
|
||||
|
||||
namespace Wabbajack.Test
|
||||
{
|
||||
[TestClass]
|
||||
public class DownloaderTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void MegaDownload()
|
||||
{
|
||||
var ini = @"[General]
|
||||
directURL=https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k";
|
||||
|
||||
var downloader = new MegaDownloader();
|
||||
downloader.Init();
|
||||
var state = (AbstractDownloadState)downloader.GetDownloaderState(ini.LoadIniString());
|
||||
|
||||
Assert.IsNotNull(state);
|
||||
|
||||
var converted = state.ViaJSON();
|
||||
Assert.IsTrue(converted.Verify());
|
||||
var filename = Guid.NewGuid().ToString();
|
||||
|
||||
Assert.IsTrue(converted.IsWhitelisted(new ServerWhitelist {AllowedPrefixes = new List<string>{"https://mega.nz/#!CsMSFaaJ!-uziC4mbJPRy2e4pPk8Gjb3oDT_38Be9fzZ6Ld4NL-k" } }));
|
||||
Assert.IsFalse(converted.IsWhitelisted(new ServerWhitelist { AllowedPrefixes = new List<string>{ "blerg" }}));
|
||||
|
||||
converted.Download(new Archive() {Name = "MEGA Test.txt"}, filename);
|
||||
|
||||
Assert.AreEqual("Lb1iTsz3iyZeHGs3e94TVmOhf22sqtHLhqkCdXbjiyc=", Utils.FileSHA256(filename));
|
||||
|
||||
Assert.AreEqual(File.ReadAllText(filename), "Cheese for Everyone!");
|
||||
}
|
||||
}
|
||||
}
|
@ -61,6 +61,7 @@
|
||||
<Reference Include="AlphaFS, Version=2.2.0.0, Culture=neutral, PublicKeyToken=4d31a58f7d7ad5c9, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\AlphaFS.2.2.6\lib\net452\AlphaFS.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MSTest.TestFramework.1.3.2\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
|
||||
</Reference>
|
||||
@ -73,6 +74,7 @@
|
||||
<Reference Include="System.Transactions" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="DownloaderTests.cs" />
|
||||
<Compile Include="Extensions.cs" />
|
||||
<Compile Include="TestUtils.cs" />
|
||||
<Compile Include="SanityTests.cs" />
|
||||
|
34
Wabbajack/Downloaders/AbstractDownloadState.cs
Normal file
34
Wabbajack/Downloaders/AbstractDownloadState.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Wabbajack.Validation;
|
||||
|
||||
namespace Wabbajack.Downloaders
|
||||
{
|
||||
/// <summary>
|
||||
/// Base for all abstract downloaders
|
||||
/// </summary>
|
||||
public abstract class AbstractDownloadState
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns true if this file is allowed to be downloaded via whitelist
|
||||
/// </summary>
|
||||
/// <param name="whitelist"></param>
|
||||
/// <returns></returns>
|
||||
public abstract bool IsWhitelisted(ServerWhitelist whitelist);
|
||||
|
||||
/// <summary>
|
||||
/// Downloads this file to the given destination location
|
||||
/// </summary>
|
||||
/// <param name="destination"></param>
|
||||
public abstract void Download(Archive a, string destination);
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if this link is still valid
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract bool Verify();
|
||||
}
|
||||
}
|
19
Wabbajack/Downloaders/IDownloader.cs
Normal file
19
Wabbajack/Downloaders/IDownloader.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Wabbajack.Validation;
|
||||
|
||||
namespace Wabbajack.Downloaders
|
||||
{
|
||||
interface IDownloader
|
||||
{
|
||||
/// <summary>
|
||||
/// Setup the module. Run at the start of the application lifecycle
|
||||
/// </summary>
|
||||
void Init();
|
||||
|
||||
AbstractDownloadState GetDownloaderState(dynamic archive_ini);
|
||||
}
|
||||
}
|
68
Wabbajack/Downloaders/MEGADownloader.cs
Normal file
68
Wabbajack/Downloaders/MEGADownloader.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Runtime.InteropServices.WindowsRuntime;
|
||||
using System.Security.Policy;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Alphaleonis.Win32.Filesystem;
|
||||
using CG.Web.MegaApiClient;
|
||||
using Wabbajack.Common;
|
||||
using Wabbajack.Validation;
|
||||
|
||||
namespace Wabbajack.Downloaders
|
||||
{
|
||||
public class MegaDownloader : IDownloader
|
||||
{
|
||||
public void Init()
|
||||
{
|
||||
}
|
||||
|
||||
public AbstractDownloadState GetDownloaderState(dynamic archive_ini)
|
||||
{
|
||||
var url = archive_ini?.General?.directURL;
|
||||
if (url != null && url.StartsWith(Consts.MegaPrefix))
|
||||
return new State {Url = url};
|
||||
return null;
|
||||
}
|
||||
|
||||
public class State : AbstractDownloadState
|
||||
{
|
||||
public string Url { get; set; }
|
||||
|
||||
public override bool IsWhitelisted(ServerWhitelist whitelist)
|
||||
{
|
||||
return whitelist.AllowedPrefixes.Any(p => Url.StartsWith(p));
|
||||
}
|
||||
|
||||
public override void Download(Archive a, string destination)
|
||||
{
|
||||
var client = new MegaApiClient();
|
||||
Utils.Status("Logging into MEGA (as anonymous)");
|
||||
client.LoginAnonymous();
|
||||
var file_link = new Uri(Url);
|
||||
var node = client.GetNodeFromLink(file_link);
|
||||
Utils.Status($"Downloading MEGA file: {a.Name}");
|
||||
client.DownloadFile(file_link, destination);
|
||||
}
|
||||
|
||||
public override bool Verify()
|
||||
{
|
||||
var client = new MegaApiClient();
|
||||
Utils.Status("Logging into MEGA (as anonymous)");
|
||||
client.LoginAnonymous();
|
||||
var file_link = new Uri(Url);
|
||||
try
|
||||
{
|
||||
var node = client.GetNodeFromLink(file_link);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -188,6 +188,9 @@
|
||||
<SubType>Designer</SubType>
|
||||
</ApplicationDefinition>
|
||||
<Compile Include="Data.cs" />
|
||||
<Compile Include="Downloaders\AbstractDownloadState.cs" />
|
||||
<Compile Include="Downloaders\IDownloader.cs" />
|
||||
<Compile Include="Downloaders\MegaDownloader.cs" />
|
||||
<Compile Include="LambdaCommand.cs" />
|
||||
<Compile Include="UI\ModlistPropertiesWindow.xaml.cs">
|
||||
<DependentUpon>ModlistPropertiesWindow.xaml</DependentUpon>
|
||||
@ -327,6 +330,7 @@
|
||||
<EmbeddedResource Include="UI\Icons\github_light.png" />
|
||||
<EmbeddedResource Include="UI\Icons\patreon_light.png" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="..\packages\Fody.5.1.1\build\Fody.targets" Condition="Exists('..\packages\Fody.5.1.1\build\Fody.targets')" />
|
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
|
||||
|
Loading…
Reference in New Issue
Block a user