[frontend/pages] Add drive file upload

This commit is contained in:
pancakes 2024-12-16 13:52:02 +10:00 committed by Laura Hausmann
parent afb766f51c
commit 673e5c88bc
No known key found for this signature in database
GPG key ID: D044E84C5BE01605
3 changed files with 39 additions and 3 deletions

View file

@ -11,6 +11,7 @@
@using Microsoft.AspNetCore.Authorization @using Microsoft.AspNetCore.Authorization
@using Iceshrimp.Frontend.Components @using Iceshrimp.Frontend.Components
@inject ApiService Api; @inject ApiService Api;
@inject IJSRuntime Js;
@inject IStringLocalizer<Localization> Loc; @inject IStringLocalizer<Localization> Loc;
@inject ILogger<DrivePage> Logger; @inject ILogger<DrivePage> Logger;
@inject NavigationManager Nav; @inject NavigationManager Nav;
@ -28,8 +29,18 @@
<Icon Name="Icons.Cloud"/> <Icon Name="Icons.Cloud"/>
@Loc["Drive"] @Loc["Drive"]
} }
@if (_state == State.Loaded && Folder is not null)
{
<span class="action btn" @onclick="OpenUpload" title="@Loc["Upload file"]">
<Icon Name="Icons.UploadSimple"/>
</span>
}
</SectionContent> </SectionContent>
<div class="file-input">
<InputFile @ref="UploadInput" OnChange="Upload">Upload!</InputFile>
</div>
@if (_state == State.Loaded && Folder != null) @if (_state == State.Loaded && Folder != null)
{ {
<ol class="drive-files"> <ol class="drive-files">
@ -61,9 +72,11 @@
} }
@code { @code {
[Parameter] public string? Id { get; set; } [Parameter] public string? Id { get; set; }
private DriveFolderResponse? Folder { get; set; } = null; private DriveFolderResponse? Folder { get; set; } = null;
private State _state { get; set; } private State _state { get; set; }
private InputFile UploadInput { get; set; } = null!;
private IJSObjectReference _module = null!;
private async Task Load() private async Task Load()
{ {
@ -93,6 +106,8 @@
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await Load(); await Load();
_module = await Js.InvokeAsync<IJSObjectReference>("import",
"./Pages/DrivePage.razor.js");
} }
protected override async Task OnParametersSetAsync() protected override async Task OnParametersSetAsync()
@ -104,4 +119,18 @@
{ {
Nav.NavigateTo(Folder?.ParentId != null ? $"/drive/{Folder.ParentId}" : "/drive"); Nav.NavigateTo(Folder?.ParentId != null ? $"/drive/{Folder.ParentId}" : "/drive");
} }
// The <InputFile> Component is hidden, and triggered by a sepperate button.
// That way we get it's functionality, without the styling limitations of the InputFile component
private async Task OpenUpload()
{
await _module.InvokeVoidAsync("openUpload", UploadInput.Element);
}
private async Task Upload(InputFileChangeEventArgs e)
{
var res = await Api.Drive.UploadFileAsync(e.File);
Folder!.Files.Add(res);
StateHasChanged();
}
} }

View file

@ -1,3 +1,7 @@
.file-input {
display: none;
}
.drive-files { .drive-files {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;

View file

@ -0,0 +1,3 @@
export function openUpload(element) {
element.click();
}