You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

88 lines
2.9 KiB

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using CoviDok.Api;
using CoviDok.Api.Request;
using CoviDok.BLL;
using CoviDok.BLL.Sessions;
using CoviDok.BLL.Storage;
using CoviDok.Data.SessionProviders;
using CoviDok.Data.StorageProviders;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using NCuid;
namespace CoviDok.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImagesController : ControllerBase
{
private StorageHandler MinioHandler = new StorageHandler(new MinioProvider());
private readonly string BucketName = "test1";
private readonly SessionHandler Handler = new SessionHandler();
private static Stream MakeStream(string s)
{
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
[HttpPost("Upload")]
public async Task<ActionResult<GenericResponse>> OnPostImage(ImagePost post)
{
GenericResponse response = new GenericResponse();
Session s = await Handler.GetSession(post.SessionId);
if (s == null)
{
response.Status = Status.Error;
response.Body["reason"] = "unauthorized";
return response;
}
StorageResult Result = await MinioHandler.UploadImage(BucketName, MakeStream(post.File), post.File.Length, Cuid.Generate());
if (!Result.Success) response.Status = Status.Error;
response.Body["reason"] = Result.Data;
return response;
}
[HttpPost("Download")]
public async Task<GenericResponse> OnGetImage(ImageGet imageGet)
{
GenericResponse response = new GenericResponse();
Session s = await Handler.GetSession(imageGet.SessionId);
if (s == null)
{
response.Status = Status.Error;
response.Body["reason"] = "unauthorized";
return response;
}
try {
string res = null;
await MinioHandler.GetImage(BucketName, imageGet.ImageId, (stream) => {
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
});
response.Body["image"] = res;
return response;
}
catch (KeyNotFoundException)
{
response.Status = Status.Error;
response.Body["reason"] = "Image not found!";
return response;
}
}
}
}