@@ -28,9 +28,7 @@ public class CephDocumentServices : IDocumentService
2828 public CephDocumentServices ( ILogger < CephDocumentServices > logger , IConfiguration configuration )
2929 {
3030 _logger = logger ;
31-
3231 _lazySettings = new Lazy < CephDocumentServicesSettings > ( ( ) => GetSettings ( configuration ) ) ;
33-
3432 _lazyClient = new Lazy < AmazonS3Client > ( GetClient ) ;
3533 }
3634
@@ -58,23 +56,30 @@ public async Task<Document> AddDocumentAsync(
5856 if ( metadata == null || string . IsNullOrEmpty ( metadata . Rsin ) )
5957 throw new InvalidOperationException ( "Rsin is required to be filled in metadata" ) ;
6058
61- TempFileHelper . AssureNotTampered ( contentFile ) ;
59+ var validatedContentFile = TempFileHelper . GetValidatedPath ( contentFile ) ;
6260
63- if ( ! File . Exists ( contentFile ) )
64- throw new InvalidOperationException ( $ "Content file '{ contentFile } ' does not exist.") ;
61+ if ( ! File . Exists ( validatedContentFile ) )
62+ throw new InvalidOperationException ( $ "Content file '{ validatedContentFile } ' does not exist.") ;
6563
66- string bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
64+ var bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
6765
6866 await EnsureBucketExistsAsync ( bucket , cancellationToken ) ;
6967
7068 var key = $ "{ Guid . NewGuid ( ) } ";
7169
7270 try
7371 {
74- using FileStream content = new FileStream ( contentFile , FileMode . Open , FileAccess . Read , FileShare . Read , bufferSize , useAsync : true ) ; // When using 'useAsync: true' you get better performance with buffers much larger than the default 4096 bytes.
75- long documentSize = content . Length ;
76-
77- _logger . LogDebug ( "Streaming data from {contentFile} to AmazonS3 object using key {key}..." , contentFile , key ) ;
72+ await using var content = new FileStream (
73+ validatedContentFile ,
74+ FileMode . Open ,
75+ FileAccess . Read ,
76+ FileShare . Read ,
77+ bufferSize ,
78+ useAsync : true
79+ ) ; // When using 'useAsync: true' you get better performance with buffers much larger than the default 4096 bytes.
80+ var documentSize = content . Length ;
81+
82+ _logger . LogDebug ( "Streaming data from {validatedContentFile} to AmazonS3 object using key {key}..." , validatedContentFile , key ) ;
7883
7984 var request = new PutObjectRequest
8085 {
@@ -85,7 +90,7 @@ public async Task<Document> AddDocumentAsync(
8590
8691 request . Metadata . Add ( metadata , documentName ) ;
8792
88- using ( request . InputStream = content )
93+ await using ( request . InputStream = content )
8994 {
9095 _logger . LogDebug ( "Writing document '{documentName}' to bucket '{bucket}'..." , documentName , bucket ) ;
9196
@@ -104,7 +109,7 @@ public async Task<Document> AddDocumentAsync(
104109 {
105110 if ( deleteContentFile )
106111 {
107- File . Delete ( contentFile ) ;
112+ File . Delete ( validatedContentFile ) ;
108113 }
109114 }
110115 }
@@ -125,13 +130,13 @@ public async Task<Document> AddDocumentAsync(
125130 if ( metadata == null || string . IsNullOrEmpty ( metadata . Rsin ) )
126131 throw new InvalidOperationException ( "Rsin is required to be filled in metadata" ) ;
127132
128- string bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
133+ var bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
129134
130135 await EnsureBucketExistsAsync ( bucket , cancellationToken ) ;
131136
132137 var key = $ "{ Guid . NewGuid ( ) } ";
133138
134- long length = content . Length ;
139+ var length = content . Length ;
135140
136141 var request = new PutObjectRequest
137142 {
@@ -160,7 +165,7 @@ public async Task DeleteDocumentAsync(DocumentUrn urn, CancellationToken cancell
160165
161166 if ( await DocumentExistsAsync ( urn , cancellationToken ) )
162167 {
163- DeleteObjectRequest request = new DeleteObjectRequest { BucketName = urn . Name , Key = urn . ObjectId } ;
168+ var request = new DeleteObjectRequest { BucketName = urn . Name , Key = urn . ObjectId } ;
164169
165170 await Client . DeleteObjectAsync ( request , cancellationToken ) ;
166171 }
@@ -213,7 +218,7 @@ public async Task<MultiPartDocument> InitiateMultipartUploadAsync(
213218 if ( metadata == null || string . IsNullOrEmpty ( metadata . Rsin ) )
214219 throw new InvalidOperationException ( "Rsin is required to be filled in metadata" ) ;
215220
216- string bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
221+ var bucket = GetOrGenerateBucketName ( metadata . Rsin ) ;
217222
218223 await EnsureBucketExistsAsync ( bucket , cancellationToken ) ;
219224
@@ -230,7 +235,7 @@ public async Task<MultiPartDocument> InitiateMultipartUploadAsync(
230235
231236 var response = await Client . InitiateMultipartUploadAsync ( request , cancellationToken ) ;
232237
233- InternalMultiPartDocument internalMultiPartDocument = new InternalMultiPartDocument ( response . UploadId , bucket , key ) ;
238+ var internalMultiPartDocument = new InternalMultiPartDocument ( response . UploadId , bucket , key ) ;
234239
235240 return ToContext ( internalMultiPartDocument ) ;
236241 }
@@ -245,7 +250,7 @@ public async Task<IUploadPart> TryUploadPartAsync(
245250 {
246251 LastError = ( "" , DocumentError . None ) ;
247252
248- InternalMultiPartDocument internalMultiPartDocument = FromContext ( multipPartDocument ) ;
253+ var internalMultiPartDocument = FromContext ( multipPartDocument ) ;
249254
250255 if ( content . Length != size )
251256 {
@@ -282,7 +287,7 @@ public async Task<Document> CompleteMultipartUploadAsync(
282287 {
283288 LastError = ( "" , DocumentError . None ) ;
284289
285- InternalMultiPartDocument internalMultiPartDocument = FromContext ( multipPartDocument ) ;
290+ var internalMultiPartDocument = FromContext ( multipPartDocument ) ;
286291
287292 var parts = uploadparts . Select ( MapUploadPart ) . ToList ( ) ;
288293
@@ -300,7 +305,7 @@ public async Task<Document> CompleteMultipartUploadAsync(
300305
301306 var documentUrn = new DocumentUrn ( ProviderPrefix , internalMultiPartDocument . Name , internalMultiPartDocument . Key ) ;
302307
303- long length = parts . Sum ( p => p . Size ) ;
308+ var length = parts . Sum ( p => p . Size ) ;
304309
305310 return new Document ( documentUrn , length ) ;
306311 }
@@ -311,7 +316,7 @@ public async Task<bool> AbortMultipartUploadAsync(IMultiPartDocument multipPartD
311316
312317 try
313318 {
314- InternalMultiPartDocument internalMultiPartDocument = FromContext ( multipPartDocument ) ;
319+ var internalMultiPartDocument = FromContext ( multipPartDocument ) ;
315320
316321 var request = new AbortMultipartUploadRequest
317322 {
@@ -429,20 +434,20 @@ private AmazonS3Client GetClient()
429434
430435 private string GetOrGenerateBucketName ( string rsin )
431436 {
432- string buckettemplate = Settings . Bucket ?? "{yyyyMM}" ; // Note: When not specified default is: store buckets per year+month
437+ var buckettemplate = Settings . Bucket ?? "{yyyyMM}" ; // Note: When not specified default is: store buckets per year+month
433438
434439 buckettemplate = buckettemplate . Replace ( "{rsin}" , rsin ) ;
435440
436- int posTmplL = buckettemplate . IndexOf ( '{' ) ;
437- int posTmplR = buckettemplate . IndexOf ( '}' ) ;
441+ var posTmplL = buckettemplate . IndexOf ( '{' ) ;
442+ var posTmplR = buckettemplate . IndexOf ( '}' ) ;
438443
439444 string bucket ;
440445 if ( posTmplL > - 1 && posTmplR > - 1 && posTmplL < posTmplR )
441446 {
442447 var template = buckettemplate . Substring ( posTmplL + 1 , posTmplR - posTmplL - 1 ) ;
443448
444- string leftPart = buckettemplate . Substring ( 0 , posTmplL ) ;
445- string rightPart = buckettemplate . Substring ( posTmplR + 1 ) ;
449+ var leftPart = buckettemplate . Substring ( 0 , posTmplL ) ;
450+ var rightPart = buckettemplate . Substring ( posTmplR + 1 ) ;
446451
447452 bucket = leftPart + DateTime . UtcNow . ToString ( template ) + rightPart ;
448453 }
@@ -462,7 +467,7 @@ private string GetOrGenerateBucketName(string rsin)
462467
463468 private static CephDocumentServicesSettings GetSettings ( IConfiguration configuration )
464469 {
465- string key = "CephDocumentServicesSettings" ;
470+ var key = "CephDocumentServicesSettings" ;
466471
467472 var settings =
468473 configuration . GetSection ( key ) . Get < CephDocumentServicesSettings > ( )
0 commit comments