From 30505154b6ec4a00b438748f3bb67a9a791487eb Mon Sep 17 00:00:00 2001 From: Daniel Gyulai Date: Sun, 22 Nov 2020 19:12:37 +0100 Subject: [PATCH] Added validation to image download --- CoviDok/BLL/Storage/StorageHandler.cs | 7 ++++++- CoviDok/Controllers/ImagesController.cs | 25 ++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CoviDok/BLL/Storage/StorageHandler.cs b/CoviDok/BLL/Storage/StorageHandler.cs index 4bc051c..2c55f8d 100644 --- a/CoviDok/BLL/Storage/StorageHandler.cs +++ b/CoviDok/BLL/Storage/StorageHandler.cs @@ -41,7 +41,12 @@ namespace CoviDok.BLL.Storage public async Task GetImage(string bucketName, string ImageId, Action callback) { - await storageProvider.Download(bucketName, ImageId, 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) diff --git a/CoviDok/Controllers/ImagesController.cs b/CoviDok/Controllers/ImagesController.cs index 8f70d57..0d8eb7b 100644 --- a/CoviDok/Controllers/ImagesController.cs +++ b/CoviDok/Controllers/ImagesController.cs @@ -67,15 +67,22 @@ namespace CoviDok.Controllers response.Body["reason"] = "unauthorized"; return response; } - - string res = null; ; - await MinioHandler.GetImage(BucketName, imageGet.ImageId, (stream) => { - StreamReader reader = new StreamReader(stream); - res = reader.ReadToEnd(); - }); - - response.Body["image"] = res; - return response; + try { + string res = null; + await MinioHandler.GetImage(BucketName, imageGet.ImageId, (stream) => { + StreamReader reader = new StreamReader(stream); + res = reader.ReadToEnd(); + }); + + response.Body["image"] = res; + return response; + } + catch (KeyNotFoundException) + { + response.Status = Status.Error; + response.Body["reason"] = "Image not found!"; + return response; + } } } }