/
StorageHelper.cs
148 lines (117 loc) · 5.67 KB
/
StorageHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using JesseCarlbergProduction.Models;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace JesseCarlbergProduction.Extensions
{
public static class StorageHelper
{
public static bool IsImage(IFormFile file)
{
if (file.ContentType.Contains("image"))
{
return true;
}
string[] formats = new string[] { ".jpg", ".png", ".gif", ".jpeg" };
return formats.Any(item => file.FileName.EndsWith(item, StringComparison.OrdinalIgnoreCase));
}
public static async Task<bool> UploadFileToStorage(Stream fileStream, string fileName,
IAzureStorageConfig _storageConfig)
{
// Create a URI to the blob
Uri blobUri = new Uri("https://" +
_storageConfig.AccountName +
".blob.core.windows.net/" +
_storageConfig.ImageContainer +
"/" + fileName);
// Create StorageSharedKeyCredentials object by reading
// the values from the configuration (appsettings.json)
StorageSharedKeyCredential storageCredentials =
new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
// Create the blob client.
BlobClient blobClient = new BlobClient(blobUri, storageCredentials);
// Upload the file
await blobClient.UploadAsync(fileStream);
return await Task.FromResult(true);
}
public static async Task<bool> UploadFileToPhotoStorage(Stream fileStream, string fileName, string imageContainer,
IAzureStorageConfig _storageConfig)
{
// Create a URI to the blob
Uri blobUri = new Uri("https://" +
_storageConfig.AccountName +
".blob.core.windows.net/" +
imageContainer +
"/" + fileName);
// Create StorageSharedKeyCredentials object by reading
// the values from the configuration (appsettings.json)
StorageSharedKeyCredential storageCredentials =
new StorageSharedKeyCredential(_storageConfig.AccountName, _storageConfig.AccountKey);
// Create the blob client.
BlobClient blobClient = new BlobClient(blobUri, storageCredentials);
// Upload the file
await blobClient.UploadAsync(fileStream);
return await Task.FromResult(true);
}
public static async Task<List<string>> GetThumbNailUrls(IAzureStorageConfig _storageConfig)
{
List<string> thumbnailUrls = new List<string>();
// Create a URI to the storage account
Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/");
// Create BlobServiceClient from the account URI
BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri);
// Get reference to the container
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ThumbnailContainer);
if (container.Exists())
{
foreach (BlobItem blobItem in container.GetBlobs())
{
thumbnailUrls.Add(container.Uri + "/" + blobItem.Name);
}
}
return await Task.FromResult(thumbnailUrls);
}
public static async Task<List<string>> GetImagesUrls(IAzureStorageConfig _storageConfig)
{
List<string> imageUrls = new List<string>();
// Create a URI to the storage account
Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/");
// Create BlobServiceClient from the account URI
BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri);
// Get reference to the container
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ImageContainer);
if (container.Exists())
{
foreach (BlobItem blobItem in container.GetBlobs())
{
imageUrls.Add(container.Uri + "/" + blobItem.Name);
}
}
return await Task.FromResult(imageUrls);
}
public static async Task<List<string>> GetPhotoSessionImagesUrls(IAzureStorageConfig _storageConfig, string photoShoot)
{
List<string> imageUrls = new List<string>();
// Create a URI to the storage account
Uri accountUri = new Uri("https://" + _storageConfig.AccountName + ".blob.core.windows.net/");
// Create BlobServiceClient from the account URI
BlobServiceClient blobServiceClient = new BlobServiceClient(accountUri);
// Get reference to the container
BlobContainerClient container = blobServiceClient.GetBlobContainerClient(_storageConfig.ImageContainer);
if (container.Exists())
{
foreach (BlobItem blobItem in container.GetBlobs())
{
imageUrls.Add(container.Uri + "/" + photoShoot);
}
}
return await Task.FromResult(imageUrls);
}
}
}