Skip to content

Commit a7744be

Browse files
committed
Updates to v12
1 parent a3f1d05 commit a7744be

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

ImageResizeWebApp/ImageResizeWebApp/Controllers/ImagesController.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
2929

3030
try
3131
{
32-
3332
if (files.Count == 0)
34-
3533
return BadRequest("No files received from the upload");
3634

3735
if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)
38-
3936
return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");
4037

4138
if (storageConfig.ImageContainer == string.Empty)
42-
4339
return BadRequest("Please provide a name for your image container in the azure blob storage");
4440

4541
foreach (var formFile in files)
@@ -63,18 +59,12 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
6359
if (isUploaded)
6460
{
6561
if (storageConfig.ThumbnailContainer != string.Empty)
66-
6762
return new AcceptedAtActionResult("GetThumbNails", "Images", null, null);
68-
6963
else
70-
7164
return new AcceptedResult();
7265
}
7366
else
74-
7567
return BadRequest("Look like the image couldnt upload to the storage");
76-
77-
7868
}
7969
catch (Exception ex)
8070
{
@@ -86,28 +76,21 @@ public async Task<IActionResult> Upload(ICollection<IFormFile> files)
8676
[HttpGet("thumbnails")]
8777
public async Task<IActionResult> GetThumbNails()
8878
{
89-
9079
try
9180
{
9281
if (storageConfig.AccountKey == string.Empty || storageConfig.AccountName == string.Empty)
93-
94-
return BadRequest("sorry, can't retrieve your azure storage details from appsettings.js, make sure that you add azure storage details there");
82+
return BadRequest("Sorry, can't retrieve your Azure storage details from appsettings.js, make sure that you add Azure storage details there.");
9583

9684
if (storageConfig.ImageContainer == string.Empty)
97-
98-
return BadRequest("Please provide a name for your image container in the azure blob storage");
85+
return BadRequest("Please provide a name for your image container in Azure blob storage.");
9986

10087
List<string> thumbnailUrls = await StorageHelper.GetThumbNailUrls(storageConfig);
101-
102-
return new ObjectResult(thumbnailUrls);
103-
88+
return new ObjectResult(thumbnailUrls);
10489
}
10590
catch (Exception ex)
10691
{
10792
return BadRequest(ex.Message);
10893
}
109-
11094
}
111-
11295
}
11396
}

ImageResizeWebApp/ImageResizeWebApp/Helpers/StorageHelper.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
using ImageResizeWebApp.Models;
22
using Microsoft.AspNetCore.Http;
3+
using Azure;
34
using Azure.Storage;
45
using Azure.Storage.Blobs;
6+
using Azure.Storage.Sas;
57

68
using System;
79
using System.Collections.Generic;
810
using System.IO;
911
using System.Linq;
1012
using System.Threading.Tasks;
1113
using Azure.Storage.Blobs.Models;
14+
using System.Diagnostics;
1215

1316
namespace ImageResizeWebApp.Helpers
1417
{
@@ -31,15 +34,15 @@ public static async Task<bool> UploadFileToStorage(Stream fileStream, string fil
3134
AzureStorageConfig _storageConfig)
3235
{
3336
// Create a URI to the blob
34-
Uri blobUri = new Uri("https://" +
35-
_storageConfig.AccountName +
36-
".blob.core.windows.net/" +
37-
_storageConfig.ImageContainer +
37+
Uri blobUri = new Uri("https://" +
38+
_storageConfig.AccountName +
39+
".blob.core.windows.net/" +
40+
_storageConfig.ImageContainer +
3841
"/" + fileName);
3942

4043
// Create StorageSharedKeyCredentials object by reading
4144
// the values from the configuration (appsettings.json)
42-
StorageSharedKeyCredential storageCredentials =
45+
StorageSharedKeyCredential storageCredentials =
4346
new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
4447

4548
// Create the blob client.
@@ -64,9 +67,12 @@ public static async Task<List<string>> GetThumbNailUrls(AzureStorageConfig _stor
6467
// Get reference to the container
6568
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer);
6669

67-
foreach (BlobItem blobItem in container.GetBlobs())
70+
if (container.Exists())
6871
{
69-
thumbnailUrls.Add(container.Uri + "/" + blobItem.Name);
72+
foreach (BlobItem blobItem in container.GetBlobs())
73+
{
74+
thumbnailUrls.Add(container.Uri + "/" + blobItem.Name);
75+
}
7076
}
7177

7278
return await Task.FromResult(thumbnailUrls);

0 commit comments

Comments
 (0)