Browse Source

Admin: Use `IRemoteStreamContent` for event cover image

pull/62/head
Engincan VESKE 5 years ago
parent
commit
4545b43631
  1. 2
      src/EventHub.Admin.Application.Contracts/Events/EventDetailDto.cs
  2. 3
      src/EventHub.Admin.Application.Contracts/Events/IEventAppService.cs
  3. 3
      src/EventHub.Admin.Application.Contracts/Events/UpdateEventDto.cs
  4. 3
      src/EventHub.Admin.Application/EventHubApplicationAutoMapperProfile.cs
  5. 23
      src/EventHub.Admin.Application/Events/EventAppService.cs
  6. 32
      src/EventHub.Admin.HttpApi.Host/Controllers/Events/EventController.cs
  7. 2
      src/EventHub.Admin.HttpApi.Host/EventHubAdminHttpApiHostModule.cs
  8. BIN
      src/EventHub.Admin.HttpApi.Host/Images/eh-event.png
  9. 3
      src/EventHub.Admin.Web/Pages/EventManagement.razor
  10. 32
      src/EventHub.Admin.Web/Pages/EventManagement.razor.cs

2
src/EventHub.Admin.Application.Contracts/Events/EventDetailDto.cs

@ -13,8 +13,6 @@ namespace EventHub.Admin.Events
public DateTime EndTime { get; set; } public DateTime EndTime { get; set; }
public byte[] CoverImageContent { get; set; }
public bool IsOnline { get; set; } public bool IsOnline { get; set; }
public string OnlineLink { get; set; } public string OnlineLink { get; set; }

3
src/EventHub.Admin.Application.Contracts/Events/IEventAppService.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.Content;
namespace EventHub.Admin.Events namespace EventHub.Admin.Events
{ {
@ -16,6 +17,6 @@ namespace EventHub.Admin.Events
Task<List<CountryLookupDto>> GetCountriesLookupAsync(); Task<List<CountryLookupDto>> GetCountriesLookupAsync();
Task<byte[]> GetCoverImageAsync(Guid id); Task<IRemoteStreamContent> GetCoverImageAsync(Guid id);
} }
} }

3
src/EventHub.Admin.Application.Contracts/Events/UpdateEventDto.cs

@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using EventHub.Events; using EventHub.Events;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp.Content;
namespace EventHub.Admin.Events namespace EventHub.Admin.Events
{ {
@ -24,7 +25,7 @@ namespace EventHub.Admin.Events
public DateTime EndTime { get; set; } public DateTime EndTime { get; set; }
[CanBeNull] [CanBeNull]
public byte[] CoverImageContent { get; set; } public RemoteStreamContent CoverImageStreamContent { get; set; }
public bool IsOnline { get; set; } public bool IsOnline { get; set; }

3
src/EventHub.Admin.Application/EventHubApplicationAutoMapperProfile.cs

@ -26,8 +26,7 @@ namespace EventHub.Admin
CreateMap<OrganizationMemberWithDetails, OrganizationMemberDto>(); CreateMap<OrganizationMemberWithDetails, OrganizationMemberDto>();
CreateMap<Event, EventDetailDto>() CreateMap<Event, EventDetailDto>();
.Ignore(x => x.CoverImageContent);
CreateMap<Country, CountryLookupDto>(); CreateMap<Country, CountryLookupDto>();

23
src/EventHub.Admin.Application/Events/EventAppService.cs

@ -9,6 +9,7 @@ using EventHub.Events;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.BlobStoring; using Volo.Abp.BlobStoring;
using Volo.Abp.Content;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
namespace EventHub.Admin.Events namespace EventHub.Admin.Events
@ -36,9 +37,7 @@ namespace EventHub.Admin.Events
public async Task<EventDetailDto> GetAsync(Guid id) public async Task<EventDetailDto> GetAsync(Guid id)
{ {
var @event = await _eventRepository.GetAsync(id); var @event = await _eventRepository.GetAsync(id);
var eventDetailDto = ObjectMapper.Map<Event, EventDetailDto>(@event); var eventDetailDto = ObjectMapper.Map<Event, EventDetailDto>(@event);
eventDetailDto.CoverImageContent = await GetCoverImageAsync(id);
return eventDetailDto; return eventDetailDto;
} }
@ -69,16 +68,26 @@ namespace EventHub.Admin.Events
@event.SetTime(input.StartTime, @event.EndTime); @event.SetTime(input.StartTime, @event.EndTime);
await _eventManager.SetCapacityAsync(@event, input.Capacity); await _eventManager.SetCapacityAsync(@event, input.Capacity);
await SetCoverImageAsync(blobName: id.ToString(), input.CoverImageContent); if (input.CoverImageStreamContent != null && input.CoverImageStreamContent.ContentLength > 0)
{
await SetCoverImageAsync(blobName: id.ToString(), input.CoverImageStreamContent);
}
await _eventRepository.UpdateAsync(@event); await _eventRepository.UpdateAsync(@event);
} }
public async Task<byte[]> GetCoverImageAsync(Guid id) [AllowAnonymous]
public async Task<IRemoteStreamContent> GetCoverImageAsync(Guid id)
{ {
var blobName = id.ToString(); var blobName = id.ToString();
var coverImageStream = await _eventBlobContainer.GetOrNullAsync(blobName);
if (coverImageStream == null)
{
return null;
}
return await _eventBlobContainer.GetAllBytesOrNullAsync(blobName); return new RemoteStreamContent(coverImageStream);
} }
public async Task<List<CountryLookupDto>> GetCountriesLookupAsync() public async Task<List<CountryLookupDto>> GetCountriesLookupAsync()
@ -94,9 +103,9 @@ namespace EventHub.Admin.Events
return ObjectMapper.Map<List<Country>, List<CountryLookupDto>>(countries); return ObjectMapper.Map<List<Country>, List<CountryLookupDto>>(countries);
} }
private async Task SetCoverImageAsync(string blobName, byte[] coverImageContent, bool overrideExisting = true) private async Task SetCoverImageAsync(string blobName, IRemoteStreamContent streamContent, bool overrideExisting = true)
{ {
await _eventBlobContainer.SaveAsync(blobName, coverImageContent, overrideExisting); await _eventBlobContainer.SaveAsync(blobName, streamContent.GetStream(), overrideExisting);
} }
} }
} }

