@using System.Net.Mime @inject MediaService MediaService @inject ModalService ModalService @inject IMessageService MessageService @inject NavigationManager NavigationManager @inject ILogger Logger @if (MediaService.FileExists(context)) { if (context.Thumbnail != null && context.Type == MediaType.Manual) { } else if (context.Thumbnail != null) { } else if (context.Type != MediaType.Manual) { } } } @{ var fileInputId = Guid.NewGuid(); }
@code { [Parameter] public ICollection Values { get; set; } = new List(); [Parameter] public EventCallback> ValuesChanged { get; set; } [Parameter] public Guid GameId { get; set; } [Parameter] public string GameTitle { get; set; } Settings Settings = SettingService.GetSettings(); MediaType[] AllowedMediaTypes = new MediaType[] { MediaType.Icon, MediaType.Cover, MediaType.Background, MediaType.Logo, MediaType.Manual }; MediaType[] SearchableMediaTypes = new MediaType[] { MediaType.Icon, MediaType.Cover, MediaType.Background, MediaType.Logo, MediaType.Manual }; string[] ValidMimeTypes = new string[] { MediaTypeNames.Image.Png, MediaTypeNames.Image.Jpeg, MediaTypeNames.Image.Gif, MediaTypeNames.Application.Pdf, MediaTypeNames.Image.Icon }; private async Task Update(Media media) { try { if (media.Id != Guid.Empty) { await MediaService.UpdateAsync(media); MessageService.Success($"{media.Type} updated!"); } } catch (Exception ex) { MessageService.Error($"{media.Type} could not be updated."); Logger.LogError(ex, $"{media.Type} could not be updated."); } } private async Task AddMedia() { if (Values == null) Values = new List(); try { var types = Enum.GetValues(); var nextType = types.FirstOrDefault(t => !Values.Any(v => v.Type == t) && AllowedMediaTypes.Contains(t)); var defaultStorageLocation = await MediaService.GetDefaultStorageLocationAsync(); var media = new Media { GameId = GameId, Type = nextType == MediaType.Icon && Values.Any(v => v.Type == MediaType.Icon) ? MediaType.Manual : nextType, StorageLocationId = defaultStorageLocation.Id, Crc32 = String.Empty }; media = await MediaService.AddAsync(media); Values.Add(await MediaService.GetAsync(media.Id)); } catch (Exception ex) { MessageService.Error("Could not add new media!"); Logger.LogError(ex, "Could not add new media!"); } } private async Task SearchMedia(Media media) { var modalOptions = new ModalOptions() { Title = $"Download {media.Type}", Maximizable = false, DefaultMaximized = true, Closable = true, OkText = "Select", }; var grabberOptions = new MediaGrabberOptions() { Type = media.Type, Search = GameTitle }; var modalRef = await ModalService.CreateModalAsync(modalOptions, grabberOptions); modalRef.OnOk = async (result) => { modalRef.Config.ConfirmLoading = true; media.SourceUrl = result.SourceUrl; media.MimeType = result.MimeType; if (media.Id == Guid.Empty) { media = await MediaService.DownloadMediaAsync(result.SourceUrl, media); await MediaService.AddAsync(media); } else { MediaService.DeleteLocalMediaFile(media); media = await MediaService.DownloadMediaAsync(result.SourceUrl, media); } Values = await MediaService.GetAsync(m => m.GameId == media.GameId); if (ValuesChanged.HasDelegate) await ValuesChanged.InvokeAsync(Values); }; } private async Task UploadMedia(InputFileChangeEventArgs e, Media media) { if (!ValidMimeTypes.Contains(e.File.ContentType)) { MessageService.Error("Unsupported file type"); Logger.LogError($"Unsupported file type {e.File.ContentType}"); 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 = 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); } Values = await MediaService.GetAsync(m => m.GameId == media.GameId); if (ValuesChanged.HasDelegate) await ValuesChanged.InvokeAsync(Values); } private async Task RemoveMedia(Media media) { Values.Remove(media); if (media.Id != Guid.Empty) await MediaService.DeleteAsync(media); } private async Task OpenManual(Media media) { var modalOptions = new ModalOptions() { Title = media.Name, Maximizable = true, DefaultMaximized = true, Closable = true, Footer = null, Draggable = true, Resizable = true, WrapClassName = "pdf-reader-dialog", }; var modalRef = await ModalService.CreateModalAsync(modalOptions, media); } private void NavigateToSettings() { NavigationManager.NavigateTo("/Settings/Media", true); } }