Port AuthorFilesVM

This commit is contained in:
Timothy Baldridge 2021-12-26 22:53:39 -07:00
parent 26aabf413c
commit eb0b5b0ea5
9 changed files with 47 additions and 49 deletions

View File

@ -14,8 +14,10 @@ using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
using Wabbajack.Common;
using Wabbajack.Hashing.xxHash64;
using Wabbajack.Lib.Extensions;
using Wabbajack.Paths;
using Wabbajack.Paths.IO;
namespace Wabbajack
{
@ -38,12 +40,12 @@ namespace Wabbajack
{
try
{
if (!path.Exists)
if (!path.FileExists())
{
bitmapImage = default;
return false;
}
bitmapImage = new BitmapImage(new Uri((string)path, UriKind.RelativeOrAbsolute));
bitmapImage = new BitmapImage(new Uri(path.ToString(), UriKind.RelativeOrAbsolute));
return true;
}
catch (Exception)
@ -64,7 +66,7 @@ namespace Wabbajack
public static void OpenFolder(AbsolutePath path)
{
Process.Start(new ProcessStartInfo(AbsolutePath.WindowsFolder.Combine("explorer.exe").ToString(), path.ToString())
Process.Start(new ProcessStartInfo(KnownFolders.Windows.Combine("explorer.exe").ToString(), path.ToString())
{
CreateNoWindow = true,
});
@ -81,24 +83,6 @@ namespace Wabbajack
return default;
}
public static IDisposable BindCpuStatus(IObservable<CPUStatus> status, ObservableCollectionExtended<CPUDisplayVM> list)
{
return status.ObserveOn(RxApp.TaskpoolScheduler)
.ToObservableChangeSet(x => x.ID)
.Batch(TimeSpan.FromMilliseconds(50), RxApp.TaskpoolScheduler)
.EnsureUniqueChanges()
.ObserveOnGuiThread()
.TransformAndCache(
onAdded: (key, cpu) => new CPUDisplayVM(cpu),
onUpdated: (change, vm) => vm.AbsorbStatus(change.Current))
.AutoRefresh(x => x.IsWorking)
.AutoRefresh(x => x.StartTime)
.Filter(i => i.IsWorking && i.ID != WorkQueue.UnassignedCpuId)
.Sort(SortExpressionComparer<CPUDisplayVM>.Ascending(s => s.StartTime))
.Bind(list)
.Subscribe();
}
public static IObservable<BitmapImage> DownloadBitmapImage(this IObservable<string> obs, Action<Exception> exceptionHandler)
{
return obs
@ -150,20 +134,20 @@ namespace Wabbajack
private static async Task WriteCachedImage(string url, byte[] data)
{
var folder = Consts.LocalAppDataPath.Combine("ModListImages");
if (!folder.Exists) folder.CreateDirectory();
var folder = KnownFolders.WabbajackAppLocal.Combine("ModListImages");
if (!folder.DirectoryExists()) folder.CreateDirectory();
var path = folder.Combine(Encoding.UTF8.GetBytes(url).xxHash().ToHex());
var path = folder.Combine(Encoding.UTF8.GetBytes(url).Hash().ToHex());
await path.WriteAllBytesAsync(data);
}
private static async Task<(bool Found, MemoryStream data)> FindCachedImage(string uri)
{
var folder = Consts.LocalAppDataPath.Combine("ModListImages");
if (!folder.Exists) folder.CreateDirectory();
var folder = KnownFolders.WabbajackAppLocal.Combine("ModListImages");
if (!folder.DirectoryExists()) folder.CreateDirectory();
var path = folder.Combine(Encoding.UTF8.GetBytes(uri).xxHash().ToHex());
return path.Exists ? (true, new MemoryStream(await path.ReadAllBytesAsync())) : (false, default);
var path = folder.Combine(Encoding.UTF8.GetBytes(uri).Hash().ToHex());
return path.FileExists() ? (true, new MemoryStream(await path.ReadAllBytesAsync())) : (false, default);
}
/// <summary>

View File

@ -1,27 +1,23 @@
using System;
using System.Reactive;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Wabbajack.Common;
using Wabbajack.Lib.AuthorApi;
using Wabbajack.Lib.FileUploader;
using Wabbajack.Networking.Http.Interfaces;
using Wabbajack.Lib;
using Wabbajack.Networking.WabbajackClientApi;
using Wabbajack.Services.OSIntegrated.TokenProviders;
namespace Wabbajack
namespace Wabbajack.View_Models.Settings
{
public class AuthorFilesVM : BackNavigatingVM
{
private readonly ObservableAsPropertyHelper<Visibility> _isVisible;
[Reactive]
public Visibility IsVisible { get; set; };
public Visibility IsVisible { get; set; }
public ICommand SelectFile { get; }
public ICommand HyperlinkCommand { get; }
@ -32,13 +28,15 @@ namespace Wabbajack
[Reactive] public string FinalUrl { get; set; }
public FilePickerVM Picker { get;}
private Subject<bool> _isUploading = new Subject<bool>();
private Subject<bool> _isUploading = new();
private readonly WabbajackApiTokenProvider _token;
private readonly Client _wjClient;
private IObservable<bool> IsUploading { get; }
public AuthorFilesVM(WabbajackApiTokenProvider token, SettingsVM vm) : base(vm.MWVM)
public AuthorFilesVM(WabbajackApiTokenProvider token, Client wjClient, SettingsVM vm) : base(vm.MWVM)
{
_token = token;
_wjClient = wjClient;
IsUploading = _isUploading;
Picker = new FilePickerVM(this);
@ -57,8 +55,8 @@ namespace Wabbajack
ManageFiles = ReactiveCommand.Create(async () =>
{
var authorApiKey = await AuthorAPI.GetAPIKey();
Utils.OpenWebsite(new Uri($"{Consts.WabbajackBuildServerUri}author_controls/login/{authorApiKey}"));
var authorApiKey = (await token.Get())!.AuthorKey;
UIUtils.OpenWebsite(new Uri($"{Consts.WabbajackBuildServerUri}author_controls/login/{authorApiKey}"));
});
Upload = ReactiveCommand.Create(async () =>
@ -66,14 +64,17 @@ namespace Wabbajack
_isUploading.OnNext(true);
try
{
using var queue = new WorkQueue();
var result = await (await Client.Create()).UploadFile(queue, Picker.TargetPath,
(msg, progress) =>
{
FinalUrl = msg;
UploadProgress = (double)progress;
});
FinalUrl = result.ToString();
var (progress, task) = _wjClient.UploadAuthorFile(Picker.TargetPath);
var disposable = progress.Subscribe(m =>
{
FinalUrl = m.Message;
UploadProgress = (double)m.PercentDone;
});
var final = await task;
disposable.Dispose();
FinalUrl = final.ToString();
}
catch (Exception ex)
{

View File

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows.Input;
using ReactiveUI;
using Wabbajack.Lib;
using Wabbajack.View_Models.Settings;
namespace Wabbajack
{

View File

@ -6,9 +6,10 @@
xmlns:local="clr-namespace:Wabbajack"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:rxui="http://reactiveui.net"
xmlns:settings="clr-namespace:Wabbajack.View_Models.Settings"
d:DesignHeight="450"
d:DesignWidth="800"
x:TypeArguments="local:AuthorFilesVM"
x:TypeArguments="settings:AuthorFilesVM"
mc:Ignorable="d">
<Border
x:Name="AuthorFiles"

View File

@ -1,5 +1,6 @@
using System.Windows.Controls;
using ReactiveUI;
using Wabbajack.View_Models.Settings;
namespace Wabbajack
{

View File

@ -18,4 +18,5 @@ public abstract class IPS4OAuth2 : ADownloadState, IMetaState
public Uri? ImageURL { get; set; }
public bool IsNSFW { get; set; }
public string? Description { get; set; }
public Uri? LinkUrl => null;
}

View File

@ -26,6 +26,7 @@ public class Nexus : ADownloadState, IMetaState
public bool IsNSFW { get; set; }
public string? Description { get; set; }
public Uri? LinkUrl => new($"https://www.nexusmods.com/{Game.MetaData().NexusName}/mods/{ModID}");
public async Task<bool> LoadMetaData()
{

View File

@ -1,6 +1,9 @@
using System;
namespace Wabbajack.Lib;
public static class Consts
{
public static string AppName = "Wabbajack";
public static Uri WabbajackBuildServerUri => new("https://build.wabbajack.org");
}

View File

@ -329,4 +329,9 @@ public class Client
if (!result.IsSuccessStatusCode)
throw new HttpException(result);
}
public (IObservable<(Percent PercentDone, string Message)> Progress, Task<Uri> Task) UploadAuthorFile(AbsolutePath pickerTargetPath)
{
throw new NotImplementedException();
}
}