|
|
@ -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<GenericResponse> 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<IActionResult> OnGetImage(ImageGet imageGet) |
|
|
|
public async Task<GenericResponse> 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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|