@@ -220,61 +220,65 @@ public static AzureFileSystem GetInstance(string containerName, string rootUrl,
220220 public void AddFile ( string path , Stream stream , bool overrideIfExists )
221221 {
222222 CloudBlockBlob blockBlob = this . GetBlockBlobReference ( path ) ;
223- bool exists = blockBlob . Exists ( ) ;
224- DateTimeOffset created = DateTimeOffset . MinValue ;
225223
226- if ( ! overrideIfExists && exists )
224+ if ( blockBlob != null )
227225 {
228- InvalidOperationException error = new InvalidOperationException ( $ "File already exists at { blockBlob . Uri } ") ;
229- this . LogHelper . Error < AzureBlobFileSystem > ( $ "File already exists at { path } ", error ) ;
230- return ;
231- }
226+ bool exists = blockBlob . Exists ( ) ;
227+ DateTimeOffset created = DateTimeOffset . MinValue ;
232228
233- try
234- {
235- if ( exists )
229+ if ( ! overrideIfExists && exists )
236230 {
237- // Ensure original created date is preserved.
238- blockBlob . FetchAttributes ( ) ;
239- if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
231+ InvalidOperationException error = new InvalidOperationException ( $ "File already exists at { blockBlob . Uri } ") ;
232+ this . LogHelper . Error < AzureBlobFileSystem > ( $ "File already exists at { path } ", error ) ;
233+ return ;
234+ }
235+
236+ try
237+ {
238+ if ( exists )
240239 {
241- // We store the creation date in meta data.
242- created = DateTime . Parse ( blockBlob . Metadata [ "CreatedDate" ] , CultureInfo . InvariantCulture ) . ToUniversalTime ( ) ;
240+ // Ensure original created date is preserved.
241+ blockBlob . FetchAttributes ( ) ;
242+ if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
243+ {
244+ // We store the creation date in meta data.
245+ created = DateTime . Parse ( blockBlob . Metadata [ "CreatedDate" ] , CultureInfo . InvariantCulture ) . ToUniversalTime ( ) ;
246+ }
243247 }
244- }
245248
246- blockBlob . UploadFromStream ( stream ) ;
249+ blockBlob . UploadFromStream ( stream ) ;
247250
248- string contentType = this . MimeTypeResolver . Resolve ( path ) ;
251+ string contentType = this . MimeTypeResolver . Resolve ( path ) ;
249252
250- if ( ! string . IsNullOrWhiteSpace ( contentType ) )
251- {
252- blockBlob . Properties . ContentType = contentType ;
253- }
253+ if ( ! string . IsNullOrWhiteSpace ( contentType ) )
254+ {
255+ blockBlob . Properties . ContentType = contentType ;
256+ }
254257
255- blockBlob . Properties . CacheControl = $ "public, max-age={ this . MaxDays * 86400 } ";
256- blockBlob . SetProperties ( ) ;
258+ blockBlob . Properties . CacheControl = $ "public, max-age={ this . MaxDays * 86400 } ";
259+ blockBlob . SetProperties ( ) ;
257260
258- if ( created == DateTimeOffset . MinValue )
259- {
260- created = DateTimeOffset . UtcNow ;
261- }
261+ if ( created == DateTimeOffset . MinValue )
262+ {
263+ created = DateTimeOffset . UtcNow ;
264+ }
262265
263- // Store the creation date in meta data.
264- if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
265- {
266- blockBlob . Metadata [ "CreatedDate" ] = created . ToString ( CultureInfo . InvariantCulture ) ;
266+ // Store the creation date in meta data.
267+ if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
268+ {
269+ blockBlob . Metadata [ "CreatedDate" ] = created . ToString ( CultureInfo . InvariantCulture ) ;
270+ }
271+ else
272+ {
273+ blockBlob . Metadata . Add ( "CreatedDate" , created . ToString ( CultureInfo . InvariantCulture ) ) ;
274+ }
275+
276+ blockBlob . SetMetadata ( ) ;
267277 }
268- else
278+ catch ( Exception ex )
269279 {
270- blockBlob . Metadata . Add ( "CreatedDate ", created . ToString ( CultureInfo . InvariantCulture ) ) ;
280+ this . LogHelper . Error < AzureBlobFileSystem > ( $ "Unable to upload file at { path } ", ex ) ;
271281 }
272-
273- blockBlob . SetMetadata ( ) ;
274- }
275- catch ( Exception ex )
276- {
277- this . LogHelper . Error < AzureBlobFileSystem > ( $ "Unable to upload file at { path } ", ex ) ;
278282 }
279283 }
280284
@@ -362,13 +366,16 @@ public void DeleteFile(string path)
362366 {
363367 CloudBlockBlob blockBlob = this . GetBlockBlobReference ( path ) ;
364368
365- try
369+ if ( blockBlob != null )
366370 {
367- blockBlob . DeleteIfExists ( DeleteSnapshotsOption . IncludeSnapshots ) ;
368- }
369- catch ( Exception ex )
370- {
371- this . LogHelper . Error < AzureBlobFileSystem > ( $ "Unable to delete file at { path } ", ex ) ;
371+ try
372+ {
373+ blockBlob . DeleteIfExists ( DeleteSnapshotsOption . IncludeSnapshots ) ;
374+ }
375+ catch ( Exception ex )
376+ {
377+ this . LogHelper . Error < AzureBlobFileSystem > ( $ "Unable to delete file at { path } ", ex ) ;
378+ }
372379 }
373380 }
374381
@@ -396,7 +403,8 @@ public bool DirectoryExists(string path)
396403 /// </returns>
397404 public bool FileExists ( string path )
398405 {
399- return this . GetBlockBlobReference ( path ) . Exists ( ) ;
406+ CloudBlockBlob blockBlobReference = this . GetBlockBlobReference ( path ) ;
407+ return blockBlobReference ? . Exists ( ) ?? false ;
400408 }
401409
402410 /// <summary>
@@ -410,12 +418,15 @@ public DateTimeOffset GetCreated(string path)
410418 {
411419 CloudBlockBlob blockBlob = this . GetBlockBlobReference ( path ) ;
412420
413- // Populate the blob's attributes.
414- blockBlob . FetchAttributes ( ) ;
415- if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
421+ if ( blockBlob != null )
416422 {
417- // We store the creation date in meta data.
418- return DateTimeOffset . Parse ( blockBlob . Metadata [ "CreatedDate" ] , CultureInfo . InvariantCulture ) . ToUniversalTime ( ) ;
423+ // Populate the blob's attributes.
424+ blockBlob . FetchAttributes ( ) ;
425+ if ( blockBlob . Metadata . ContainsKey ( "CreatedDate" ) )
426+ {
427+ // We store the creation date in meta data.
428+ return DateTimeOffset . Parse ( blockBlob . Metadata [ "CreatedDate" ] , CultureInfo . InvariantCulture ) . ToUniversalTime ( ) ;
429+ }
419430 }
420431
421432 return DateTimeOffset . MinValue ;
@@ -504,12 +515,18 @@ public string GetFullPath(string path)
504515 public DateTimeOffset GetLastModified ( string path )
505516 {
506517 CloudBlockBlob blockBlob = this . GetBlockBlobReference ( path ) ;
507- blockBlob . FetchAttributes ( ) ;
508- return blockBlob . Properties . LastModified . GetValueOrDefault ( ) ;
518+
519+ if ( blockBlob != null )
520+ {
521+ blockBlob . FetchAttributes ( ) ;
522+ return blockBlob . Properties . LastModified . GetValueOrDefault ( ) ;
523+ }
524+
525+ return DateTimeOffset . MinValue ;
509526 }
510527
511528 /// <summary>
512- /// Returns the relative path to the media item .
529+ /// Returns the application relative path to the file .
513530 /// </summary>
514531 /// <param name="fullPathOrUrl">The full path or url.</param>
515532 /// <returns>
@@ -521,7 +538,7 @@ public string GetRelativePath(string fullPathOrUrl)
521538 }
522539
523540 /// <summary>
524- /// Returns the url to the media item .
541+ /// Returns the application relative url to the file .
525542 /// </summary>
526543 /// <remarks>If the virtual path provider is enabled this returns a relative url.</remarks>
527544 /// <param name="path">The path to return the url for.</param>
@@ -539,32 +556,36 @@ public string GetUrl(string path)
539556 }
540557
541558 /// <summary>
542- /// Gets a <see cref="Stream"/> containing the contains of the given file .
559+ /// Gets a <see cref="Stream"/> representing the file at the gieven path .
543560 /// </summary>
544561 /// <param name="path">The path to the file.</param>
545562 /// <returns>
546563 /// <see cref="Stream"/>.
547564 /// </returns>
548565 public Stream OpenFile ( string path )
549566 {
550- // TODO: Caching?
551567 CloudBlockBlob blockBlob = this . GetBlockBlobReference ( path ) ;
552568
553- if ( ! blockBlob . Exists ( ) )
569+ if ( blockBlob != null )
554570 {
555- this . LogHelper . Info < AzureBlobFileSystem > ( $ "No file exists at { path } .") ;
556- return null ;
557- }
571+ if ( ! blockBlob . Exists ( ) )
572+ {
573+ this . LogHelper . Info < AzureBlobFileSystem > ( $ "No file exists at { path } .") ;
574+ return null ;
575+ }
558576
559- MemoryStream stream = new MemoryStream ( ) ;
560- blockBlob . DownloadToStream ( stream ) ;
577+ MemoryStream stream = new MemoryStream ( ) ;
578+ blockBlob . DownloadToStream ( stream ) ;
561579
562- if ( stream . CanSeek )
563- {
564- stream . Seek ( 0 , SeekOrigin . Begin ) ;
580+ if ( stream . CanSeek )
581+ {
582+ stream . Seek ( 0 , SeekOrigin . Begin ) ;
583+ }
584+
585+ return stream ;
565586 }
566587
567- return stream ;
588+ return null ;
568589 }
569590
570591 /// <summary>
@@ -601,7 +622,12 @@ private static CloudBlobContainer CreateContainer(CloudBlobClient cloudBlobClien
601622 private CloudBlockBlob GetBlockBlobReference ( string path )
602623 {
603624 string blobPath = this . FixPath ( path ) ;
604- return this . cloudBlobContainer . GetBlockBlobReference ( blobPath ) ;
625+
626+ // Only make the request if there is an actual path. See issue 8.
627+ // https://github.com/JimBobSquarePants/UmbracoFileSystemProviders.Azure/issues/8
628+ return ! string . IsNullOrWhiteSpace ( path )
629+ ? this . cloudBlobContainer . GetBlockBlobReference ( blobPath )
630+ : null ;
605631 }
606632
607633 /// <summary>
0 commit comments