Files
mixtape/zero.Backoffice/Endpoints/UI/UIController.cs
T

119 lines
3.0 KiB
C#
Raw Normal View History

using Microsoft.AspNetCore.Mvc;
2022-01-04 12:58:42 +01:00
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Net.Http.Headers;
using System.Collections;
2022-01-04 12:58:42 +01:00
using System.IO;
using System.Reflection;
2021-12-06 13:29:15 +01:00
using zero.Backoffice.Services;
namespace zero.Backoffice.Endpoints.UI;
2021-12-03 14:45:49 +01:00
public class UIController : ZeroBackofficeController
{
2021-12-06 13:29:15 +01:00
readonly IIconService IconService;
readonly IResourceService ResourceService;
readonly ISectionService SectionService;
readonly IZeroOptions Options;
2022-01-04 12:58:42 +01:00
readonly IMediaManagement Media;
2022-01-04 12:58:42 +01:00
public UIController(IIconService iconService, IResourceService resourceService, ISectionService sectionService, IZeroOptions options, IMediaManagement media)
{
2021-12-06 13:29:15 +01:00
IconService = iconService;
ResourceService = resourceService;
SectionService = sectionService;
Options = options;
2022-01-04 12:58:42 +01:00
Media = media;
}
[HttpGet("sections")]
//[ZeroAuthorize(CountryPermissions.Create)]
2021-12-06 13:29:15 +01:00
public async Task<ActionResult<IEnumerable>> GetSections() => Ok(await SectionService.GetSections());
2021-12-06 13:29:15 +01:00
[HttpGet("settingareas")]
public async Task<ActionResult<IEnumerable>> GetSettingGroups() => Ok(await SectionService.GetSettingsAreas());
[HttpGet("iconsets")]
public async Task<ActionResult<IEnumerable>> GetIconSets()
{
2021-12-06 13:29:15 +01:00
return Ok(await IconService.GetSets());
}
2021-12-06 13:29:15 +01:00
[HttpGet("translations")]
public async Task<ActionResult<Dictionary<string, string>>> GetTranslations()
{
return Ok(await ResourceService.GetTranslations("en-us"));
}
[HttpGet("flavors")]
public ActionResult<Dictionary<string, FlavorProvider>> GetFlavors()
{
Dictionary<string, FlavorProvider> result = new();
foreach ((Type type, FlavorProvider provider) in Options.For<FlavorOptions>().Providers)
{
string key = type.GetCustomAttribute<RavenCollectionAttribute>(true)?.Name ?? type.Name;
result[Safenames.Alias(key)] = provider;
}
return result;
}
2021-12-15 15:34:05 +01:00
[HttpGet("blueprints")]
public ActionResult<IEnumerable<string>> GetBlueprints()
{
HashSet<string> result = new();
foreach (Blueprint blueprint in Options.For<BlueprintOptions>())
{
string key = blueprint.ContentType.GetCustomAttribute<RavenCollectionAttribute>(true)?.Name ?? blueprint.Alias;
result.Add(Safenames.Alias(key));
}
return result;
}
2022-01-04 12:58:42 +01:00
[HttpGet("thumbnail/{id}-{size}.tmp")]
public async Task<IActionResult> GetThumbnail(string id, string size)
{
zero.Media.Media media = await Media.GetFile(id);
if (media == null)
{
return Ok();
}
string path = Media.GetPublicFilePath(media, size);
if (path == null)
{
return Ok();
}
if (path.StartsWith("url://"))
{
path = path.Substring(6);
}
FileExtensionContentTypeProvider provider = new();
string contentType;
if (!provider.TryGetContentType(Path.GetFileName(path), out contentType))
{
contentType = "application/octet-stream";
}
try
{
return File(await Media.GetFileStream(media, size), contentType, DateTimeOffset.Now, EntityTagHeaderValue.Any);
}
catch (FileSystemException)
{
return Ok();
}
}
}