113 lines
No EOL
3.2 KiB
Text
113 lines
No EOL
3.2 KiB
Text
@using Microsoft.Extensions.Options
|
|
@using LANCommander.SDK.Enums
|
|
@inherits OwningComponentBase
|
|
@inject UserService UserService
|
|
@inject MediaService MediaService
|
|
@inject IOptions<Settings.Settings> Settings
|
|
@inject IMessageService MessageService
|
|
|
|
<InputFile id="@("AvatarUpload")" OnChange="(e) => UploadAvatar(e)" hidden accept=".jpg,jpeg,.png" />
|
|
|
|
<label for="AvatarUpload" style="cursor: pointer;">
|
|
@if (String.IsNullOrWhiteSpace(AvatarUrl))
|
|
{
|
|
<div class="ant-upload ant-upload-select-picture-card ant-upload-select">
|
|
<span class="ant-upload" tabindex="0" style="display: grid;">
|
|
<div>
|
|
<Icon Type="@IconType.Outline.Plus" />
|
|
<div class="ant-upload-text">Upload</div>
|
|
</div>
|
|
</span>
|
|
</div>
|
|
}
|
|
else
|
|
{
|
|
<Image Width="@Width" Src="@AvatarUrl" Preview="false" />
|
|
}
|
|
</label>
|
|
|
|
@code {
|
|
[Parameter] public Guid UserId { get; set; }
|
|
[Parameter] public string Width { get; set; } = "104px";
|
|
|
|
string AvatarUrl;
|
|
|
|
User User = new User();
|
|
ICollection<Media> Media = new List<Media>();
|
|
|
|
string[] ValidMimeTypes = new string[]
|
|
{
|
|
"image/png",
|
|
"image/jpeg"
|
|
};
|
|
|
|
protected override async Task OnParametersSetAsync()
|
|
{
|
|
await LoadUser();
|
|
}
|
|
|
|
protected async Task LoadUser()
|
|
{
|
|
User = await UserService.Include(u => u.Media!).GetAsync(UserId);
|
|
|
|
await LoadAvatar();
|
|
}
|
|
|
|
async Task LoadAvatar()
|
|
{
|
|
var avatar = await MediaService.FirstOrDefaultAsync(m => m.Type == SDK.Enums.MediaType.Avatar && m.UserId == UserId);
|
|
|
|
if (avatar != null)
|
|
AvatarUrl = $"/api/Media/{avatar.Id}/Download?fileId={avatar.FileId}";
|
|
else
|
|
AvatarUrl = String.Empty;
|
|
}
|
|
|
|
async Task UploadAvatar(InputFileChangeEventArgs e)
|
|
{
|
|
var media = User.Media?.FirstOrDefault(m => m.Type == SDK.Enums.MediaType.Avatar);
|
|
var config = Settings.Value.Server.Media.GetMediaTypeConfig(MediaType.Avatar);
|
|
|
|
if (media == null)
|
|
media = new Media
|
|
{
|
|
Type = SDK.Enums.MediaType.Avatar,
|
|
UserId = User.Id
|
|
};
|
|
|
|
if (!ValidMimeTypes.Contains(e.File.ContentType))
|
|
{
|
|
MessageService.Error("Unsupported file type");
|
|
return;
|
|
}
|
|
|
|
if (e.File.Size > config.MaxFileSize)
|
|
{
|
|
MessageService.Error($"File size must be smaller than {ByteSizeLib.ByteSize.FromBytes(config.MaxFileSize)}");
|
|
return;
|
|
}
|
|
|
|
media.SourceUrl = "";
|
|
media.MimeType = e.File.ContentType;
|
|
|
|
if (media.Id == Guid.Empty)
|
|
{
|
|
media = await MediaService.WriteToFileAsync(media, e.File.OpenReadStream(maxAllowedSize: config.MaxFileSize));
|
|
}
|
|
else if (media.Id != Guid.Empty)
|
|
{
|
|
MediaService.DeleteLocalMediaFile(media);
|
|
|
|
media = await MediaService.WriteToFileAsync(media, e.File.OpenReadStream(maxAllowedSize: config.MaxFileSize));
|
|
|
|
await MediaService.UpdateAsync(media);
|
|
}
|
|
|
|
}
|
|
|
|
async Task OnAvatarUploaded()
|
|
{
|
|
// reload full user in order to get related media
|
|
await LoadUser();
|
|
}
|
|
} |