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.
 
 
 
 
 

75 lines
2.3 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 Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NCuid;
namespace CoviDok.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImagesController : ControllerBase
{
private MinioHandler MinioHandler = new MinioHandler(
"minio:9000",
"secretaccesskey",
"secretsecretkey");
private readonly string BucketName = "test1";
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<GenericResponse> OnPostImage(ImagePost post)
{
GenericResponse response = new GenericResponse();
if (post.SessionID != "a")
{
response.Status = Status.Error;
response.Body["reason"] = "unauthorized";
return response;
}
MinioResult 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 gr = new GenericResponse();
string[] attrs = imageGet.ImageID.Split(":");
if (attrs.Length != 2) {
gr.Status = Status.Error;
gr.Body["reason"] = "Bad image ID!";
return gr;
}
string res = null; ;
await MinioHandler.GetImage(imageGet.ImageID, (stream) => {
StreamReader reader = new StreamReader(stream);
res = reader.ReadToEnd();
});
gr.Body["image"] = res;
return gr;
}
}
}