From c3272e150de1938ac07f9e60e069ecf87203eaee Mon Sep 17 00:00:00 2001 From: Daniel Gyulai Date: Thu, 12 Nov 2020 13:47:40 +0100 Subject: [PATCH] Change Minio to base64 strings --- CoviDok/Api/Request/ImagePost.cs | 2 +- CoviDok/Controllers/ImagesController.cs | 35 ++++++++++++++++++++----- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CoviDok/Api/Request/ImagePost.cs b/CoviDok/Api/Request/ImagePost.cs index 6609145..f013002 100644 --- a/CoviDok/Api/Request/ImagePost.cs +++ b/CoviDok/Api/Request/ImagePost.cs @@ -9,6 +9,6 @@ namespace CoviDok.Api.Request public class ImagePost { public string SessionID { get; set; } - public IFormFile File { get; set; } + public string File { get; set; } } } diff --git a/CoviDok/Controllers/ImagesController.cs b/CoviDok/Controllers/ImagesController.cs index e6ab438..41e7110 100644 --- a/CoviDok/Controllers/ImagesController.cs +++ b/CoviDok/Controllers/ImagesController.cs @@ -1,12 +1,15 @@ 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 { @@ -20,6 +23,16 @@ namespace CoviDok.Controllers "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 OnPostImage(ImagePost post) { @@ -29,9 +42,9 @@ namespace CoviDok.Controllers response.Status = Status.Error; response.Body["reason"] = "unauthorized"; return response; - } + } - MinioResult Result = await MinioHandler.UploadImage(BucketName, post.File.OpenReadStream(), post.File.Length, post.File.FileName); + 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; @@ -39,16 +52,24 @@ namespace CoviDok.Controllers } [HttpPost("Download")] - public async Task OnGetImage(ImageGet imageGet) + public async Task OnGetImage(ImageGet imageGet) { + GenericResponse gr = new GenericResponse(); string[] attrs = imageGet.ImageID.Split(":"); - if (attrs.Length != 2) return BadRequest(); + if (attrs.Length != 2) { + gr.Status = Status.Error; + gr.Body["reason"] = "Bad image ID!"; + return gr; + } - FileStreamResult res = null; ; + string res = null; ; await MinioHandler.GetImage(imageGet.ImageID, (stream) => { - res = File(stream, attrs[1]); + StreamReader reader = new StreamReader(stream); + res = reader.ReadToEnd(); }); - return res; + + gr.Body["image"] = res; + return gr; } } }