marco.pms.api/Marco.Pms.Services/Controllers/WeatherForecastController.cs

73 lines
2.5 KiB
C#

using Marco.Pms.Model.Utilities;
using Marco.Pms.Services.Service;
using Microsoft.AspNetCore.Mvc;
namespace MarcoBMS.Services.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
private readonly S3UploadService _s3Service;
public WeatherForecastController(ILogger<WeatherForecastController> logger, S3UploadService s3Service)
{
_logger = logger;
_s3Service = s3Service;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
[HttpPost("upload-image")]
public async Task<IActionResult> SendToSThree([FromBody] FileUploadModel Image)
{
if (string.IsNullOrEmpty(Image.Base64Data))
return BadRequest("Base64 data is missing");
byte[] fileBytes;
try
{
//If base64 has a data URI prefix, strip it
var base64 = Image.Base64Data.Contains(",")
? Image.Base64Data.Substring(Image.Base64Data.IndexOf(",") + 1)
: Image.Base64Data;
fileBytes = Convert.FromBase64String(base64);
}
catch (Exception error)
{
//return BadRequest("Invalid base64 string.");
return BadRequest(error);
}
using var stream = new MemoryStream(fileBytes);
var objectKey = await _s3Service.UploadFileAsync(stream, Image.FileName, Image.ContentType);
//var objectKey = await _s3Service.UploadFileAsync(Image.FileName, Image.ContentType);
var preSignedUrl = await _s3Service.GeneratePreSignedUrlAsync(objectKey);
return Ok(new
{
objectKey,
url = preSignedUrl
});
}
}
}