using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RobotNet.MapManager.Services; namespace RobotNet.MapManager.Controllers; [Route("api/[controller]")] [ApiController] [AllowAnonymous] public class ImagesController(MapEditorStorageRepository StorageRepo, IHttpClientFactory HttpClientFactory, LoggerController Logger) : ControllerBase { [HttpGet] [Route("map/{id}")] public async Task GetMapImage(Guid id) { try { var (usingLocal, url) = StorageRepo.GetUrl("MapImages", $"{id}"); if (!usingLocal) { var http = HttpClientFactory.CreateClient(); var imageBytes = await http.GetByteArrayAsync(url); if (imageBytes != null && imageBytes.Length > 0) return File(imageBytes, "image/png"); else return NotFound("Không thể lấy được ảnh map."); } if (System.IO.File.Exists(url)) return File(System.IO.File.ReadAllBytes(url), "image/png"); else return NotFound(); } catch(Exception ex) { Logger.Warning($"GetMapImage {id}: Hệ thống có lỗi xảy ra - {ex.Message}"); return NotFound(); } } [HttpGet] [Route("elementModel/{id}")] public async Task GetElementModelImage(Guid id, [FromQuery] bool IsOpen) { try { var (usingLocal, url) = StorageRepo.GetUrl(IsOpen ? "ElementOpenModels" : "ElementCloseModels", id.ToString()); if (!usingLocal) { var http = HttpClientFactory.CreateClient(); var imageBytes = await http.GetByteArrayAsync(url); if (imageBytes != null && imageBytes.Length > 0) { return File(imageBytes, "image/png"); } else { return NotFound("Không thể lấy được ảnh element model."); } } if (System.IO.File.Exists(url)) return File(System.IO.File.ReadAllBytes(url), "image/png"); else return NotFound(); } catch (Exception ex) { Logger.Warning($"GetElementModelImage {id}: Hệ thống có lỗi xảy ra - {ex.Message}"); return NotFound(); } } }