Skip to content

Commit

Permalink
Adding support for serving blobs via CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Aug 13, 2012
1 parent bb996f6 commit f860133
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Facts/Services/CloudBlobFileStorageServiceFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -390,7 +390,7 @@ static CloudBlobFileStorageService CreateService(
if (fakeBlobClient == null)
fakeBlobClient = new Mock<ICloudBlobClient>();

return new CloudBlobFileStorageService(fakeBlobClient.Object);
return new CloudBlobFileStorageService(fakeBlobClient.Object, Mock.Of<IConfiguration>());
}
}
}
8 changes: 8 additions & 0 deletions Website/App_Start/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public PackageStoreType PackageStoreType
}
}

public string AzureCdnHost
{
get
{
return ReadAppSettings("AzureCdnHost");
}
}

protected virtual string GetConfiguredSiteRoot()
{
return ReadAppSettings("SiteRoot");
Expand Down
1 change: 1 addition & 0 deletions Website/App_Start/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 22 additions & 4 deletions Website/Services/CloudBlobFileStorageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,7 +38,9 @@ public ActionResult CreateDownloadFileActionResult(
{
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(
Expand Down Expand Up @@ -133,5 +137,19 @@ public void SaveFile(
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;
}
}
}
1 change: 1 addition & 0 deletions Website/Web.config
Original file line number Diff line number Diff line change
Expand Up @@ -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="" />
Expand Down

0 comments on commit f860133

Please sign in to comment.