515 lines
22 KiB
C#
515 lines
22 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading.Tasks;
|
|
using Manager.Interfaces.DTO;
|
|
using Manager.Interfaces.Models;
|
|
using Manager.Services;
|
|
using ManagerService.Helpers;
|
|
using ManagerService.Service.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Logging;
|
|
using Newtonsoft.Json;
|
|
using NSwag.Annotations;
|
|
|
|
namespace ManagerService.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[ApiController, Route("api/[controller]")]
|
|
[OpenApiTag("Resource", Description = "Resource management")]
|
|
public class ResourceController : ControllerBase
|
|
{
|
|
private ResourceDatabaseService _resourceService;
|
|
private ResourceDataDatabaseService _resourceDataService;
|
|
private SectionDatabaseService _sectionService;
|
|
private ConfigurationDatabaseService _configurationService;
|
|
private readonly ILogger<ResourceController> _logger;
|
|
|
|
private static int MaxWidth = 1024;
|
|
private static int MaxHeight = 1024;
|
|
|
|
public ResourceController(ILogger<ResourceController> logger, ResourceDatabaseService resourceService, ResourceDataDatabaseService resourceDataService, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
|
|
{
|
|
_logger = logger;
|
|
_resourceService = resourceService;
|
|
_resourceDataService = resourceDataService;
|
|
_sectionService = sectionService;
|
|
_configurationService = configurationService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all resources (summary)
|
|
/// </summary>
|
|
/// <param name="id">id instance</param>
|
|
/// <param name="types">types of resource</param>
|
|
[ProducesResponseType(typeof(List<ResourceDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get([FromQuery] string instanceId, [FromQuery] List<ResourceType> types)
|
|
{
|
|
try
|
|
{
|
|
if (instanceId == null)
|
|
throw new ArgumentNullException("InstanceId needed");
|
|
List<Resource> resources = new List<Resource>();
|
|
if (types.Count > 0)
|
|
{
|
|
resources = _resourceService.GetAllByType(instanceId, types);
|
|
}
|
|
else
|
|
{
|
|
resources = _resourceService.GetAll(instanceId);
|
|
}
|
|
|
|
List<ResourceDTO> resourceDTOs = new List<ResourceDTO>();
|
|
foreach(var resource in resources)
|
|
{
|
|
ResourceDTO resourceDTO = new ResourceDTO();
|
|
resourceDTO = resource.ToDTO();
|
|
if(resource.Type == ResourceType.ImageUrl)
|
|
{
|
|
var resourceData = _resourceDataService.GetByResourceId(resource.Id);
|
|
resourceDTO.data = resourceData != null ? resourceData.Data : null;
|
|
}
|
|
resourceDTOs.Add(resourceDTO);
|
|
}
|
|
|
|
return new OkObjectResult(resourceDTOs.OrderByDescending(r => r.dateCreation));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a specific resource
|
|
/// </summary>
|
|
/// <param name="id">id resource</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(ResourceDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{id}/detail")]
|
|
public ObjectResult GetDetail(string id)
|
|
{
|
|
try
|
|
{
|
|
Resource resource = _resourceService.GetById(id);
|
|
|
|
if (resource == null)
|
|
throw new KeyNotFoundException("This resource was not found");
|
|
|
|
ResourceDTO resourceDTO = new ResourceDTO();
|
|
resourceDTO = resource.ToDTO();
|
|
ResourceData resourceData = _resourceDataService.GetByResourceId(id);
|
|
resourceDTO.data = resourceData.Data;
|
|
/*if (resource.Type == ResourceType.ImageUrl)
|
|
{
|
|
var resourceData = _resourceDataService.GetByResourceId(resource.Id);
|
|
resourceDTO.data = resourceData != null ? resourceData.Data : null;
|
|
}*/
|
|
|
|
// RESIZE IMAGE
|
|
|
|
/*byte[] imageBytes = Convert.FromBase64String(resourceData.Data);
|
|
|
|
using (MemoryStream originalImageMemoryStream = new MemoryStream(imageBytes))
|
|
{
|
|
using (Image image = Image.FromStream(originalImageMemoryStream))
|
|
{
|
|
var width = image.Width;
|
|
var height = image.Height;
|
|
|
|
if (image.Width > MaxWidth || image.Height > MaxHeight)
|
|
{
|
|
Size newSize = ImageResizer.ResizeKeepAspect(image.Size, MaxWidth, MaxHeight);
|
|
byte[] resizedImage = ImageResizer.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height);
|
|
|
|
resourceData.Data = Convert.ToBase64String(resizedImage);
|
|
ResourceData resourceModified = _resourceDataService.Update(resourceData.Id, resourceData);
|
|
}
|
|
}
|
|
}*/
|
|
|
|
return new OkObjectResult(resourceDTO);
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show a specific resource (as a picture or video stream)
|
|
/// </summary>
|
|
/// <param name="id">id resource</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(FileResult), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{id}")]
|
|
public ActionResult Show(string id)
|
|
{
|
|
try
|
|
{
|
|
Resource resource = _resourceService.GetById(id);
|
|
ResourceData resourceData = _resourceDataService.GetByResourceId(id);
|
|
|
|
if (resource == null || resourceData == null)
|
|
throw new KeyNotFoundException("This resource was not found");
|
|
|
|
var file = Convert.FromBase64String(resourceData.Data);
|
|
|
|
// RESIZE IMAGE
|
|
|
|
/*using (MemoryStream originalImageMemoryStream = new MemoryStream(file))
|
|
{
|
|
using (Image image = Image.FromStream(originalImageMemoryStream))
|
|
{
|
|
var width = image.Width;
|
|
var height = image.Height;
|
|
|
|
if(image.Width > MaxWidth || image.Height > MaxHeight)
|
|
{
|
|
Size newSize = ImageResizer.ResizeKeepAspect(image.Size, MaxWidth, MaxHeight);
|
|
byte[] resizedImage = ImageResizer.ResizeImage(image, newSize.Width, newSize.Height, image.Width, image.Height);
|
|
|
|
resourceData.Data = Convert.ToBase64String(resizedImage);
|
|
ResourceData resourceModified = _resourceDataService.Update(resourceData.Id, resourceData);
|
|
}
|
|
}
|
|
}*/
|
|
|
|
if (resource.Type == ResourceType.Image)
|
|
{
|
|
return new FileContentResult(file, "image/png");
|
|
}
|
|
if (resource.Type == ResourceType.Video || resource.Type == ResourceType.Audio)
|
|
{
|
|
return new FileContentResult(file, "application/octet-stream");
|
|
}
|
|
|
|
return new FileContentResult(file, "image/png");
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Upload a specific resource (picture or video)
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(string), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost("upload"), DisableRequestSizeLimit]
|
|
public IActionResult Upload([FromForm] string label, [FromForm] string type, [FromForm] string instanceId) // Create but with local //[FromBody] ResourceDetailDTO uploadResource
|
|
{
|
|
try
|
|
{
|
|
if (label == null || type == null || instanceId == null)
|
|
throw new ArgumentNullException("One of resource params is null");
|
|
|
|
var resourceType = (ResourceType)Enum.Parse(typeof(ResourceType), type);
|
|
List<Resource> resources = new List<Resource>();
|
|
|
|
foreach (var file in Request.Form.Files)
|
|
{
|
|
if (file.Length > 0)
|
|
{
|
|
var stringResult = "";
|
|
double fileSizeibMbs = (double) ((double)file.Length) / (1024*1024);
|
|
if (fileSizeibMbs <= 4.01)
|
|
{
|
|
using (var ms = new MemoryStream())
|
|
{
|
|
file.CopyTo(ms);
|
|
var fileBytes = ms.ToArray();
|
|
if (resourceType == ResourceType.Image) {
|
|
fileBytes = ImageHelper.ResizeAndAddWatermark(fileBytes, MaxWidth, MaxHeight);
|
|
}
|
|
stringResult = Convert.ToBase64String(fileBytes);
|
|
}
|
|
} else
|
|
{
|
|
throw new FileLoadException(message: "Fichier inexistant ou trop volumineux (max 4Mb)");
|
|
}
|
|
// Todo add some verification ?
|
|
Resource resource = new Resource();
|
|
resource.Label = label;
|
|
resource.Type = resourceType;
|
|
resource.DateCreation = DateTime.Now;
|
|
resource.InstanceId = instanceId;
|
|
Resource resourceCreated = _resourceService.Create(resource);
|
|
resources.Add(resourceCreated);
|
|
|
|
ResourceData resourceData = new ResourceData();
|
|
resourceData.Data = stringResult;
|
|
resourceData.ResourceId = resourceCreated.Id;
|
|
resourceData.InstanceId = stringResult;
|
|
ResourceData resourceDataCreated = _resourceDataService.Create(resourceData);
|
|
}
|
|
}
|
|
return Ok(resources.Select(r => r.ToDTO()));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (FileLoadException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new ConflictObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new resource
|
|
/// </summary>
|
|
/// <param name="newResource">New resource info</param>
|
|
[ProducesResponseType(typeof(ResourceDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult Create([FromBody] ResourceDTO newResource)
|
|
{
|
|
try
|
|
{
|
|
if (newResource == null)
|
|
throw new ArgumentNullException("Resource param is null");
|
|
|
|
// Todo add some verification ?
|
|
Resource resource = new Resource();
|
|
resource.InstanceId = newResource.instanceId;
|
|
resource.Label = newResource.label;
|
|
resource.Type = newResource.type;
|
|
resource.DateCreation = DateTime.Now;
|
|
//resource.Data = newResource.data;
|
|
resource.InstanceId = newResource.instanceId;
|
|
|
|
Resource resourceCreated = _resourceService.Create(resource);
|
|
|
|
return new OkObjectResult(resourceCreated.ToDTO()); // WITHOUT DATA
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new ConflictObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update a resource
|
|
/// </summary>
|
|
/// <param name="updatedResource">Resource to update</param>
|
|
[ProducesResponseType(typeof(ResourceDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult Update([FromBody] ResourceDTO updatedResource)
|
|
{
|
|
try
|
|
{
|
|
if (updatedResource == null)
|
|
throw new ArgumentNullException("Resource param is null");
|
|
|
|
Resource resource = _resourceService.GetById(updatedResource.id);
|
|
|
|
if (resource == null)
|
|
throw new KeyNotFoundException("Resource does not exist");
|
|
|
|
// Todo add some verification ?
|
|
resource.InstanceId = updatedResource.instanceId;
|
|
resource.Label = updatedResource.label;
|
|
resource.Type = updatedResource.type;
|
|
//resource.Data = updatedResource.data; // NOT ALLOWED
|
|
|
|
Resource resourceModified = _resourceService.Update(updatedResource.id, resource);
|
|
|
|
return new OkObjectResult(resourceModified.ToDTO());
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete a resource
|
|
/// </summary>
|
|
/// <param name="id">Id of resource to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{id}")]
|
|
public ObjectResult Delete(string id)
|
|
{
|
|
try
|
|
{
|
|
if (id == null)
|
|
throw new ArgumentNullException("Resource param is null");
|
|
|
|
var ressource = _resourceService.GetById(id);
|
|
var ressourceData = _resourceDataService.GetByResourceId(id);
|
|
if (ressource == null)
|
|
throw new KeyNotFoundException("Resource does not exist");
|
|
|
|
|
|
foreach (var configuration in _configurationService.GetAll(ressource.InstanceId))
|
|
{
|
|
if (configuration.ImageId == id)
|
|
{
|
|
configuration.ImageId = null;
|
|
configuration.ImageSource = null;
|
|
}
|
|
}
|
|
|
|
|
|
// Delete all resource occurence
|
|
foreach (var section in _sectionService.GetAll(ressource.InstanceId))
|
|
{
|
|
if (section.ImageId == id)
|
|
{
|
|
section.ImageId = null;
|
|
section.ImageSource = null;
|
|
}
|
|
|
|
switch (section.Type)
|
|
{
|
|
case SectionType.Map:
|
|
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.Data);
|
|
mapDTO.iconResourceId = mapDTO.iconResourceId == id ? null : mapDTO.iconResourceId;
|
|
foreach (var point in mapDTO.points)
|
|
{
|
|
foreach (var image in point.images)
|
|
{
|
|
image.imageSource = image.imageResourceId == id ? null : image.imageSource;
|
|
image.imageResourceId = image.imageResourceId == id ? null : image.imageResourceId;
|
|
}
|
|
}
|
|
section.Data = JsonConvert.SerializeObject(mapDTO);
|
|
break;
|
|
case SectionType.Slider:
|
|
SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.Data);
|
|
List<ImageDTO> imagesToKeep = new List<ImageDTO>();
|
|
foreach (var image in sliderDTO.images)
|
|
{
|
|
if (image.resourceId != id)
|
|
imagesToKeep.Add(image);
|
|
}
|
|
sliderDTO.images = imagesToKeep;
|
|
section.Data = JsonConvert.SerializeObject(sliderDTO);
|
|
break;
|
|
case SectionType.Quizz:
|
|
QuizzDTO quizzDTO = JsonConvert.DeserializeObject<QuizzDTO>(section.Data);
|
|
foreach (var question in quizzDTO.questions)
|
|
{
|
|
question.source = question.resourceId == id ? null : question.source;
|
|
question.resourceId = question.resourceId == id ? null : question.resourceId;
|
|
}
|
|
if (quizzDTO.bad_level != null) {
|
|
quizzDTO.bad_level.source = quizzDTO.bad_level.resourceId == id ? null : quizzDTO.bad_level.source;
|
|
quizzDTO.bad_level.resourceId = quizzDTO.bad_level.resourceId == id ? null : quizzDTO.bad_level.resourceId;
|
|
}
|
|
if (quizzDTO.medium_level != null)
|
|
{
|
|
quizzDTO.medium_level.source = quizzDTO.medium_level.resourceId == id ? null : quizzDTO.medium_level.source;
|
|
quizzDTO.medium_level.resourceId = quizzDTO.medium_level.resourceId == id ? null : quizzDTO.medium_level.resourceId;
|
|
}
|
|
if (quizzDTO.good_level != null)
|
|
{
|
|
quizzDTO.good_level.source = quizzDTO.good_level.resourceId == id ? null : quizzDTO.good_level.source;
|
|
quizzDTO.good_level.resourceId = quizzDTO.good_level.resourceId == id ? null : quizzDTO.good_level.resourceId;
|
|
}
|
|
if (quizzDTO.great_level != null)
|
|
{
|
|
quizzDTO.great_level.source = quizzDTO.great_level.resourceId == id ? null : quizzDTO.great_level.source;
|
|
quizzDTO.great_level.resourceId = quizzDTO.great_level.resourceId == id ? null : quizzDTO.great_level.resourceId;
|
|
}
|
|
section.Data = JsonConvert.SerializeObject(quizzDTO);
|
|
break;
|
|
case SectionType.Article:
|
|
ArticleDTO articleDTO = JsonConvert.DeserializeObject<ArticleDTO>(section.Data);
|
|
List<ImageDTO> imagesArticleToKeep = new List<ImageDTO>();
|
|
foreach (var image in articleDTO.images)
|
|
{
|
|
if (image.resourceId != id)
|
|
imagesArticleToKeep.Add(image);
|
|
}
|
|
articleDTO.images = imagesArticleToKeep;
|
|
section.Data = JsonConvert.SerializeObject(articleDTO);
|
|
break;
|
|
}
|
|
|
|
_sectionService.Update(section.Id, section);
|
|
}
|
|
|
|
_resourceService.Remove(id);
|
|
if (ressourceData != null)
|
|
{
|
|
_resourceDataService.Remove(ressourceData.Id);
|
|
}
|
|
|
|
return new ObjectResult("The resource has been deleted") { StatusCode = 202 };
|
|
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|