32
src/EventHub.Admin.HttpApi.Host/Controllers/Events/EventController.cs

@ -2,10 +2,13 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using EventHub.Admin.Events; using EventHub.Admin.Events;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Dtos;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Content;
using Volo.Abp.VirtualFileSystem;
namespace EventHub.Admin.Controllers.Events namespace EventHub.Admin.Controllers.Events
{ {
@ -17,10 +20,12 @@ namespace EventHub.Admin.Controllers.Events
public class EventController : AbpController, IEventAppService public class EventController : AbpController, IEventAppService
{ {
private readonly IEventAppService _eventAppService; private readonly IEventAppService _eventAppService;
private readonly IVirtualFileProvider _virtualFileProvider;
public EventController(IEventAppService eventAppService)
public EventController(IEventAppService eventAppService, IVirtualFileProvider virtualFileProvider)
{ {
_eventAppService = eventAppService; _eventAppService = eventAppService;
_virtualFileProvider = virtualFileProvider;
} }
[HttpGet("{id}")] [HttpGet("{id}")]
@ -36,9 +41,28 @@ namespace EventHub.Admin.Controllers.Events
} }
[HttpGet("cover-image/{id}")] [HttpGet("cover-image/{id}")]
public Task<byte[]> GetCoverImageAsync(Guid id) [AllowAnonymous]
public async Task<IRemoteStreamContent> GetCoverImageAsync(Guid id)
{ {
return _eventAppService.GetCoverImageAsync(id); var remoteStreamContent = await _eventAppService.GetCoverImageAsync(id);
if (remoteStreamContent is null)
{
var stream = _virtualFileProvider
.GetFileInfo("/Images/eh-event.png")
.CreateReadStream();
remoteStreamContent = new RemoteStreamContent(stream)
{
ContentType = "image/png"
};
await stream.FlushAsync();
}
Response.Headers.Add("Accept-Ranges", "bytes");
Response.ContentType = remoteStreamContent.ContentType;
return remoteStreamContent;
} }
[HttpGet] [HttpGet]

2
src/EventHub.Admin.HttpApi.Host/EventHubAdminHttpApiHostModule.cs

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using EventHub.Admin.Events;
using EventHub.Admin.Organizations; using EventHub.Admin.Organizations;
using EventHub.Admin.Utils; using EventHub.Admin.Utils;
using EventHub.EntityFrameworkCore; using EventHub.EntityFrameworkCore;
@ -69,6 +70,7 @@ namespace EventHub.Admin
Configure<AbpAspNetCoreMvcOptions>(options => Configure<AbpAspNetCoreMvcOptions>(options =>
{ {
options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(UpdateOrganizationDto)); options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(UpdateOrganizationDto));
options.ConventionalControllers.FormBodyBindingIgnoredTypes.Add(typeof(UpdateEventDto));
}); });
} }

