@@ -22,120 +22,109 @@ public static bool IsImage(IFormFile file)
22
22
return true ;
23
23
}
24
24
25
- string [ ] formats = new string [ ] { ".jpg" , ".png" , ".gif" , ".jpeg" } ; // add more if u like...
25
+ string [ ] formats = new string [ ] { ".jpg" , ".png" , ".gif" , ".jpeg" } ;
26
26
27
- // linq from Henrik Stenbæk
28
27
return formats . Any ( item => file . FileName . EndsWith ( item , StringComparison . OrdinalIgnoreCase ) ) ;
29
28
}
30
- public static async Task < string > UploadFileToStorage ( Stream fileStream , string fileName , AzureStorageConfig _storageConfig )
31
- {
32
- string containerName = Guid . NewGuid ( ) . ToString ( ) . ToLower ( ) ;
33
-
34
- if ( _storageConfig != null )
35
- {
36
- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
37
29
38
- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
39
-
40
- if ( storageAccount != null )
41
- {
42
- // Create the blob client.
43
- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
30
+ public static async Task < bool > UploadFileToStorage ( Stream fileStream , string fileName , AzureStorageConfig _storageConfig )
31
+ {
32
+ // Create storagecredentials object by reading the values from the configuration (appsettings.json)
33
+ StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
44
34
45
- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ImageContainer ) ;
35
+ // Create cloudstorage account by passing the storagecredentials
36
+ CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
46
37
47
- await container . CreateIfNotExistsAsync ( ) ;
38
+ // Create the blob client.
39
+ CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
48
40
49
- // Retrieve reference to a blob named "myblob".
50
- CloudBlockBlob blockBlob = container . GetBlockBlobReference ( fileName ) ;
41
+ // Get reference to the blob container by passing the name by reading the value from the configuration (appsettings.json)
42
+ CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ImageContainer ) ;
51
43
52
- await blockBlob . UploadFromStreamAsync ( fileStream ) ;
44
+ // Create the container if not already exist
45
+ await container . CreateIfNotExistsAsync ( ) ;
53
46
54
- return await Task . FromResult ( fileName ) ;
55
- }
47
+ // Get the reference to the block blob from the container
48
+ CloudBlockBlob blockBlob = container . GetBlockBlobReference ( fileName ) ;
56
49
57
- }
50
+ // Upload the file
51
+ await blockBlob . UploadFromStreamAsync ( fileStream ) ;
58
52
59
- return await Task . FromResult ( string . Empty ) ;
53
+ return await Task . FromResult ( true ) ;
60
54
}
55
+
61
56
public static async Task < bool > CreateQueueItem ( string imageInfo , AzureStorageConfig _storageConfig )
62
57
{
63
- if ( _storageConfig != null )
64
- {
65
- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
58
+ // Create storagecredentials object by reading the values from the configuration (appsettings.json)
59
+ StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
66
60
67
- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
61
+ // Create cloudstorage account by passing the storagecredentials
62
+ CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
68
63
69
- if ( storageAccount != null && imageInfo != string . Empty )
70
- {
71
- // Create the queue client.
72
- CloudQueueClient queueClient = storageAccount . CreateCloudQueueClient ( ) ;
64
+ // Create the queue client.
65
+ CloudQueueClient queueClient = storageAccount . CreateCloudQueueClient ( ) ;
73
66
74
- // Retrieve a reference to a queue.
75
- CloudQueue queue = queueClient . GetQueueReference ( _storageConfig . QueueName ) ;
67
+ // Retrieve a reference to a queue.
68
+ CloudQueue queue = queueClient . GetQueueReference ( _storageConfig . QueueName ) ;
76
69
77
- // Create the queue if it doesn't already exist.
78
- await queue . CreateIfNotExistsAsync ( ) ;
70
+ // Create the queue if it doesn't already exist.
71
+ await queue . CreateIfNotExistsAsync ( ) ;
79
72
80
- // Create a message and add it to the queue.
81
- CloudQueueMessage message = new CloudQueueMessage ( imageInfo ) ;
73
+ // Create a message and add it to the queue.
74
+ CloudQueueMessage message = new CloudQueueMessage ( imageInfo ) ;
82
75
83
- await queue . AddMessageAsync ( message ) ;
76
+ // Add a message to the queue
77
+ await queue . AddMessageAsync ( message ) ;
84
78
85
- return await Task . FromResult ( true ) ;
86
- }
87
-
88
- }
89
- return await Task . FromResult ( false ) ;
79
+ return await Task . FromResult ( true ) ;
90
80
}
81
+
91
82
public static async Task < List < string > > GetThumbNailUrls ( AzureStorageConfig _storageConfig )
92
83
{
93
84
List < string > thumbnailUrls = new List < string > ( ) ;
94
- try
95
- {
96
- StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
97
85
98
- CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
86
+ // Create storagecredentials object by reading the values from the configuration (appsettings.json)
87
+ StorageCredentials storageCredentials = new StorageCredentials ( _storageConfig . AccountName , _storageConfig . AccountKey ) ;
99
88
100
- CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
89
+ // Create cloudstorage account by passing the storagecredentials
90
+ CloudStorageAccount storageAccount = new CloudStorageAccount ( storageCredentials , true ) ;
101
91
102
- CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ThumbnailContainer ) ;
103
-
104
- await container . CreateIfNotExistsAsync ( ) ;
92
+ // Create blob client
93
+ CloudBlobClient blobClient = storageAccount . CreateCloudBlobClient ( ) ;
105
94
106
- await container . SetPermissionsAsync ( new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType . Blob } ) ;
95
+ // Get reference to the container
96
+ CloudBlobContainer container = blobClient . GetContainerReference ( _storageConfig . ThumbnailContainer ) ;
107
97
108
- // Loop over items within the container and output the length and URI.
109
- int i = 0 ;
98
+ // Create the container if it is not exists
99
+ await container . CreateIfNotExistsAsync ( ) ;
110
100
111
- BlobContinuationToken continuationToken = null ;
101
+ // Set the permission of the container to public
102
+ await container . SetPermissionsAsync ( new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType . Blob } ) ;
112
103
113
- BlobResultSegment resultSegment = null ;
104
+ BlobContinuationToken continuationToken = null ;
114
105
115
- //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
116
- //When the continuation token is null, the last page has been returned and execution can exit the loop.
117
- do
118
- {
119
- //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
120
- //or by calling a different overload.
121
- resultSegment = await container . ListBlobsSegmentedAsync ( "" , true , BlobListingDetails . All , 10 , continuationToken , null , null ) ;
106
+ BlobResultSegment resultSegment = null ;
122
107
123
- foreach ( var blobItem in resultSegment . Results )
124
- {
125
- thumbnailUrls . Add ( blobItem . StorageUri . PrimaryUri . ToString ( ) ) ;
126
- }
108
+ //Call ListBlobsSegmentedAsync and enumerate the result segment returned, while the continuation token is non-null.
109
+ //When the continuation token is null, the last page has been returned and execution can exit the loop.
110
+ do
111
+ {
112
+ //This overload allows control of the page size. You can return all remaining results by passing null for the maxResults parameter,
113
+ //or by calling a different overload.
114
+ resultSegment = await container . ListBlobsSegmentedAsync ( "" , true , BlobListingDetails . All , 10 , continuationToken , null , null ) ;
127
115
128
- //Get the continuation token.
129
- continuationToken = resultSegment . ContinuationToken ;
116
+ foreach ( var blobItem in resultSegment . Results )
117
+ {
118
+ thumbnailUrls . Add ( blobItem . StorageUri . PrimaryUri . ToString ( ) ) ;
130
119
}
131
- while ( continuationToken != null ) ;
132
120
133
- return await Task . FromResult ( thumbnailUrls ) ;
134
- }
135
- catch ( Exception ex )
136
- {
137
- throw ex ;
121
+ //Get the continuation token.
122
+ continuationToken = resultSegment . ContinuationToken ;
138
123
}
124
+
125
+ while ( continuationToken != null ) ;
126
+
127
+ return await Task . FromResult ( thumbnailUrls ) ;
139
128
}
140
129
}
141
130
}
0 commit comments