88using Microsoft . Extensions . Configuration ;
99using static TryMudBlazor . Server . Utilities . SnippetsEncoder ;
1010
11- namespace TryMudBlazor . Server . Controllers
11+ namespace TryMudBlazor . Server . Controllers ;
12+
13+ [ Route ( "api/[controller]" ) ]
14+ [ ApiController ]
15+ public class SnippetsController : ControllerBase
1216{
13- [ Route ( "api/[controller]" ) ]
14- [ ApiController ]
15- public class SnippetsController : ControllerBase
17+ private readonly BlobContainerClient _containerClient ;
18+
19+ public SnippetsController ( IConfiguration config )
1620 {
17- private readonly IConfiguration _config ;
18- private readonly BlobContainerClient containerClient ;
19- public SnippetsController ( IConfiguration config )
20- {
21- _config = config ;
22- var containerUri = new Uri ( _config [ "SnippetsContainerUrl" ] ) ;
23- string accessKey = _config [ "SnippetsAccessKey" ] ;
24- if ( accessKey == "secret" )
25- {
26- var defaultAzureCredentialOptions = new DefaultAzureCredentialOptions ( ) ;
27- defaultAzureCredentialOptions . ManagedIdentityClientId = _config [ "ManagedCredentialsId" ] ;
28- containerClient = new BlobContainerClient ( containerUri ,
29- new DefaultAzureCredential ( defaultAzureCredentialOptions ) ) ;
30- }
31- else
32- {
33- var blobUri = new BlobUriBuilder ( containerUri ) ;
34- var acccountName = blobUri . AccountName ;
35- var key = new StorageSharedKeyCredential ( acccountName , accessKey ) ;
36- containerClient = new BlobContainerClient ( containerUri , key ) ;
37- }
38- }
21+ var snippetsContainerUrl = config [ "SnippetsContainerUrl" ] ;
22+ var accessKey = config [ "SnippetsAccessKey" ] ;
3923
40- [ HttpGet ( "{snippetId}" ) ]
41- public async Task < IActionResult > Get ( string snippetId )
24+ if ( string . IsNullOrEmpty ( snippetsContainerUrl ) || string . IsNullOrEmpty ( accessKey ) )
4225 {
43- snippetId = DecodeSnippetId ( snippetId ) ;
44- var blob = containerClient . GetBlobClient ( BlobPath ( snippetId ) ) ;
45- var response = await blob . DownloadAsync ( ) ;
46- var zipStream = new MemoryStream ( ) ;
47- await response . Value . Content . CopyToAsync ( zipStream ) ;
48- zipStream . Position = 0 ;
49- return File ( zipStream , "application/octet-stream" , "snippet.zip" ) ;
26+ throw new Exception ( "Please configure SnippetsContainerUrl and SnippetsAccessKey in appsettings.json" ) ;
5027 }
5128
52- [ HttpPost ]
53- public async Task < IActionResult > Post ( )
54- {
55- var newSnippetId = NewSnippetId ( ) ;
56- await containerClient . UploadBlobAsync ( BlobPath ( newSnippetId ) , Request . Body ) ;
57- return Ok ( EncodeSnippetId ( newSnippetId ) ) ;
58- }
29+ var containerUri = new Uri ( snippetsContainerUrl ) ;
5930
60- private static string NewSnippetId ( )
31+ if ( accessKey == "secret" )
6132 {
62- var yearFolder = DateTime . Now . Year ;
63- var monthFolder = DateTime . Now . Month ;
64- var dayFolder = DateTime . Now . Day ;
65- var time = Convert . ToInt32 ( DateTime . Now . TimeOfDay . TotalMilliseconds ) ;
66- var snippetTime = $ " { time : D8 } " ;
67- return $ " { yearFolder : 0000 } { monthFolder : 00 } { dayFolder : 00 } { snippetTime : D8 } " ;
33+ var defaultAzureCredentialOptions = new DefaultAzureCredentialOptions
34+ {
35+ ManagedIdentityClientId = config [ "ManagedCredentialsId" ]
36+ } ;
37+ _containerClient = new BlobContainerClient ( containerUri ,
38+ new DefaultAzureCredential ( defaultAzureCredentialOptions ) ) ;
6839 }
69-
70- private static string BlobPath ( string snippetId )
40+ else
7141 {
72- var yearFolder = snippetId . Substring ( 0 , 4 ) ;
73- var monthFolder = snippetId . Substring ( 4 , 2 ) ;
74- var dayFolder = snippetId . Substring ( 6 , 2 ) ;
75- var time = snippetId . Substring ( 8 ) ;
76- var snippetFolder = $ "{ yearFolder : 0000} /{ monthFolder : 00} /{ dayFolder : 00} ";
77- var snippetTime = $ "{ time : 00000000} ";
78- return $ "{ snippetFolder } /{ snippetTime } ";
42+ var blobUri = new BlobUriBuilder ( containerUri ) ;
43+ var accountName = blobUri . AccountName ;
44+ var key = new StorageSharedKeyCredential ( accountName , accessKey ) ;
45+ _containerClient = new BlobContainerClient ( containerUri , key ) ;
7946 }
8047 }
81- }
48+
49+ [ HttpGet ( "{snippetId}" ) ]
50+ public async Task < IActionResult > Get ( string snippetId )
51+ {
52+ snippetId = DecodeSnippetId ( snippetId ) ;
53+ var blob = _containerClient . GetBlobClient ( BlobPath ( snippetId ) ) ;
54+ var response = await blob . DownloadAsync ( ) ;
55+ var zipStream = new MemoryStream ( ) ;
56+ await response . Value . Content . CopyToAsync ( zipStream ) ;
57+ zipStream . Position = 0 ;
58+
59+ return File ( zipStream , "application/octet-stream" , "snippet.zip" ) ;
60+ }
61+
62+ [ HttpPost ]
63+ public async Task < IActionResult > Post ( )
64+ {
65+ var newSnippetId = NewSnippetId ( ) ;
66+ await _containerClient . UploadBlobAsync ( BlobPath ( newSnippetId ) , Request . Body ) ;
67+
68+ return Ok ( EncodeSnippetId ( newSnippetId ) ) ;
69+ }
70+
71+ private static string NewSnippetId ( )
72+ {
73+ var yearFolder = DateTime . Now . Year ;
74+ var monthFolder = DateTime . Now . Month ;
75+ var dayFolder = DateTime . Now . Day ;
76+ var time = Convert . ToInt32 ( DateTime . Now . TimeOfDay . TotalMilliseconds ) ;
77+ var snippetTime = $ "{ time : D8} ";
78+
79+ return $ "{ yearFolder : 0000} { monthFolder : 00} { dayFolder : 00} { snippetTime : D8} ";
80+ }
81+
82+ private static string BlobPath ( string snippetId )
83+ {
84+ var yearFolder = snippetId . Substring ( 0 , 4 ) ;
85+ var monthFolder = snippetId . Substring ( 4 , 2 ) ;
86+ var dayFolder = snippetId . Substring ( 6 , 2 ) ;
87+ var time = snippetId . Substring ( 8 ) ;
88+ var snippetFolder = $ "{ yearFolder : 0000} /{ monthFolder : 00} /{ dayFolder : 00} ";
89+ var snippetTime = $ "{ time : 00000000} ";
90+
91+ return $ "{ snippetFolder } /{ snippetTime } ";
92+ }
93+ }
0 commit comments