Skip to content

Commit

Permalink
Move StorePackageFileInBackupLocationAsync to NuGetGallery.Core (#5549)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Feb 28, 2018
1 parent 8eacea6 commit 0ec515d
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 204 deletions.
1 change: 1 addition & 0 deletions src/NuGetGallery.Core/CoreConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public static class CoreConstants
public const int MaxPackageIdLength = 128;

public const string PackageFileSavePathTemplate = "{0}.{1}{2}";
public const string PackageFileBackupSavePathTemplate = "{0}/{1}/{2}.{3}";

public const string NuGetPackageFileExtension = ".nupkg";

Expand Down
82 changes: 82 additions & 0 deletions src/NuGetGallery.Core/Services/CorePackageFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Web;
using NuGet.Versioning;

namespace NuGetGallery
{
Expand Down Expand Up @@ -128,6 +130,86 @@ public Task<bool> DoesValidationPackageFileExistAsync(Package package)
return _fileStorageService.FileExistsAsync(CoreConstants.ValidationFolderName, fileName);
}

public async Task StorePackageFileInBackupLocationAsync(Package package, Stream packageFile)
{
if (package == null)
{
throw new ArgumentNullException(nameof(package));
}

if (packageFile == null)
{
throw new ArgumentNullException(nameof(packageFile));
}

if (package.PackageRegistration == null ||
string.IsNullOrWhiteSpace(package.PackageRegistration.Id) ||
(string.IsNullOrWhiteSpace(package.NormalizedVersion) && string.IsNullOrWhiteSpace(package.Version)))
{
throw new ArgumentException(CoreStrings.PackageIsMissingRequiredData, nameof(package));
}

string version;
if (string.IsNullOrEmpty(package.NormalizedVersion))
{
version = NuGetVersion.Parse(package.Version).ToNormalizedString();
}
else
{
version = package.NormalizedVersion;
}

var fileName = BuildBackupFileName(
package.PackageRegistration.Id,
version,
package.Hash);

// If the package already exists, don't even bother uploading it. The file name is based off of the hash so
// we know the upload isn't necessary.
if (await _fileStorageService.FileExistsAsync(CoreConstants.PackageBackupsFolderName, fileName))
{
return;
}

try
{
await _fileStorageService.SaveFileAsync(CoreConstants.PackageBackupsFolderName, fileName, packageFile);
}
catch (InvalidOperationException)
{
// If the package file already exists, swallow the exception since we know the content is the same.
return;
}
}

private static string BuildBackupFileName(string id, string version, string hash)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}

if (version == null)
{
throw new ArgumentNullException(nameof(version));
}

if (hash == null)
{
throw new ArgumentNullException(nameof(hash));
}

var hashBytes = Convert.FromBase64String(hash);

return string.Format(
CultureInfo.InvariantCulture,
CoreConstants.PackageFileBackupSavePathTemplate,
id.ToLowerInvariant(),
version.ToLowerInvariant(),
HttpServerUtility.UrlTokenEncode(hashBytes),
CoreConstants.NuGetPackageFileExtension);
}

protected static string BuildFileName(Package package, string format, string extension)
{
if (package == null)
Expand Down
5 changes: 5 additions & 0 deletions src/NuGetGallery.Core/Services/ICorePackageFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,10 @@ public interface ICorePackageFileService
/// <param name="id">The package ID. This value is case-insensitive.</param>
/// <param name="version">The package version. This value is case-insensitive and need not be normalized.</param>
Task DeletePackageFileAsync(string id, string version);

/// <summary>
/// Copies the contents of the package represented by the stream into the file storage backup location.
/// </summary>
Task StorePackageFileInBackupLocationAsync(Package package, Stream packageFile);
}
}
2 changes: 0 additions & 2 deletions src/NuGetGallery/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public static class Constants
public const string ReadMeFileSavePathTemplateActive = "active/{0}/{1}{2}";
public const string ReadMeFileSavePathTemplatePending = "pending/{0}/{1}{2}";

public const string PackageFileBackupSavePathTemplate = "{0}/{1}/{2}.{3}";

public const string MarkdownFileExtension = ".md";
public const string HtmlFileExtension = ".html";
public const string JsonFileExtension = ".json";
Expand Down
5 changes: 0 additions & 5 deletions src/NuGetGallery/Services/IPackageFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ public interface IPackageFileService : ICorePackageFileService
/// </summary>
Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl, string unsafeId, string unsafeVersion);

/// <summary>
/// Copies the contents of the package represented by the stream into the file storage backup location.
/// </summary>
Task StorePackageFileInBackupLocationAsync(Package package, Stream packageFile);

/// <summary>
/// Deletes the package readme.md file from storage.
/// </summary>
Expand Down
52 changes: 0 additions & 52 deletions src/NuGetGallery/Services/PackageFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,6 @@ public Task<ActionResult> CreateDownloadPackageActionResultAsync(Uri requestUrl,
return _fileStorageService.CreateDownloadFileActionResultAsync(requestUrl, CoreConstants.PackagesFolderName, fileName);
}

public Task StorePackageFileInBackupLocationAsync(Package package, Stream packageFile)
{
if (package == null)
{
throw new ArgumentNullException(nameof(package));
}

if (packageFile == null)
{
throw new ArgumentNullException(nameof(packageFile));
}

if (package.PackageRegistration == null ||
string.IsNullOrWhiteSpace(package.PackageRegistration.Id) ||
(string.IsNullOrWhiteSpace(package.NormalizedVersion) && string.IsNullOrWhiteSpace(package.Version)))
{
throw new ArgumentException(CoreStrings.PackageIsMissingRequiredData, nameof(package));
}

var fileName = BuildBackupFileName(package.PackageRegistration.Id, string.IsNullOrEmpty(package.NormalizedVersion)
? NuGetVersion.Parse(package.Version).ToNormalizedString() : package.NormalizedVersion, package.Hash);
return _fileStorageService.SaveFileAsync(CoreConstants.PackageBackupsFolderName, fileName, packageFile);
}

/// <summary>
/// Deletes the package readme.md file from storage.
/// </summary>
Expand Down Expand Up @@ -126,33 +102,5 @@ public async Task<string> DownloadReadMeMdFileAsync(Package package)

return null;
}

private static string BuildBackupFileName(string id, string version, string hash)
{
if (id == null)
{
throw new ArgumentNullException(nameof(id));
}

if (version == null)
{
throw new ArgumentNullException(nameof(version));
}

if (hash == null)
{
throw new ArgumentNullException(nameof(hash));
}

var hashBytes = Convert.FromBase64String(hash);

return string.Format(
CultureInfo.InvariantCulture,
Constants.PackageFileBackupSavePathTemplate,
id,
version,
HttpServerUtility.UrlTokenEncode(hashBytes),
CoreConstants.NuGetPackageFileExtension);
}
}
}
Loading

0 comments on commit 0ec515d

Please sign in to comment.