BIN
src/EventHub.Admin.HttpApi.Host/Images/eh-event.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 184 KiB

3
src/EventHub.Admin.Web/Pages/EventManagement.razor

@ -3,11 +3,14 @@
@using EventHub.Admin.Permissions @using EventHub.Admin.Permissions
@using EventHub.Admin.Events @using EventHub.Admin.Events
@using EventHub.Events @using EventHub.Events
@using EventHub.Web
@using Microsoft.Extensions.Options
@using Volo.Abp.AspNetCore.Components.Notifications @using Volo.Abp.AspNetCore.Components.Notifications
@inherits EventHubComponentBase @inherits EventHubComponentBase
@attribute [Authorize(EventHubPermissions.Events.Default)] @attribute [Authorize(EventHubPermissions.Events.Default)]
@inject IEventAppService EventAppService @inject IEventAppService EventAppService
@inject NavigationManager NavigationManager @inject NavigationManager NavigationManager
@inject IOptions<EventHubUrlOptions> UrlOptions
<Card> <Card>
<CardHeader> <CardHeader>

32
src/EventHub.Admin.Web/Pages/EventManagement.razor.cs

@ -12,6 +12,7 @@ using System.IO;
using System.Globalization; using System.Globalization;
using NUglify.Helpers; using NUglify.Helpers;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Content;
namespace EventHub.Admin.Web.Pages namespace EventHub.Admin.Web.Pages
{ {
@ -92,9 +93,10 @@ namespace EventHub.Admin.Web.Pages
{ {
EditingEventId = input.Id; EditingEventId = input.Id;
Event = await EventAppService.GetAsync(EditingEventId); Event = await EventAppService.GetAsync(EditingEventId);
EditingEvent = ObjectMapper.Map<EventDetailDto, UpdateEventDto>(Event); EditingEvent = ObjectMapper.Map<EventDetailDto, UpdateEventDto>(Event);
FillCoverImageUrl(EditingEvent.CoverImageContent);
FileEntry = new FileEntry();
CoverImageUrl = UrlOptions.Value.AdminApi.EnsureEndsWith('/') + "api/eventhub/admin/event/cover-image/" + EditingEventId;
EditEventModal.Show(); EditEventModal.Show();
} }
@ -143,16 +145,15 @@ namespace EventHub.Admin.Web.Pages
await GetEventsAsync(); await GetEventsAsync();
} }
private void FillCoverImageUrl(byte[] content) private void SetCoverImageUrl(string contentType, byte[] content)
{ {
if (content.IsNullOrEmpty()) if (content.IsNullOrEmpty())
{ {
return; return;
} }
var imageBase64Data = Convert.ToBase64String(content); contentType = string.IsNullOrWhiteSpace(contentType) ? "image/png" : contentType;
var imageDataUrl = $"data:image/png;base64,{imageBase64Data}"; CoverImageUrl = $"data:{contentType};base64,{Convert.ToBase64String(content)}";
CoverImageUrl = imageDataUrl;
} }
private async Task OnCoverImageFileChanged(FileChangedEventArgs e) private async Task OnCoverImageFileChanged(FileChangedEventArgs e)
@ -163,20 +164,23 @@ namespace EventHub.Admin.Web.Pages
return; return;
} }
using (var stream = new MemoryStream()) var stream = new MemoryStream();
await FileEntry.WriteToStreamAsync(stream);
stream.Seek(0, SeekOrigin.Begin);
EditingEvent.CoverImageStreamContent = new RemoteStreamContent(stream)
{ {
await FileEntry.WriteToStreamAsync(stream); ContentType = FileEntry.Type,
FileName = FileEntry.Name
};
stream.Seek(0, SeekOrigin.Begin); SetCoverImageUrl(FileEntry.Type, stream.ToArray());
EditingEvent.CoverImageContent = stream.ToArray(); await InvokeAsync(StateHasChanged);
FillCoverImageUrl(EditingEvent.CoverImageContent);
await InvokeAsync(StateHasChanged);
}
} }
private void OnDeleteCoverImageButtonClicked() private void OnDeleteCoverImageButtonClicked()
{ {
EditingEvent.CoverImageContent = null; EditingEvent.CoverImageStreamContent = null;
FileEntry = new FileEntry(); FileEntry = new FileEntry();
CoverImageUrl = null; CoverImageUrl = null;
} }

Loading…
Cancel
Save