Permalink
Browse files

Adding support for serving blobs via CDN

  • Loading branch information...
1 parent bb996f6 commit f8601338af1a23d3f65222e231ec2d937ab196b7 @pranavkm pranavkm committed with pranavkm Jun 27, 2012
@@ -86,7 +86,7 @@ public void WillReturnARedirectResultToTheBlobUri()
var result = service.CreateDownloadFileActionResult(Constants.PackagesFolderName, "theFileName") as RedirectResult;
Assert.NotNull(result);
- Assert.Equal("http://theuri/", result.Url);
+ Assert.Equal("http://theUri", result.Url);
}
}
@@ -390,7 +390,7 @@ public FolderNamesDataAttribute(bool includePermissions = false)
if (fakeBlobClient == null)
fakeBlobClient = new Mock<ICloudBlobClient>();
- return new CloudBlobFileStorageService(fakeBlobClient.Object);
+ return new CloudBlobFileStorageService(fakeBlobClient.Object, Mock.Of<IConfiguration>());
}
}
}
@@ -82,6 +82,14 @@ public PackageStoreType PackageStoreType
}
}
+ public string AzureCdnHost
+ {
+ get
+ {
+ return ReadAppSettings("AzureCdnHost");
+ }
+ }
+
protected virtual string GetConfiguredSiteRoot()
{
return ReadAppSettings("SiteRoot");
@@ -7,6 +7,7 @@ public interface IConfiguration
string AzureStorageAccountName { get; }
string AzureStorageBlobUrl { get; }
string FileStorageDirectory { get; }
+ string AzureCdnHost { get; }
PackageStoreType PackageStoreType { get; }
string GetSiteRoot(bool useHttps);
@@ -9,12 +9,14 @@ namespace NuGetGallery
{
public class CloudBlobFileStorageService : IFileStorageService
{
- ICloudBlobClient client;
- IDictionary<string, ICloudBlobContainer> containers = new Dictionary<string, ICloudBlobContainer>();
+ private readonly ICloudBlobClient client;
+ private readonly IConfiguration configuration;
+ private readonly IDictionary<string, ICloudBlobContainer> containers = new Dictionary<string, ICloudBlobContainer>();
- public CloudBlobFileStorageService(ICloudBlobClient client)
+ public CloudBlobFileStorageService(ICloudBlobClient client, IConfiguration configuration)
{
this.client = client;
+ this.configuration = configuration;
PrepareContainer(Constants.PackagesFolderName, isPublic: true);
PrepareContainer(Constants.DownloadsFolderName, isPublic: true);
@@ -36,7 +38,9 @@ public bool AllowCachingOfPackage
{
var container = GetContainer(folderName);
var blob = container.GetBlobReference(fileName);
- return new RedirectResult(blob.Uri.ToString(), false);
+
+ var redirectUri = GetRedirectUri(blob.Uri);
+ return new RedirectResult(redirectUri.OriginalString, false);
}
public void DeleteFile(
@@ -133,5 +137,19 @@ static string GetContentType(string folderName)
blob.Properties.ContentType = GetContentType(folderName);
blob.SetProperties();
}
+
+ private Uri GetRedirectUri(Uri blobUri)
+ {
+ if (!String.IsNullOrEmpty(configuration.AzureCdnHost))
+ {
+ // If a Cdn is specified, convert the blob url to an Azure Cdn url.
+ UriBuilder builder = new UriBuilder(blobUri.Scheme, configuration.AzureCdnHost);
+ builder.Path = blobUri.AbsolutePath;
+ builder.Query = blobUri.Query;
+
+ return builder.Uri;
+ }
+ return blobUri;
+ }
}
}
View
@@ -20,6 +20,7 @@
<add key="Gallery:AzureStorageAccessKey" value="" />
<add key="Gallery:AzureStorageAccountName" value="" />
<add key="Gallery:AzureStorageBlobUrl" value="" />
+ <add key="Gallery:AzureCdnHost" value="" />
<add key="Gallery:GoogleAnalyticsPropertyId" value="" />
<add key="Gallery:PackageStoreType" value="" />
<add key="Gallery:ReleaseBranch" value="" />

0 comments on commit f860133

Please sign in to comment.