Browse Source

Added (yet untested) image handling. May or may not work.

master
Daniel Gyulai 4 years ago
parent
commit
8ba1bd5df4
  1. 13
      CoviDok/Api/Request/ImageGet.cs
  2. 14
      CoviDok/Api/Request/ImagePost.cs
  3. 41
      CoviDok/BLL/MinioHandler.cs
  4. 14
      CoviDok/BLL/MinioResult.cs
  5. 53
      CoviDok/Controllers/ImagesController.cs

13
CoviDok/Api/Request/ImageGet.cs

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoviDok.Api.Request
{
public class ImageGet
{
public string SessionID { get; set; }
public string ImageID { get; set; }
}
}

14
CoviDok/Api/Request/ImagePost.cs

@ -0,0 +1,14 @@
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CoviDok.Api.Request
{
public class ImagePost
{
public string SessionID { get; set; }
public IFormFile File { get; set; }
}
}

41
CoviDok/BLL/MinioHandler.cs

@ -2,6 +2,7 @@
using Minio.Exceptions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
@ -11,27 +12,16 @@ namespace CoviDok.BLL
{
private MinioClient Client = null;
public class MinioResult
public MinioHandler(string Host, string AccessKey, string SecretKey)
{
bool Success;
string Data;
public MinioResult(bool Success, string Data)
{
this.Success = Success;
this.Data = Data;
}
//Client = new MinioClient(
//"192.168.0.160:9000",
//"secretaccesskey",
//"secretsecretkey");
Client = new MinioClient(Host, AccessKey, SecretKey);
}
public MinioHandler()
{
Client = new MinioClient(
"192.168.0.160:9000",
"secretaccesskey",
"secretsecretkey");
}
public async Task<MinioResult> Upload(string BucketName, string FilePath, string ObjectName)
public async Task<MinioResult> UploadImage(string BucketName, Stream FilePath, long size, string ObjectName)
{
try
{
@ -42,7 +32,7 @@ namespace CoviDok.BLL
await Client.MakeBucketAsync(BucketName);
}
// Upload a file to bucket.
await Client.PutObjectAsync(BucketName, ObjectName, FilePath);
await Client.PutObjectAsync(BucketName, ObjectName, FilePath, size);
return new MinioResult(true, BucketName + ":" + ObjectName);
}
catch (MinioException e)
@ -51,17 +41,10 @@ namespace CoviDok.BLL
}
}
public async Task<MinioResult> GetImage(string BucketName, string FilePath, string ObjectName)
public async Task GetImage(string ImageID, Action<Stream> callback)
{
try
{
await Client.GetObjectAsync(BucketName, ObjectName, FilePath);
return new MinioResult(true, FilePath);
}
catch (MinioException e)
{
return new MinioResult(false, e.Message);
}
string[] attrs = ImageID.Split(":");
await Client.GetObjectAsync(attrs[0], attrs[1], callback);
}
}
}

14
CoviDok/BLL/MinioResult.cs

@ -0,0 +1,14 @@
namespace CoviDok.BLL
{
public class MinioResult
{
public readonly bool Success;
public readonly string Data;
public MinioResult(bool Success, string Data)
{
this.Success = Success;
this.Data = Data;
}
}
}

53
CoviDok/Controllers/ImagesController.cs

@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CoviDok.Api;
using CoviDok.Api.Request;
using CoviDok.BLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace CoviDok.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImagesController : ControllerBase
{
private MinioHandler MinioHandler = new MinioHandler(
"192.168.0.160:9000",
"secretaccesskey",
"secretsecretkey");
private readonly string BucketName = "test1";
[HttpPost]
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, post.File.OpenReadStream(), post.File.Length, post.File.FileName);
if (!Result.Success) response.Status = Status.Error;
response.Body["reason"] = Result.Data;
return response;
}
public async Task<IActionResult> OnGetImage(ImageGet imageGet)
{
string[] attrs = imageGet.ImageID.Split(":");
if (attrs.Length != 2) return BadRequest();
FileStreamResult res = null; ;
await MinioHandler.GetImage(imageGet.ImageID, (stream) => {
res = File(stream, attrs[1]);
});
return res;
}
}
}
Loading…
Cancel
Save