From 25d82fb8c173c873edd40667322de152db61cbf0 Mon Sep 17 00:00:00 2001 From: Daniel Gyulai Date: Mon, 23 Nov 2020 22:55:02 +0100 Subject: [PATCH] Debug log --- CoviDok/BLL/Storage/StorageHandler.cs | 116 ++++++++++++------------ CoviDok/Controllers/ImagesController.cs | 20 ++-- CoviDok/Startup.cs | 2 +- 3 files changed, 72 insertions(+), 66 deletions(-) diff --git a/CoviDok/BLL/Storage/StorageHandler.cs b/CoviDok/BLL/Storage/StorageHandler.cs index 2c55f8d..6fb387e 100644 --- a/CoviDok/BLL/Storage/StorageHandler.cs +++ b/CoviDok/BLL/Storage/StorageHandler.cs @@ -1,57 +1,59 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading.Tasks; - -namespace CoviDok.BLL.Storage -{ - class StorageHandler - { - private readonly IStorageProvider storageProvider; - - public StorageHandler(IStorageProvider provider) - { - //Client = new MinioClient( - //"192.168.0.160:9000", - //"secretaccesskey", - //"secretsecretkey"); - storageProvider = provider; - } - - public async Task UploadImage(string BucketName, Stream FilePath, long size, string ObjectName) - { - try - { - // Make a bucket on the server, if not already present. - bool found = await storageProvider.NamespaceExists(BucketName); - if (!found) - { - await storageProvider.CreateNamespace(BucketName); - } - // Upload a file to bucket. - await storageProvider.Upload(BucketName, ObjectName, FilePath, size); - return new StorageResult(true, ObjectName); - } - catch (Exception e) - { - return new StorageResult(false, e.Message); - } - } - - public async Task GetImage(string bucketName, string ImageId, Action callback) - { - if (await storageProvider.ObjectExists(bucketName, ImageId)) - { - await storageProvider.Download(bucketName, ImageId, callback); - } - else throw new KeyNotFoundException(); - - } - - public async Task ImageExists(string bucketName, string ImageId) - { - return await storageProvider.ObjectExists(bucketName, ImageId); - } - } -} +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; + +namespace CoviDok.BLL.Storage +{ + class StorageHandler + { + private readonly IStorageProvider storageProvider; + + public StorageHandler(IStorageProvider provider) + { + //Client = new MinioClient( + //"192.168.0.160:9000", + //"secretaccesskey", + //"secretsecretkey"); + storageProvider = provider; + } + + public async Task UploadImage(string BucketName, Stream FilePath, long size, string ObjectName) + { + try + { + // Make a bucket on the server, if not already present. + bool found = await storageProvider.NamespaceExists(BucketName); + if (!found) + { + await storageProvider.CreateNamespace(BucketName); + } + // Upload a file to bucket. + Console.WriteLine("StorageHandler:UploadImage - Uploading " + ObjectName); + await storageProvider.Upload(BucketName, ObjectName, FilePath, size); + Console.WriteLine("StorageHandler:UploadImage - Uploaded " + ObjectName); + return new StorageResult(true, ObjectName); + } + catch (Exception e) + { + return new StorageResult(false, e.Message); + } + } + + public async Task GetImage(string bucketName, string ImageId, Action callback) + { + if (await storageProvider.ObjectExists(bucketName, ImageId)) + { + await storageProvider.Download(bucketName, ImageId, callback); + } + else throw new KeyNotFoundException(); + + } + + public async Task ImageExists(string bucketName, string ImageId) + { + return await storageProvider.ObjectExists(bucketName, ImageId); + } + } +} diff --git a/CoviDok/Controllers/ImagesController.cs b/CoviDok/Controllers/ImagesController.cs index e2c766b..295e77e 100644 --- a/CoviDok/Controllers/ImagesController.cs +++ b/CoviDok/Controllers/ImagesController.cs @@ -41,19 +41,23 @@ namespace CoviDok.Controllers [HttpPost("Upload")] public async Task> OnPostImage(ImagePost post) { - GenericResponse response = new GenericResponse(); - Session s = await Handler.GetSession(post.SessionId); - if (s == null) + GenericResponse response = new GenericResponse(); + try { + 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.Body["reason"] = "unauthorized"; 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")] diff --git a/CoviDok/Startup.cs b/CoviDok/Startup.cs index 6fb39dc..7c583e3 100644 --- a/CoviDok/Startup.cs +++ b/CoviDok/Startup.cs @@ -37,7 +37,7 @@ namespace CoviDok AccessKey = _configuration["MinioSettings:AccessKey"], 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.