Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changes to ~/nuget.exe per code review

  • Loading branch information...
commit 0b51b781c985965bf1130876c4bb58fdaa26b427 1 parent 0afe584
Pranav K pranavkm authored
101 Facts/Controllers/PackagesControllerFacts.cs
View
@@ -1069,6 +1069,100 @@ public void WillCurateThePackage()
fakeAutoCuratePackageCmd.Verify(fake => fake.Execute(fakePackage, fakeNuGetPackage.Object));
}
+
+ [Fact]
+ public void WillExtractNuGetExe()
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = true };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ nugetExeDownloader.Setup(d => d.UpdateExecutable(It.IsAny<IPackage>())).Verifiable();
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify();
+ }
+
+ [Fact]
+ public void WillNotExtractNuGetExeIfIsNotLatestStable()
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = false };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
+ }
+
+ [Theory]
+ [InlineData("nuget-commandline")]
+ [InlineData("nuget..commandline")]
+ [InlineData("nuget.command")]
+ public void WillNotExtractNuGetExeIfIsItDoesNotMatchId(string id)
+ {
+ // Arrange
+ var fakeCurrentUser = new User { Key = 42 };
+ var fakeUserSvc = new Mock<IUserService>();
+ fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
+ var fakeIdentity = new Mock<IIdentity>();
+ fakeIdentity.Setup(x => x.Name).Returns("theUsername");
+ var fakeUploadFileSvc = new Mock<IUploadFileService>();
+ var fakeFileStream = new MemoryStream();
+ fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
+ var fakePackageSvc = new Mock<IPackageService>();
+ var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = id }, Version = "2.0.0", IsLatestStable = true };
+ fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
+ var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
+ var controller = CreateController(
+ packageSvc: fakePackageSvc,
+ uploadFileSvc: fakeUploadFileSvc,
+ fakeIdentity: fakeIdentity,
+ userSvc: fakeUserSvc,
+ downloaderSvc: nugetExeDownloader);
+
+ // Act
+ controller.VerifyPackage(false);
+
+ // Assert
+ nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
+ }
}
public class TheCancelVerifyPackageAction
@@ -1124,7 +1218,8 @@ public void RedirectsToUploadPageAfterDelete()
Mock<IPackage> fakeNuGetPackage = null,
Mock<ISearchService> searchService = null,
Exception readPackageException = null,
- Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null)
+ Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null,
+ Mock<INuGetExeDownloaderService> downloaderSvc = null)
{
packageSvc = packageSvc ?? new Mock<IPackageService>();
@@ -1133,6 +1228,7 @@ public void RedirectsToUploadPageAfterDelete()
messageSvc = messageSvc ?? new Mock<IMessageService>();
searchService = searchService ?? CreateSearchService();
autoCuratePackageCmd = autoCuratePackageCmd ?? new Mock<IAutomaticallyCuratePackageCommand>();
+ downloaderSvc = downloaderSvc ?? new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
var controller = new Mock<PackagesController>(
packageSvc.Object,
@@ -1140,7 +1236,8 @@ public void RedirectsToUploadPageAfterDelete()
userSvc.Object,
messageSvc.Object,
searchService.Object,
- autoCuratePackageCmd.Object);
+ autoCuratePackageCmd.Object,
+ downloaderSvc.Object);
controller.CallBase = true;
if (httpContext != null)
99 Facts/Services/NuGetExeDownloaderServiceFacts.cs
View
@@ -1,5 +1,4 @@
-using System;
-using System.IO;
+using System.IO;
using System.Linq;
using System.Web.Mvc;
using Moq;
@@ -10,34 +9,36 @@ namespace NuGetGallery.Services
{
public class NuGetExeDownloaderServiceFacts
{
- private static readonly string _exePath = @"x:\NuGetGallery\nuget.exe";
-
[Fact]
- public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExistsAndIsRecent()
+ public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExists()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true).Verifiable();
- fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
- .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(60)))
- .Verifiable();
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe"))
+ .Returns(true).Verifiable();
+ fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
+ .Returns(Mock.Of<ActionResult>())
+ .Verifiable();
// Act
- var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
+ var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
+ downloaderSvc.CreateNuGetExeDownloadActionResult();
// Assert
- fileSystem.Verify();
- AssertActionResult(result);
+ fileStorage.Verify();
}
[Fact]
public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(false);
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe")).Returns(false);
+ fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
+ .Verifiable();
+ fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
+ .Returns(Mock.Of<ActionResult>())
+ .Verifiable();
var package = new Package { Version = "2.0.0" };
var packageService = new Mock<IPackageService>(MockBehavior.Strict);
@@ -50,70 +51,31 @@ public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
.Verifiable();
// Act
- var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
+ var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileStorage);
+ downloaderSvc.CreateNuGetExeDownloadActionResult();
// Assert
packageFileSvc.Verify();
packageService.Verify();
- AssertActionResult(result);
}
[Fact]
- public void CreateNuGetExeDownloadExtractsFileIfItExistsButIsNotRecent()
+ public void UpdateExecutableExtractsExeToFileStorage()
{
// Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true);
- fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
- .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromHours(32)));
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
-
- var package = new Package { Version = "2.0.0" };
- var packageService = new Mock<IPackageService>(MockBehavior.Strict);
- packageService.Setup(s => s.FindPackageByIdAndVersion("NuGet.CommandLine", null, false))
- .Returns(package)
- .Verifiable();
- var packageFileSvc = new Mock<IPackageFileService>(MockBehavior.Strict);
- packageFileSvc.Setup(s => s.DownloadPackageFile(package))
- .Returns(CreateCommandLinePackage)
- .Verifiable();
-
- // Act
- var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
- var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
-
- // Assert
- packageFileSvc.Verify();
- packageService.Verify();
- AssertActionResult(result);
- }
-
- [Fact]
- public void UpdateExecutableExtractsExeToDisk()
- {
- // Arrange
- var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
- fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
+ var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
+ fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
+ .Verifiable();
var nugetPackage = new Mock<IPackage>();
nugetPackage.Setup(s => s.GetFiles()).Returns(new[] { CreateExePackageFile() }.AsQueryable());
// Act
- var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
+ var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
downloaderSvc.UpdateExecutable(nugetPackage.Object);
// Assert
- fileSystem.Verify();
- }
-
- private static void AssertActionResult(ActionResult result)
- {
- Assert.IsType<FilePathResult>(result);
- var filePathResult = (FilePathResult)result;
- Assert.Equal(_exePath, filePathResult.FileName);
- Assert.Equal(@"application/octet-stream", filePathResult.ContentType);
- Assert.Equal(@"NuGet.exe", filePathResult.FileDownloadName);
+ fileStorage.Verify();
}
private static Stream CreateCommandLinePackage()
@@ -146,16 +108,13 @@ private static IPackageFile CreateExePackageFile()
private static NuGetExeDownloaderService GetDownloaderService(
Mock<IPackageService> packageSvc = null,
Mock<IPackageFileService> packageFileSvc = null,
- Mock<IFileSystemService> fileSystemSvc = null)
+ Mock<IFileStorageService> fileStorageSvc = null)
{
packageSvc = packageSvc ?? new Mock<IPackageService>(MockBehavior.Strict);
packageFileSvc = packageFileSvc ?? new Mock<IPackageFileService>(MockBehavior.Strict);
- fileSystemSvc = fileSystemSvc ?? new Mock<IFileSystemService>(MockBehavior.Strict);
+ fileStorageSvc = fileStorageSvc ?? new Mock<IFileStorageService>(MockBehavior.Strict);
- return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileSystemSvc.Object)
- {
- NuGetExePath = _exePath
- };
+ return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileStorageSvc.Object);
}
}
}
8 Website/Constants.cs
View
@@ -14,14 +14,22 @@ public static class Constants
public const string NuGetPackageFileExtension = ".nupkg";
public const string PackageFileDownloadUriTemplate = "packages/{0}/{1}/download";
public const string PackageFileSavePathTemplate = "{0}.{1}{2}";
+
public const string PackagesFolderName = "packages";
+ public const string DownloadsFolderName = "downloads";
+
public const string PopularitySortOrder = "package-download-count";
public const string RecentSortOrder = "package-created";
public const string RelevanceSortOrder = "relevance";
+
public const string Sha1HashAlgorithmId = "SHA1";
public const string Sha512HashAlgorithmId = "SHA512";
public const string PBKDF2HashAlgorithmId = "PBKDF2";
+
public const string UploadFileNameTemplate = "{0}{1}";
public const string UploadsFolderName = "uploads";
+
+ public const string NuGetCommandLinePackageId = "NuGet.CommandLine";
+
}
}
5 Website/Controllers/ApiController.cs
View
@@ -11,7 +11,6 @@ namespace NuGetGallery
{
public partial class ApiController : Controller
{
- private const string NuGetCommandLinePackage = "NuGet.CommandLine";
private readonly IPackageService packageSvc;
private readonly IUserService userSvc;
private readonly IPackageFileService packageFileSvc;
@@ -55,7 +54,7 @@ public virtual ActionResult GetPackage(string id, string version)
OutputCache(VaryByParam = "none", Location = OutputCacheLocation.ServerAndClient, Duration = 600)]
public virtual ActionResult GetNuGetExe()
{
- return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionnResult();
+ return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionResult();
}
[ActionName("VerifyPackageKeyApi"), HttpGet]
@@ -130,7 +129,7 @@ private ActionResult CreatePackageInternal(string apiKey)
}
var package = packageSvc.CreatePackage(packageToPush, user);
- if (packageToPush.Id.Equals(NuGetCommandLinePackage, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
+ if (packageToPush.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
{
// If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
nugetExeDownloaderSvc.UpdateExecutable(packageToPush);
11 Website/Controllers/PackagesController.cs
View
@@ -24,6 +24,7 @@ public partial class PackagesController : Controller
private readonly IMessageService messageService;
private readonly ISearchService searchSvc;
private readonly IAutomaticallyCuratePackageCommand autoCuratedPackageCmd;
+ private readonly INuGetExeDownloaderService nugetExeDownloaderSvc;
public PackagesController(
IPackageService packageSvc,
@@ -31,7 +32,8 @@ public partial class PackagesController : Controller
IUserService userSvc,
IMessageService messageService,
ISearchService searchSvc,
- IAutomaticallyCuratePackageCommand autoCuratedPackageCmd)
+ IAutomaticallyCuratePackageCommand autoCuratedPackageCmd,
+ INuGetExeDownloaderService nugetExeDownloaderSvc)
{
this.packageSvc = packageSvc;
this.uploadFileSvc = uploadFileSvc;
@@ -39,6 +41,7 @@ public partial class PackagesController : Controller
this.messageService = messageService;
this.searchSvc = searchSvc;
this.autoCuratedPackageCmd = autoCuratedPackageCmd;
+ this.nugetExeDownloaderSvc = nugetExeDownloaderSvc;
}
[Authorize]
@@ -512,6 +515,12 @@ public virtual ActionResult VerifyPackage(bool? listed)
tx.Complete();
}
+ if (package.PackageRegistration.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
+ {
+ // If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
+ nugetExeDownloaderSvc.UpdateExecutable(nugetPackage);
+ }
+
TempData["Message"] = String.Format(CultureInfo.CurrentCulture, Strings.SuccessfullyUploadedPackage, package.PackageRegistration.Id, package.Version);
return RedirectToRoute(RouteName.DisplayPackage, new { package.PackageRegistration.Id, package.Version });
}
13 Website/Services/CloudBlobFileStorageService.cs
View
@@ -17,6 +17,7 @@ public CloudBlobFileStorageService(ICloudBlobClient client)
this.client = client;
PrepareContainer(Constants.PackagesFolderName, isPublic: true);
+ PrepareContainer(Constants.DownloadsFolderName, isPublic: true);
PrepareContainer(Constants.UploadsFolderName, isPublic: false);
}
@@ -38,6 +39,16 @@ public CloudBlobFileStorageService(ICloudBlobClient client)
blob.DeleteIfExists();
}
+ public bool FileExists(
+ string folderName,
+ string fileName)
+ {
+
+ var container = GetContainer(folderName);
+ var blob = container.GetBlobReference(fileName);
+ return blob.Exists();
+ }
+
ICloudBlobContainer GetContainer(string folderName)
{
return containers[folderName];
@@ -50,6 +61,8 @@ static string GetContentType(string folderName)
case Constants.PackagesFolderName:
case Constants.UploadsFolderName:
return Constants.PackageContentType;
+ case Constants.DownloadsFolderName:
+ return Constants.OctetStreamContentType;
default:
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
}
22 Website/Services/CloudBlobWrapper.cs
View
@@ -13,7 +13,7 @@ public CloudBlobWrapper(CloudBlob blob)
this.blob = blob;
}
- public BlobProperties Properties
+ public BlobProperties Properties
{
get { return blob.Properties; }
}
@@ -40,6 +40,26 @@ public void DownloadToStream(Stream target)
}
}
+ public bool Exists()
+ {
+ try
+ {
+ blob.FetchAttributes();
+ return true;
+ }
+ catch (StorageClientException e)
+ {
+ if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
+ {
+ return false;
+ }
+ else
+ {
+ throw;
+ }
+ }
+ }
+
public void SetProperties()
{
blob.SetProperties();
27 Website/Services/FileSystemFileStorageService.cs
View
@@ -27,14 +27,14 @@ public class FileSystemFileStorageService : IFileStorageService
}
public ActionResult CreateDownloadFileActionResult(
- string folderName,
+ string folderName,
string fileName)
{
if (String.IsNullOrWhiteSpace(folderName))
throw new ArgumentNullException("folderName");
if (String.IsNullOrWhiteSpace(fileName))
throw new ArgumentNullException("fileName");
-
+
var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
if (!fileSystemSvc.FileExists(path))
return new HttpNotFoundResult();
@@ -50,27 +50,42 @@ static string GetContentType(string folderName)
{
case Constants.PackagesFolderName:
return Constants.PackageContentType;
+ case Constants.DownloadsFolderName:
+ return Constants.OctetStreamContentType;
default:
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
}
}
public void DeleteFile(
- string folderName,
+ string folderName,
string fileName)
{
if (String.IsNullOrWhiteSpace(folderName))
throw new ArgumentNullException("folderName");
if (String.IsNullOrWhiteSpace(fileName))
throw new ArgumentNullException("fileName");
-
+
var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
if (fileSystemSvc.FileExists(path))
fileSystemSvc.DeleteFile(path);
}
+ public bool FileExists(
+ string folderName,
+ string fileName)
+ {
+ if (String.IsNullOrWhiteSpace(folderName))
+ throw new ArgumentNullException("folderName");
+ if (String.IsNullOrWhiteSpace(fileName))
+ throw new ArgumentNullException("fileName");
+
+ var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
+ return fileSystemSvc.FileExists(path);
+ }
+
public Stream GetFile(
- string folderName,
+ string folderName,
string fileName)
{
if (String.IsNullOrWhiteSpace(folderName))
@@ -87,7 +102,7 @@ static string GetContentType(string folderName)
public void SaveFile(
string folderName,
- string fileName,
+ string fileName,
Stream packageFile)
{
if (String.IsNullOrWhiteSpace(folderName))
1  Website/Services/ICloudBlob.cs
View
@@ -11,6 +11,7 @@ public interface ICloudBlob
void DeleteIfExists();
void DownloadToStream(Stream target);
+ bool Exists();
void SetProperties();
void UploadFromStream(Stream packageFile);
}
4 Website/Services/IFileStorageService.cs
View
@@ -13,6 +13,10 @@ public interface IFileStorageService
string folderName,
string fileName);
+ bool FileExists(
+ string folderName,
+ string fileName);
+
Stream GetFile(
string folderName,
string fileName);
2  Website/Services/INuGetExeDownloaderService.cs
View
@@ -5,7 +5,7 @@ namespace NuGetGallery
{
public interface INuGetExeDownloaderService
{
- ActionResult CreateNuGetExeDownloadActionnResult();
+ ActionResult CreateNuGetExeDownloadActionResult();
void UpdateExecutable(IPackage package);
}
33 Website/Services/NuGetExeDownloaderService.cs
View
@@ -1,7 +1,6 @@
using System;
using System.IO;
using System.Linq;
-using System.Web;
using System.Web.Mvc;
using NuGet;
@@ -9,38 +8,25 @@ namespace NuGetGallery
{
public class NuGetExeDownloaderService : INuGetExeDownloaderService
{
- private static readonly TimeSpan _exeRefreshInterval = TimeSpan.FromDays(1);
- private static readonly Lazy<string> _defaultNuGetExePath = new Lazy<string>(() => Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", "NuGet.exe"));
private static readonly object fileLock = new object();
private readonly IPackageService packageSvc;
private readonly IPackageFileService packageFileSvc;
- private readonly IFileSystemService fileSystem;
- private string _nugetExePath;
+ private readonly IFileStorageService fileStorageSvc;
public NuGetExeDownloaderService(
IPackageService packageSvc,
IPackageFileService packageFileSvc,
- IFileSystemService fileSystem)
+ IFileStorageService fileStorageSvc)
{
this.packageSvc = packageSvc;
this.packageFileSvc = packageFileSvc;
- this.fileSystem = fileSystem;
+ this.fileStorageSvc = fileStorageSvc;
}
- public string NuGetExePath
- {
- get { return _nugetExePath ?? _defaultNuGetExePath.Value; }
- set { _nugetExePath = value; }
- }
-
- public ActionResult CreateNuGetExeDownloadActionnResult()
+ public ActionResult CreateNuGetExeDownloadActionResult()
{
EnsureNuGetExe();
- var result = new FilePathResult(NuGetExePath, Constants.OctetStreamContentType)
- {
- FileDownloadName = "NuGet.exe"
- };
- return result;
+ return fileStorageSvc.CreateDownloadFileActionResult(Constants.DownloadsFolderName, "nuget.exe");
}
public void UpdateExecutable(IPackage zipPackage)
@@ -53,9 +39,9 @@ public void UpdateExecutable(IPackage zipPackage)
private void EnsureNuGetExe()
{
- if (fileSystem.FileExists(NuGetExePath) && (DateTime.UtcNow - fileSystem.GetCreationTimeUtc(NuGetExePath)) < _exeRefreshInterval)
+ if (fileStorageSvc.FileExists(Constants.DownloadsFolderName, "nuget.exe"))
{
- // Ensure the file exists and it is recent enough.
+ // Ensure the file exists on blob storage.
return;
}
@@ -80,10 +66,9 @@ private void ExtractNuGetExe(IPackage package)
var executable = package.GetFiles("tools")
.First(f => f.Path.Equals(@"tools\NuGet.exe", StringComparison.OrdinalIgnoreCase));
- using (Stream fileStream = fileSystem.OpenWrite(NuGetExePath),
- packageFileStream = executable.GetStream())
+ using (Stream packageFileStream = executable.GetStream())
{
- packageFileStream.CopyTo(fileStream);
+ fileStorageSvc.SaveFile(Constants.DownloadsFolderName, "nuget.exe", packageFileStream);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.