From 3c1e1b2cc651c237d735b565775db2813e9b3ab4 Mon Sep 17 00:00:00 2001 From: Ron DeFreitas Date: Fri, 11 Sep 2015 17:36:25 -0400 Subject: [PATCH] GetSharedAccessPath would not return due to UTC / DateTimeOffset issue. (had no test coverage) --- .../StorageUniversalTest.cs | 39 +++++++++++++++++++ .../AzureBlobStorageProvider.cs | 8 ++-- .../FileSystemStorageProvider.cs | 4 +- AzureBlobFileSystem/IStorageFile.cs | 2 +- AzureBlobFileSystem/IStorageProvider.cs | 9 +++++ 5 files changed, 57 insertions(+), 5 deletions(-) diff --git a/AzureBlobFileSystem.Test/StorageUniversalTest.cs b/AzureBlobFileSystem.Test/StorageUniversalTest.cs index 888e24b..e2c68e5 100644 --- a/AzureBlobFileSystem.Test/StorageUniversalTest.cs +++ b/AzureBlobFileSystem.Test/StorageUniversalTest.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.IO; using System.Linq; using NUnit.Framework; @@ -206,5 +207,43 @@ public void renameFolder_should_mode_inner_files() Assert.AreEqual("f1", SUT.ListFiles(folder + "2").Single().GetName()); } + + [Test] + public void sharedPath_should_return_when_given_offset() + { + var folder = SUT.Combine(TEST_FOLDER, "folder"); + SUT.CreateFolder(folder); + + SUT.CreateFile(SUT.Combine(folder, "f1")); + + var offset = DateTimeOffset.MaxValue; + + Assert.DoesNotThrow(() => + { + var file = SUT.ListFiles(folder).First(); + var path = file.GetPath(); + var sharedPath = file.GetSharedAccessPath(offset); + Trace.WriteLine(path); + Trace.WriteLine(sharedPath); + }); + } + + [Test] + public void sharedPath_should_return_when_not_given_offset() + { + var folder = SUT.Combine(TEST_FOLDER, "folder"); + SUT.CreateFolder(folder); + + SUT.CreateFile(SUT.Combine(folder, "f1")); + + Assert.DoesNotThrow(() => + { + var file = SUT.ListFiles(folder).First(); + var path = file.GetPath(); + var sharedPath = file.GetSharedAccessPath(); + Trace.WriteLine(path); + Trace.WriteLine(sharedPath); + }); + } } } \ No newline at end of file diff --git a/AzureBlobFileSystem/AzureBlobStorageProvider.cs b/AzureBlobFileSystem/AzureBlobStorageProvider.cs index 098d245..4a27743 100644 --- a/AzureBlobFileSystem/AzureBlobStorageProvider.cs +++ b/AzureBlobFileSystem/AzureBlobStorageProvider.cs @@ -103,6 +103,8 @@ public bool FileExists(string path) return container.BlobExists(path); } + public DateTimeOffset? DefaultSharedAccessExpiration { get; set; } + public IEnumerable ListFiles(string path) { path = path ?? String.Empty; @@ -417,12 +419,12 @@ public string GetFileType() { return Path.GetExtension(GetPath()); } - - public string GetSharedAccessPath() + + public string GetSharedAccessPath(DateTimeOffset? expiration = null) { return _blob.Uri.AbsoluteUri + _blob.GetSharedAccessSignature(new SharedAccessBlobPolicy() { - SharedAccessExpiryTime = DateTime.MaxValue + SharedAccessExpiryTime = expiration ?? _azureFileSystem.DefaultSharedAccessExpiration }, "default"); } diff --git a/AzureBlobFileSystem/FileSystemStorageProvider.cs b/AzureBlobFileSystem/FileSystemStorageProvider.cs index e0e7171..ad6cac8 100644 --- a/AzureBlobFileSystem/FileSystemStorageProvider.cs +++ b/AzureBlobFileSystem/FileSystemStorageProvider.cs @@ -306,6 +306,8 @@ public bool FileExists(string path) return new FileInfo(MapStorage(path)).Exists; } + public DateTimeOffset? DefaultSharedAccessExpiration { get; set; } + public IStorageFile CreateOrReplaceFile(string path) { if (FileExists(path)) @@ -359,7 +361,7 @@ public string GetFileType() return _fileInfo.Extension; } - public string GetSharedAccessPath() + public string GetSharedAccessPath(DateTimeOffset? expiration = null) { return _path; } diff --git a/AzureBlobFileSystem/IStorageFile.cs b/AzureBlobFileSystem/IStorageFile.cs index 663adfc..157202c 100644 --- a/AzureBlobFileSystem/IStorageFile.cs +++ b/AzureBlobFileSystem/IStorageFile.cs @@ -10,7 +10,7 @@ public interface IStorageFile long GetSize(); DateTime GetLastUpdated(); string GetFileType(); - string GetSharedAccessPath(); + string GetSharedAccessPath(DateTimeOffset? expiration = null); /// /// Creates a stream for reading from the file. diff --git a/AzureBlobFileSystem/IStorageProvider.cs b/AzureBlobFileSystem/IStorageProvider.cs index a3dca17..9b51837 100644 --- a/AzureBlobFileSystem/IStorageProvider.cs +++ b/AzureBlobFileSystem/IStorageProvider.cs @@ -103,5 +103,14 @@ public interface IStorageProvider string Combine(string path1, string path2); bool FileExists(string path); + + + /// + /// Gets or sets the default shared access expiration date. + /// + /// + /// The default shared access expiration date. + /// + DateTimeOffset? DefaultSharedAccessExpiration { get; set; } } } \ No newline at end of file