tesses-backup/TessesDedupWeb/Pages/Index.razor

176 lines
5.1 KiB
Plaintext
Raw Normal View History

2024-07-23 03:49:40 +00:00
@page "/"
@inject DedupClient Client;
@inject Blazored.LocalStorage.ILocalStorageService localStorage;
@inject IJSRuntime jsRt;
<div id="sb_error" class="snackbar error">@Error</div>
@if(Ready)
{
@if(LoggedIn)
{
<h2>Backups: @Stats.Backups</h2>
<h2>Blocks: @Stats.Blocks</h2>
<h2>Size: @Stats.Label</h2>
<br>
<details>
<summary class="none">
<article class="round primary no-elevate">
<nav>
<div class="max">Mount backups (Read only, Linux)</div>
<i>expand_more</i>
</nav>
</article>
</summary>
<article class="round border">
<h2>To Install</h2>
<code>
$ sudo apt install httpdirfs
</code>
</article>
<article class="round border">
<h2>To Mount</h2>
<code>
$ mkdir "~/BackupsMount"<br>
$ httpdirfs -u "\$access_key" -p "@_AccessKey" "@Client.DataPath" "~/BackupsMount"
</code>
</article>
<article class="round border">
<h2>To Unmount</h2>
<code>
$ umount "~/BackupsMount"
</code>
</article>
<article class="round border">
<h2>To Unmount (if busy and you are impatient)</h2>
<code>
$ umount -l "~/BackupsMount"
</code>
</article>
</details>
}
else
{
@if(Registered)
{
<h1>Login</h1>
<div class="field label border">
<input type="text" @bind-value="Username">
<label>Username</label>
</div>
<div class="field label border">
<input type="password" @bind-value="Password">
<label>Password</label>
</div>
<div class="field label border">
<input type="text" @bind-value="DeviceName">
<label>Device Name</label>
</div>
<button @onclick="LoginAsync">Login</button>
}
else
{
<h1>Register</h1>
<div class="field label border">
<input type="text" @bind-value="Username">
<label>Username</label>
</div>
<div class="field label border">
<input type="password" @bind-value="Password">
<label>Password</label>
</div>
<div class="field label border">
<input type="password" @bind-value="ConfirmPassword">
<label>Confirm Password</label>
</div>
<div class="field label border">
<input type="text" @bind-value="DeviceName">
<label>Device Name</label>
</div>
<button @onclick="RegisterAsync">Register</button>
}
}
}
@code {
public async Task SplashErrorAsync(string error)
{
Error = error;
await jsRt.InvokeVoidAsync("ui","#sb_error");
}
public Stats Stats {get;set;}=new Stats();
public bool Ready {get;set;}=false;
public bool Registered {get;set;}=false;
public bool LoggedIn {get;set;}=false;
public string Username {get;set;}="";
public string Password {get;set;}="";
public string ConfirmPassword {get;set;}="";
public string Error {get;set;}="";
public string DeviceName {get;set;}="Browser";
public string _AccessKey {get;set;}="";
protected override async Task OnInitializedAsync()
{
LoggedIn = await localStorage.ContainKeyAsync("token");
if(LoggedIn)
{
var token=await localStorage.GetItemAsStringAsync("token");
try{
_AccessKey = token ?? "";
Stats = await Client.GetStatsAsync(_AccessKey);
}catch(System.Net.Http.HttpRequestException ex)
{
_=ex;
LoggedIn=false;
}
}
Registered = await Client.IsRegisteredAsync();
Ready = true;
}
public async Task LoginAsync()
{
var res=await Client.LoginAsync(Username,Password,DeviceName);
if(res.Success)
{
await localStorage.SetItemAsStringAsync("token",res.Key);
Registered=true;
LoggedIn=true;
_AccessKey = res.Key;
Stats = await Client.GetStatsAsync(res.Key);
}
else
{
await SplashErrorAsync("Incorrect username or password");
}
}
public async Task RegisterAsync()
{
if(Password != ConfirmPassword)
{
await SplashErrorAsync("Passwords do not match");
return;
}
var res=await Client.LoginAsync(Username,Password,DeviceName);
if(res.Success)
{
await localStorage.SetItemAsStringAsync("token",res.Key);
Stats = await Client.GetStatsAsync(res.Key);
Registered=true;
LoggedIn=true;
}
else
{
await SplashErrorAsync("Incorrect username or password");
}
}
}