Add manual upload button for media

media
Pat Hartl 2023-11-02 23:18:19 -05:00
parent 0fc8d756b3
commit 82886221fc
5 changed files with 82 additions and 0 deletions

View File

@ -48,5 +48,11 @@ namespace LANCommander.Controllers.Api
}
}
}
[HttpPost("Media")]
public async Task Media(IFormFile file)
{
}
}
}

View File

@ -56,6 +56,7 @@
{
public string SteamGridDbApiKey { get; set; } = "";
public string StoragePath { get; set; } = "Media";
public long MaxSize { get; set; } = 25;
}
public class LANCommanderIPXRelaySettings

View File

@ -2,6 +2,7 @@
@using LANCommander.Models;
@inject MediaService MediaService
@inject ModalService ModalService
@inject IMessageService MessageService
<Space Direction="DirectionVHType.Vertical" Size="@("large")" Style="width: 100%" Class="media-editor">
<SpaceItem>
@ -17,6 +18,15 @@
</PropertyColumn>
<ActionColumn>
<Space Style="display: flex; justify-content: end">
<SpaceItem>
@{
var fileInputId = Guid.NewGuid();
}
<InputFile id="@fileInputId" OnChange="(e) => UploadMedia(e, context)" hidden />
<label class="ant-btn ant-btn-text ant-btn-icon-only" for="@fileInputId">
<Icon Type="@IconType.Outline.Upload" />
</label>
</SpaceItem>
<SpaceItem>
<Button OnClick="() => SearchMedia(context)" Type="@ButtonType.Text" Icon="@IconType.Outline.Search" />
</SpaceItem>
@ -46,6 +56,14 @@
[Parameter] public Guid GameId { get; set; }
[Parameter] public string GameTitle { get; set; }
LANCommanderSettings Settings = SettingService.GetSettings();
string[] ValidMimeTypes = new string[]
{
"image/png",
"image/jpeg"
};
private async Task AddMedia()
{
if (Values == null)
@ -104,6 +122,42 @@
};
}
private async Task UploadMedia(InputFileChangeEventArgs e, Media media)
{
if (!ValidMimeTypes.Contains(e.File.ContentType))
{
MessageService.Error("Unsupported file type");
return;
}
if ((e.File.Size / 1024 / 1024) > Settings.Media.MaxSize)
{
MessageService.Error($"File size must be smaller than {Settings.Media.MaxSize}MB");
return;
}
media.SourceUrl = "";
media.MimeType = e.File.ContentType;
if (media.Id == Guid.Empty)
{
media.FileId = await MediaService.UploadMediaAsync(e.File);
await MediaService.Add(media);
}
else
{
MediaService.DeleteLocalMediaFile(media.FileId);
media.FileId = await MediaService.UploadMediaAsync(e.File);
await MediaService.Update(media);
}
Values = MediaService.Get(m => m.GameId == media.GameId).ToList();
if (ValuesChanged.HasDelegate)
await ValuesChanged.InvokeAsync(Values);
}
private async Task RemoveMedia(Media media)
{
Values.Remove(media);

View File

@ -2,6 +2,7 @@
using LANCommander.Data.Models;
using LANCommander.Helpers;
using LANCommander.Models;
using Microsoft.AspNetCore.Components.Forms;
namespace LANCommander.Services
{
@ -47,6 +48,20 @@ namespace LANCommander.Services
return Path.Combine(Settings.Media.StoragePath, entity.FileId.ToString());
}
public async Task<Guid> UploadMediaAsync(IBrowserFile file)
{
var fileId = Guid.NewGuid();
var path = Path.Combine(Settings.Media.StoragePath, fileId.ToString());
using (var fs = new FileStream(path, FileMode.Create))
{
await file.OpenReadStream().CopyToAsync(fs);
}
return fileId;
}
public void DeleteLocalMediaFile(Guid fileId)
{
var path = Path.Combine(Settings.Media.StoragePath, fileId.ToString());

View File

@ -18,6 +18,12 @@
background: #141414;
}
label.ant-btn-icon-only {
display: inline-flex;
align-items: center;
justify-content: center;
}
.ant-select-selector {
line-height: 30px;
}