Browse Source

Debug log

master
Daniel Gyulai 4 years ago
parent
commit
25d82fb8c1
  1. 116
      CoviDok/BLL/Storage/StorageHandler.cs
  2. 20
      CoviDok/Controllers/ImagesController.cs
  3. 2
      CoviDok/Startup.cs

116
CoviDok/BLL/Storage/StorageHandler.cs

@ -1,57 +1,59 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace CoviDok.BLL.Storage namespace CoviDok.BLL.Storage
{ {
class StorageHandler class StorageHandler
{ {
private readonly IStorageProvider storageProvider; private readonly IStorageProvider storageProvider;
public StorageHandler(IStorageProvider provider) public StorageHandler(IStorageProvider provider)
{ {
//Client = new MinioClient( //Client = new MinioClient(
//"192.168.0.160:9000", //"192.168.0.160:9000",
//"secretaccesskey", //"secretaccesskey",
//"secretsecretkey"); //"secretsecretkey");
storageProvider = provider; storageProvider = provider;
} }
public async Task<StorageResult> UploadImage(string BucketName, Stream FilePath, long size, string ObjectName) public async Task<StorageResult> UploadImage(string BucketName, Stream FilePath, long size, string ObjectName)
{ {
try try
{ {
// Make a bucket on the server, if not already present. // Make a bucket on the server, if not already present.
bool found = await storageProvider.NamespaceExists(BucketName); bool found = await storageProvider.NamespaceExists(BucketName);
if (!found) if (!found)
{ {
await storageProvider.CreateNamespace(BucketName); await storageProvider.CreateNamespace(BucketName);
} }
// Upload a file to bucket. // Upload a file to bucket.
await storageProvider.Upload(BucketName, ObjectName, FilePath, size); Console.WriteLine("StorageHandler:UploadImage - Uploading " + ObjectName);
return new StorageResult(true, ObjectName); await storageProvider.Upload(BucketName, ObjectName, FilePath, size);
} Console.WriteLine("StorageHandler:UploadImage - Uploaded " + ObjectName);
catch (Exception e) return new StorageResult(true, ObjectName);
{ }
return new StorageResult(false, e.Message); catch (Exception e)
} {
} return new StorageResult(false, e.Message);
}
public async Task GetImage(string bucketName, string ImageId, Action<Stream> callback) }
{
if (await storageProvider.ObjectExists(bucketName, ImageId)) public async Task GetImage(string bucketName, string ImageId, Action<Stream> callback)
{ {
await storageProvider.Download(bucketName, ImageId, callback); if (await storageProvider.ObjectExists(bucketName, ImageId))
} {
else throw new KeyNotFoundException(); await storageProvider.Download(bucketName, ImageId, callback);
}
} else throw new KeyNotFoundException();
public async Task<bool> ImageExists(string bucketName, string ImageId) }
{
return await storageProvider.ObjectExists(bucketName, ImageId); public async Task<bool> ImageExists(string bucketName, string ImageId)
} {
} return await storageProvider.ObjectExists(bucketName, ImageId);
} }
}
}

20
CoviDok/Controllers/ImagesController.cs

@ -41,19 +41,23 @@ namespace CoviDok.Controllers
[HttpPost("Upload")] [HttpPost("Upload")]
public async Task<ActionResult<GenericResponse>> OnPostImage(ImagePost post) public async Task<ActionResult<GenericResponse>> OnPostImage(ImagePost post)
{ {
GenericResponse response = new GenericResponse(); GenericResponse response = new GenericResponse();
Session s = await Handler.GetSession(post.SessionId); try
if (s == null)
{ {
Session s = await Handler.GetSession(post.SessionId);
Console.WriteLine("ImagesController:OnPostImage - Incoming request, ID: " + post.SessionId + ", payload: " + post.File.Length);
string id = Cuid.Generate();
StorageResult Result = await MinioHandler.UploadImage(BucketName, MakeStream(post.File), post.File.Length, id);
Console.WriteLine("ImagesController:OnPostImage - Request successful, uploaded " + id);
if (!Result.Success) response.Status = Status.Error;
response.Body["reason"] = Result.Data;
return response;
}
catch (UnauthorizedAccessException) {
response.Status = Status.Error; response.Status = Status.Error;
response.Body["reason"] = "unauthorized"; response.Body["reason"] = "unauthorized";
return response; 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")] [HttpPost("Download")]

2
CoviDok/Startup.cs

@ -37,7 +37,7 @@ namespace CoviDok
AccessKey = _configuration["MinioSettings:AccessKey"], AccessKey = _configuration["MinioSettings:AccessKey"],
SecretKey = _configuration["MinioSettings:SecretKey"] SecretKey = _configuration["MinioSettings:SecretKey"]
}; };
Console.WriteLine("Minio host is" + MinioProvider._settings.HostName); Console.WriteLine("Minio host is " + MinioProvider._settings.HostName);
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Loading…
Cancel
Save