101 lines
No EOL
2.9 KiB
Text
101 lines
No EOL
2.9 KiB
Text
@inherits OwningComponentBase
|
|
@inject UserService UserService
|
|
@inject MediaService MediaService
|
|
@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";
|
|
|
|
Settings Settings = SettingService.GetSettings();
|
|
|
|
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()
|
|
{
|
|
User = await UserService.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);
|
|
|
|
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 / 1024) > 512)
|
|
{
|
|
MessageService.Error($"File size must be smaller than 512KB");
|
|
return;
|
|
}
|
|
|
|
media.SourceUrl = "";
|
|
media.MimeType = e.File.ContentType;
|
|
|
|
if (media.Id == Guid.Empty)
|
|
{
|
|
media = await MediaService.UploadMediaAsync(e.File.OpenReadStream(maxAllowedSize: Settings.Media.MaxSize * 1024 * 1024), media);
|
|
|
|
await MediaService.AddAsync(media);
|
|
}
|
|
else
|
|
{
|
|
MediaService.DeleteLocalMediaFile(media);
|
|
media = await MediaService.UploadMediaAsync(e.File.OpenReadStream(maxAllowedSize: Settings.Media.MaxSize * 1024 * 1024), media);
|
|
await MediaService.UpdateAsync(media);
|
|
}
|
|
|
|
await LoadAvatar();
|
|
}
|
|
} |