tesses-backup/TessesDedupWeb/Pages/BackupView.razor

145 lines
4.6 KiB
Plaintext

@page "/backups/{id:long}"
@inject NavigationManager NavManager;
@inject DedupClient Client;
@inject Blazored.LocalStorage.ILocalStorageService localStorage;
@if(IsReady)
{
if(IsDir)
{
if(ParentPath == "/backups")
{
<a href="/backups">Up</a> <span>@Path</span>
}else{
<a @onclick="()=>Navmgr(ParentPath)">Up</a> <span>@Path</span>
}
<hr>
foreach(var item in Entries)
{
if(item.PointsTo.StartsWith("/api/v1/Download"))
{
<a class="row padding surface-container" target="_blank" href="@item.PointsTo">
<i>@item.Type</i>
<div class="max">
<h6 class="small">@item.Name</h6>
</div>
<label></label>
</a>
}else{
<a class="row padding surface-container" @onclick="()=>Navmgr(item.PointsTo)">
<i>@item.Type</i>
<div class="max">
<h6 class="small">@item.Name</h6>
</div>
<label></label>
</a>
}
}
}
}
@code {
[Parameter]
public long Id {get;set;}
public string Path {get;set;}="";
public bool IsReady {get;set;}=false;
public bool IsDir {get;set;}=false;
public string Url {get;set;}="";
public string ParentPath {get;set;}="";
public List<FilesystemEntry> Entries {get;set;}=new List<FilesystemEntry>();
string token="";
public void HandlePage(string path)
{
if(!string.IsNullOrWhiteSpace(path))
{
UnixPath unixPath = path;
var p=bkp.GetEntryFromPath(unixPath);
if(unixPath.Path == "/" || unixPath.Path.Length == 0)
ParentPath="/backups";
else
ParentPath = $"/backups/{Id}?path={WebUtility.UrlEncode(unixPath.Parent.Path)}";
if(p.Type == "dir")
{
IsDir=true;
foreach(var item in p.Entries)
{
FilesystemEntry ent = new FilesystemEntry();
ent.Name = item.Name;
if(item.Type == "dir")
{
ent.PointsTo = $"/backups/{Id}?path={WebUtility.UrlEncode((unixPath/item.Name).Path)}";
ent.Type = "folder";
}
else if(item.Type == "file")
{
ent.PointsTo = $"/api/v1/Download?access_key={WebUtility.UrlEncode(token)}&id={Id}&path={WebUtility.UrlEncode((unixPath/item.Name).Path)}";
ent.Type = "draft";
}
else
{
var follow = bkp.GetEntryFromPath(unixPath/item.Name);
if(follow.Type == "dir")
{
ent.PointsTo = $"/backups/{Id}?path={WebUtility.UrlEncode((unixPath/item.Name).Path)}";
}
else
{
ent.PointsTo = $"/api/v1/Download?access_key={WebUtility.UrlEncode(token)}&id={Id}&path={WebUtility.UrlEncode((unixPath/item.Name).Path)}";
}
ent.Type = "link";
}
Entries.Add(ent);
}
}
IsReady=true;
}
}
Backup bkp=new Backup();
private void Navmgr(string _url)
{
Entries.Clear();
IsReady=false;
NavManager.NavigateTo(_url);
var url=NavManager.ToAbsoluteUri(_url);
if(QueryHelpers.ParseQuery(url.Query).TryGetValue("path",out var value))
{
string? path = value;
if(!string.IsNullOrWhiteSpace(path))
{
HandlePage(path);
}
}
StateHasChanged();
}
protected override async Task OnInitializedAsync()
{
var _token = await localStorage.GetItemAsStringAsync("token");
if(!string.IsNullOrWhiteSpace(_token))
{
token = _token;
bkp = await Client.GetBackupAsync(token,Id);
Navmgr(NavManager.Uri);
}
}
}