Skip to content
This repository
Browse code

Changes to ~/nuget.exe per code review

  • Loading branch information...
commit 0b51b781c985965bf1130876c4bb58fdaa26b427 1 parent 0afe584
Pranav K authored May 30, 2012
101  Facts/Controllers/PackagesControllerFacts.cs
@@ -1069,6 +1069,100 @@ public void WillCurateThePackage()
1069 1069
 
1070 1070
                 fakeAutoCuratePackageCmd.Verify(fake => fake.Execute(fakePackage, fakeNuGetPackage.Object));
1071 1071
             }
  1072
+
  1073
+            [Fact]
  1074
+            public void WillExtractNuGetExe()
  1075
+            {
  1076
+                // Arrange
  1077
+                var fakeCurrentUser = new User { Key = 42 };
  1078
+                var fakeUserSvc = new Mock<IUserService>();
  1079
+                fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
  1080
+                var fakeIdentity = new Mock<IIdentity>();
  1081
+                fakeIdentity.Setup(x => x.Name).Returns("theUsername");
  1082
+                var fakeUploadFileSvc = new Mock<IUploadFileService>();
  1083
+                var fakeFileStream = new MemoryStream();
  1084
+                fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
  1085
+                var fakePackageSvc = new Mock<IPackageService>();
  1086
+                var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = true };
  1087
+                fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
  1088
+                var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
  1089
+                nugetExeDownloader.Setup(d => d.UpdateExecutable(It.IsAny<IPackage>())).Verifiable();
  1090
+                var controller = CreateController(
  1091
+                    packageSvc: fakePackageSvc,
  1092
+                    uploadFileSvc: fakeUploadFileSvc,
  1093
+                    fakeIdentity: fakeIdentity,
  1094
+                    userSvc: fakeUserSvc,
  1095
+                    downloaderSvc: nugetExeDownloader);
  1096
+
  1097
+                // Act
  1098
+                controller.VerifyPackage(false);
  1099
+
  1100
+                // Assert
  1101
+                nugetExeDownloader.Verify();
  1102
+            }
  1103
+
  1104
+            [Fact]
  1105
+            public void WillNotExtractNuGetExeIfIsNotLatestStable()
  1106
+            {
  1107
+                // Arrange
  1108
+                var fakeCurrentUser = new User { Key = 42 };
  1109
+                var fakeUserSvc = new Mock<IUserService>();
  1110
+                fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
  1111
+                var fakeIdentity = new Mock<IIdentity>();
  1112
+                fakeIdentity.Setup(x => x.Name).Returns("theUsername");
  1113
+                var fakeUploadFileSvc = new Mock<IUploadFileService>();
  1114
+                var fakeFileStream = new MemoryStream();
  1115
+                fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
  1116
+                var fakePackageSvc = new Mock<IPackageService>();
  1117
+                var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = "NuGet.CommandLine" }, Version = "2.0.0", IsLatestStable = false };
  1118
+                fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
  1119
+                var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
  1120
+                var controller = CreateController(
  1121
+                    packageSvc: fakePackageSvc,
  1122
+                    uploadFileSvc: fakeUploadFileSvc,
  1123
+                    fakeIdentity: fakeIdentity,
  1124
+                    userSvc: fakeUserSvc,
  1125
+                    downloaderSvc: nugetExeDownloader);
  1126
+
  1127
+                // Act
  1128
+                controller.VerifyPackage(false);
  1129
+
  1130
+                // Assert
  1131
+                nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
  1132
+            }
  1133
+
  1134
+            [Theory]
  1135
+            [InlineData("nuget-commandline")]
  1136
+            [InlineData("nuget..commandline")]
  1137
+            [InlineData("nuget.command")]
  1138
+            public void WillNotExtractNuGetExeIfIsItDoesNotMatchId(string id)
  1139
+            {
  1140
+                // Arrange
  1141
+                var fakeCurrentUser = new User { Key = 42 };
  1142
+                var fakeUserSvc = new Mock<IUserService>();
  1143
+                fakeUserSvc.Setup(x => x.FindByUsername(It.IsAny<string>())).Returns(fakeCurrentUser);
  1144
+                var fakeIdentity = new Mock<IIdentity>();
  1145
+                fakeIdentity.Setup(x => x.Name).Returns("theUsername");
  1146
+                var fakeUploadFileSvc = new Mock<IUploadFileService>();
  1147
+                var fakeFileStream = new MemoryStream();
  1148
+                fakeUploadFileSvc.Setup(x => x.GetUploadFile(42)).Returns(Stream.Null);
  1149
+                var fakePackageSvc = new Mock<IPackageService>();
  1150
+                var commandLinePackage = new Package { PackageRegistration = new PackageRegistration { Id = id }, Version = "2.0.0", IsLatestStable = true };
  1151
+                fakePackageSvc.Setup(x => x.CreatePackage(It.IsAny<IPackage>(), It.IsAny<User>())).Returns(commandLinePackage);
  1152
+                var nugetExeDownloader = new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
  1153
+                var controller = CreateController(
  1154
+                    packageSvc: fakePackageSvc,
  1155
+                    uploadFileSvc: fakeUploadFileSvc,
  1156
+                    fakeIdentity: fakeIdentity,
  1157
+                    userSvc: fakeUserSvc,
  1158
+                    downloaderSvc: nugetExeDownloader);
  1159
+
  1160
+                // Act
  1161
+                controller.VerifyPackage(false);
  1162
+
  1163
+                // Assert
  1164
+                nugetExeDownloader.Verify(d => d.UpdateExecutable(It.IsAny<IPackage>()), Times.Never());
  1165
+            }
1072 1166
         }
1073 1167
 
1074 1168
         public class TheCancelVerifyPackageAction
@@ -1124,7 +1218,8 @@ public void RedirectsToUploadPageAfterDelete()
1124 1218
             Mock<IPackage> fakeNuGetPackage = null,
1125 1219
             Mock<ISearchService> searchService = null,
1126 1220
             Exception readPackageException = null,
1127  
-            Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null)
  1221
+            Mock<IAutomaticallyCuratePackageCommand> autoCuratePackageCmd = null,
  1222
+            Mock<INuGetExeDownloaderService> downloaderSvc = null)
1128 1223
         {
1129 1224
 
1130 1225
             packageSvc = packageSvc ?? new Mock<IPackageService>();
@@ -1133,6 +1228,7 @@ public void RedirectsToUploadPageAfterDelete()
1133 1228
             messageSvc = messageSvc ?? new Mock<IMessageService>();
1134 1229
             searchService = searchService ?? CreateSearchService();
1135 1230
             autoCuratePackageCmd = autoCuratePackageCmd ?? new Mock<IAutomaticallyCuratePackageCommand>();
  1231
+            downloaderSvc = downloaderSvc ?? new Mock<INuGetExeDownloaderService>(MockBehavior.Strict);
1136 1232
 
1137 1233
             var controller = new Mock<PackagesController>(
1138 1234
                     packageSvc.Object,
@@ -1140,7 +1236,8 @@ public void RedirectsToUploadPageAfterDelete()
1140 1236
                     userSvc.Object,
1141 1237
                     messageSvc.Object,
1142 1238
                     searchService.Object,
1143  
-                    autoCuratePackageCmd.Object);
  1239
+                    autoCuratePackageCmd.Object,
  1240
+                    downloaderSvc.Object);
1144 1241
             controller.CallBase = true;
1145 1242
 
1146 1243
             if (httpContext != null)
99  Facts/Services/NuGetExeDownloaderServiceFacts.cs
... ...
@@ -1,5 +1,4 @@
1  
-using System;
2  
-using System.IO;
  1
+using System.IO;
3 2
 using System.Linq;
4 3
 using System.Web.Mvc;
5 4
 using Moq;
@@ -10,34 +9,36 @@ namespace NuGetGallery.Services
10 9
 {
11 10
     public class NuGetExeDownloaderServiceFacts
12 11
     {
13  
-        private static readonly string _exePath = @"x:\NuGetGallery\nuget.exe";
14  
-
15 12
         [Fact]
16  
-        public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExistsAndIsRecent()
  13
+        public void CreateNuGetExeDownloadDoesNotExtractFileIfItAlreadyExists()
17 14
         {
18 15
             // Arrange
19  
-            var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
20  
-            fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true).Verifiable();
21  
-            fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
22  
-                      .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromMinutes(60)))
23  
-                      .Verifiable();
  16
+            var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
  17
+            fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe"))
  18
+                       .Returns(true).Verifiable();
  19
+            fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
  20
+                       .Returns(Mock.Of<ActionResult>())
  21
+                       .Verifiable();
24 22
 
25 23
             // Act
26  
-            var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
27  
-            var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
  24
+            var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
  25
+            downloaderSvc.CreateNuGetExeDownloadActionResult();
28 26
 
29 27
             // Assert
30  
-            fileSystem.Verify();
31  
-            AssertActionResult(result);
  28
+            fileStorage.Verify();
32 29
         }
33 30
 
34 31
         [Fact]
35 32
         public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
36 33
         {
37 34
             // Arrange
38  
-            var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
39  
-            fileSystem.Setup(s => s.FileExists(_exePath)).Returns(false);
40  
-            fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
  35
+            var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
  36
+            fileStorage.Setup(s => s.FileExists("downloads", "nuget.exe")).Returns(false);
  37
+            fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
  38
+                       .Verifiable();
  39
+            fileStorage.Setup(s => s.CreateDownloadFileActionResult("downloads", "nuget.exe"))
  40
+                       .Returns(Mock.Of<ActionResult>())
  41
+                       .Verifiable();
41 42
 
42 43
             var package = new Package { Version = "2.0.0" };
43 44
             var packageService = new Mock<IPackageService>(MockBehavior.Strict);
@@ -50,70 +51,31 @@ public void CreateNuGetExeDownloadExtractsFileIfItDoesNotExist()
50 51
                           .Verifiable();
51 52
 
52 53
             // Act
53  
-            var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
54  
-            var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
  54
+            var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileStorage);
  55
+            downloaderSvc.CreateNuGetExeDownloadActionResult();
55 56
 
56 57
             // Assert
57 58
             packageFileSvc.Verify();
58 59
             packageService.Verify();
59  
-            AssertActionResult(result);
60 60
         }
61 61
 
62 62
         [Fact]
63  
-        public void CreateNuGetExeDownloadExtractsFileIfItExistsButIsNotRecent()
  63
+        public void UpdateExecutableExtractsExeToFileStorage()
64 64
         {
65 65
             // Arrange
66  
-            var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
67  
-            fileSystem.Setup(s => s.FileExists(_exePath)).Returns(true);
68  
-            fileSystem.Setup(s => s.GetCreationTimeUtc(_exePath))
69  
-                      .Returns(DateTime.UtcNow.Subtract(TimeSpan.FromHours(32)));
70  
-            fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
71  
-
72  
-            var package = new Package { Version = "2.0.0" };
73  
-            var packageService = new Mock<IPackageService>(MockBehavior.Strict);
74  
-            packageService.Setup(s => s.FindPackageByIdAndVersion("NuGet.CommandLine", null, false))
75  
-                          .Returns(package)
76  
-                          .Verifiable();
77  
-            var packageFileSvc = new Mock<IPackageFileService>(MockBehavior.Strict);
78  
-            packageFileSvc.Setup(s => s.DownloadPackageFile(package))
79  
-                          .Returns(CreateCommandLinePackage)
80  
-                          .Verifiable();
81  
-
82  
-            // Act
83  
-            var downloaderSvc = GetDownloaderService(packageService, packageFileSvc, fileSystem);
84  
-            var result = downloaderSvc.CreateNuGetExeDownloadActionnResult();
85  
-
86  
-            // Assert
87  
-            packageFileSvc.Verify();
88  
-            packageService.Verify();
89  
-            AssertActionResult(result);
90  
-        }
91  
-
92  
-        [Fact]
93  
-        public void UpdateExecutableExtractsExeToDisk()
94  
-        {
95  
-            // Arrange
96  
-            var fileSystem = new Mock<IFileSystemService>(MockBehavior.Strict);
97  
-            fileSystem.Setup(s => s.OpenWrite(_exePath)).Returns(Stream.Null);
  66
+            var fileStorage = new Mock<IFileStorageService>(MockBehavior.Strict);
  67
+            fileStorage.Setup(s => s.SaveFile("downloads", "nuget.exe", It.IsAny<Stream>()))
  68
+                       .Verifiable();
98 69
 
99 70
             var nugetPackage = new Mock<IPackage>();
100 71
             nugetPackage.Setup(s => s.GetFiles()).Returns(new[] { CreateExePackageFile() }.AsQueryable());
101 72
 
102 73
             // Act
103  
-            var downloaderSvc = GetDownloaderService(fileSystemSvc: fileSystem);
  74
+            var downloaderSvc = GetDownloaderService(fileStorageSvc: fileStorage);
104 75
             downloaderSvc.UpdateExecutable(nugetPackage.Object);
105 76
 
106 77
             // Assert
107  
-            fileSystem.Verify();
108  
-        }
109  
-
110  
-        private static void AssertActionResult(ActionResult result)
111  
-        {
112  
-            Assert.IsType<FilePathResult>(result);
113  
-            var filePathResult = (FilePathResult)result;
114  
-            Assert.Equal(_exePath, filePathResult.FileName);
115  
-            Assert.Equal(@"application/octet-stream", filePathResult.ContentType);
116  
-            Assert.Equal(@"NuGet.exe", filePathResult.FileDownloadName);
  78
+            fileStorage.Verify();
117 79
         }
118 80
 
119 81
         private static Stream CreateCommandLinePackage()
@@ -146,16 +108,13 @@ private static IPackageFile CreateExePackageFile()
146 108
         private static NuGetExeDownloaderService GetDownloaderService(
147 109
             Mock<IPackageService> packageSvc = null,
148 110
             Mock<IPackageFileService> packageFileSvc = null,
149  
-            Mock<IFileSystemService> fileSystemSvc = null)
  111
+            Mock<IFileStorageService> fileStorageSvc = null)
150 112
         {
151 113
             packageSvc = packageSvc ?? new Mock<IPackageService>(MockBehavior.Strict);
152 114
             packageFileSvc = packageFileSvc ?? new Mock<IPackageFileService>(MockBehavior.Strict);
153  
-            fileSystemSvc = fileSystemSvc ?? new Mock<IFileSystemService>(MockBehavior.Strict);
  115
+            fileStorageSvc = fileStorageSvc ?? new Mock<IFileStorageService>(MockBehavior.Strict);
154 116
 
155  
-            return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileSystemSvc.Object)
156  
-            {
157  
-                NuGetExePath = _exePath
158  
-            };
  117
+            return new NuGetExeDownloaderService(packageSvc.Object, packageFileSvc.Object, fileStorageSvc.Object);
159 118
         }
160 119
     }
161 120
 }
8  Website/Constants.cs
@@ -14,14 +14,22 @@ public static class Constants
14 14
         public const string NuGetPackageFileExtension = ".nupkg";
15 15
         public const string PackageFileDownloadUriTemplate = "packages/{0}/{1}/download";
16 16
         public const string PackageFileSavePathTemplate = "{0}.{1}{2}";
  17
+        
17 18
         public const string PackagesFolderName = "packages";
  19
+        public const string DownloadsFolderName = "downloads";
  20
+
18 21
         public const string PopularitySortOrder = "package-download-count";
19 22
         public const string RecentSortOrder = "package-created";
20 23
         public const string RelevanceSortOrder = "relevance";
  24
+        
21 25
         public const string Sha1HashAlgorithmId = "SHA1";
22 26
         public const string Sha512HashAlgorithmId = "SHA512";
23 27
         public const string PBKDF2HashAlgorithmId = "PBKDF2";
  28
+        
24 29
         public const string UploadFileNameTemplate = "{0}{1}";
25 30
         public const string UploadsFolderName = "uploads";
  31
+        
  32
+        public const string NuGetCommandLinePackageId = "NuGet.CommandLine";
  33
+
26 34
     }
27 35
 }
5  Website/Controllers/ApiController.cs
@@ -11,7 +11,6 @@ namespace NuGetGallery
11 11
 {
12 12
     public partial class ApiController : Controller
13 13
     {
14  
-        private const string NuGetCommandLinePackage = "NuGet.CommandLine";
15 14
         private readonly IPackageService packageSvc;
16 15
         private readonly IUserService userSvc;
17 16
         private readonly IPackageFileService packageFileSvc;
@@ -55,7 +54,7 @@ public virtual ActionResult GetPackage(string id, string version)
55 54
          OutputCache(VaryByParam = "none", Location = OutputCacheLocation.ServerAndClient, Duration = 600)]
56 55
         public virtual ActionResult GetNuGetExe()
57 56
         {
58  
-            return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionnResult();
  57
+            return nugetExeDownloaderSvc.CreateNuGetExeDownloadActionResult();
59 58
         }
60 59
 
61 60
         [ActionName("VerifyPackageKeyApi"), HttpGet]
@@ -130,7 +129,7 @@ private ActionResult CreatePackageInternal(string apiKey)
130 129
             }
131 130
 
132 131
             var package = packageSvc.CreatePackage(packageToPush, user);
133  
-            if (packageToPush.Id.Equals(NuGetCommandLinePackage, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
  132
+            if (packageToPush.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
134 133
             {
135 134
                 // If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
136 135
                 nugetExeDownloaderSvc.UpdateExecutable(packageToPush);
11  Website/Controllers/PackagesController.cs
@@ -24,6 +24,7 @@ public partial class PackagesController : Controller
24 24
         private readonly IMessageService messageService;
25 25
         private readonly ISearchService searchSvc;
26 26
         private readonly IAutomaticallyCuratePackageCommand autoCuratedPackageCmd;
  27
+        private readonly INuGetExeDownloaderService nugetExeDownloaderSvc;
27 28
 
28 29
         public PackagesController(
29 30
             IPackageService packageSvc,
@@ -31,7 +32,8 @@ public partial class PackagesController : Controller
31 32
             IUserService userSvc,
32 33
             IMessageService messageService,
33 34
             ISearchService searchSvc,
34  
-            IAutomaticallyCuratePackageCommand autoCuratedPackageCmd)
  35
+            IAutomaticallyCuratePackageCommand autoCuratedPackageCmd,
  36
+            INuGetExeDownloaderService nugetExeDownloaderSvc)
35 37
         {
36 38
             this.packageSvc = packageSvc;
37 39
             this.uploadFileSvc = uploadFileSvc;
@@ -39,6 +41,7 @@ public partial class PackagesController : Controller
39 41
             this.messageService = messageService;
40 42
             this.searchSvc = searchSvc;
41 43
             this.autoCuratedPackageCmd = autoCuratedPackageCmd;
  44
+            this.nugetExeDownloaderSvc = nugetExeDownloaderSvc;
42 45
         }
43 46
 
44 47
         [Authorize]
@@ -512,6 +515,12 @@ public virtual ActionResult VerifyPackage(bool? listed)
512 515
                 tx.Complete();
513 516
             }
514 517
 
  518
+            if (package.PackageRegistration.Id.Equals(Constants.NuGetCommandLinePackageId, StringComparison.OrdinalIgnoreCase) && package.IsLatestStable)
  519
+            {
  520
+                // If we're pushing a new stable version of NuGet.CommandLine, update the extracted executable.
  521
+                nugetExeDownloaderSvc.UpdateExecutable(nugetPackage);
  522
+            }
  523
+
515 524
             TempData["Message"] = String.Format(CultureInfo.CurrentCulture, Strings.SuccessfullyUploadedPackage, package.PackageRegistration.Id, package.Version);
516 525
             return RedirectToRoute(RouteName.DisplayPackage, new { package.PackageRegistration.Id, package.Version });
517 526
         }
13  Website/Services/CloudBlobFileStorageService.cs
@@ -17,6 +17,7 @@ public CloudBlobFileStorageService(ICloudBlobClient client)
17 17
             this.client = client;
18 18
 
19 19
             PrepareContainer(Constants.PackagesFolderName, isPublic: true);
  20
+            PrepareContainer(Constants.DownloadsFolderName, isPublic: true);
20 21
             PrepareContainer(Constants.UploadsFolderName, isPublic: false);
21 22
         }
22 23
 
@@ -38,6 +39,16 @@ public CloudBlobFileStorageService(ICloudBlobClient client)
38 39
             blob.DeleteIfExists();
39 40
         }
40 41
 
  42
+        public bool FileExists(
  43
+            string folderName,
  44
+            string fileName)
  45
+        {
  46
+            
  47
+            var container = GetContainer(folderName);
  48
+            var blob = container.GetBlobReference(fileName);
  49
+            return blob.Exists();
  50
+        }
  51
+
41 52
         ICloudBlobContainer GetContainer(string folderName)
42 53
         {
43 54
             return containers[folderName];
@@ -50,6 +61,8 @@ static string GetContentType(string folderName)
50 61
                 case Constants.PackagesFolderName:
51 62
                 case Constants.UploadsFolderName:
52 63
                     return Constants.PackageContentType;
  64
+                case Constants.DownloadsFolderName:
  65
+                    return Constants.OctetStreamContentType;
53 66
                 default:
54 67
                     throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
55 68
             }
22  Website/Services/CloudBlobWrapper.cs
@@ -13,7 +13,7 @@ public CloudBlobWrapper(CloudBlob blob)
13 13
             this.blob = blob;
14 14
         }
15 15
 
16  
-        public BlobProperties Properties 
  16
+        public BlobProperties Properties
17 17
         {
18 18
             get { return blob.Properties; }
19 19
         }
@@ -40,6 +40,26 @@ public void DownloadToStream(Stream target)
40 40
             }
41 41
         }
42 42
 
  43
+        public bool Exists()
  44
+        {
  45
+            try
  46
+            {
  47
+                blob.FetchAttributes();
  48
+                return true;
  49
+            }
  50
+            catch (StorageClientException e)
  51
+            {
  52
+                if (e.ErrorCode == StorageErrorCode.ResourceNotFound)
  53
+                {
  54
+                    return false;
  55
+                }
  56
+                else
  57
+                {
  58
+                    throw;
  59
+                }
  60
+            }
  61
+        }
  62
+
43 63
         public void SetProperties()
44 64
         {
45 65
             blob.SetProperties();
27  Website/Services/FileSystemFileStorageService.cs
@@ -27,14 +27,14 @@ public class FileSystemFileStorageService : IFileStorageService
27 27
         }
28 28
 
29 29
         public ActionResult CreateDownloadFileActionResult(
30  
-            string folderName, 
  30
+            string folderName,
31 31
             string fileName)
32 32
         {
33 33
             if (String.IsNullOrWhiteSpace(folderName))
34 34
                 throw new ArgumentNullException("folderName");
35 35
             if (String.IsNullOrWhiteSpace(fileName))
36 36
                 throw new ArgumentNullException("fileName");
37  
-            
  37
+
38 38
             var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
39 39
             if (!fileSystemSvc.FileExists(path))
40 40
                 return new HttpNotFoundResult();
@@ -50,27 +50,42 @@ static string GetContentType(string folderName)
50 50
             {
51 51
                 case Constants.PackagesFolderName:
52 52
                     return Constants.PackageContentType;
  53
+                case Constants.DownloadsFolderName:
  54
+                    return Constants.OctetStreamContentType;
53 55
                 default:
54 56
                     throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, "The folder name {0} is not supported.", folderName));
55 57
             }
56 58
         }
57 59
 
58 60
         public void DeleteFile(
59  
-            string folderName, 
  61
+            string folderName,
60 62
             string fileName)
61 63
         {
62 64
             if (String.IsNullOrWhiteSpace(folderName))
63 65
                 throw new ArgumentNullException("folderName");
64 66
             if (String.IsNullOrWhiteSpace(fileName))
65 67
                 throw new ArgumentNullException("fileName");
66  
-            
  68
+
67 69
             var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
68 70
             if (fileSystemSvc.FileExists(path))
69 71
                 fileSystemSvc.DeleteFile(path);
70 72
         }
71 73
 
  74
+        public bool FileExists(
  75
+            string folderName,
  76
+            string fileName)
  77
+        {
  78
+            if (String.IsNullOrWhiteSpace(folderName))
  79
+                throw new ArgumentNullException("folderName");
  80
+            if (String.IsNullOrWhiteSpace(fileName))
  81
+                throw new ArgumentNullException("fileName");
  82
+
  83
+            var path = BuildPath(configuration.FileStorageDirectory, folderName, fileName);
  84
+            return fileSystemSvc.FileExists(path);
  85
+        }
  86
+
72 87
         public Stream GetFile(
73  
-            string folderName, 
  88
+            string folderName,
74 89
             string fileName)
75 90
         {
76 91
             if (String.IsNullOrWhiteSpace(folderName))
@@ -87,7 +102,7 @@ static string GetContentType(string folderName)
87 102
 
88 103
         public void SaveFile(
89 104
             string folderName,
90  
-            string fileName, 
  105
+            string fileName,
91 106
             Stream packageFile)
92 107
         {
93 108
             if (String.IsNullOrWhiteSpace(folderName))
1  Website/Services/ICloudBlob.cs
@@ -11,6 +11,7 @@ public interface ICloudBlob
11 11
 
12 12
         void DeleteIfExists();
13 13
         void DownloadToStream(Stream target);
  14
+        bool Exists();
14 15
         void SetProperties();
15 16
         void UploadFromStream(Stream packageFile);
16 17
     }
4  Website/Services/IFileStorageService.cs
@@ -13,6 +13,10 @@ public interface IFileStorageService
13 13
             string folderName, 
14 14
             string fileName);
15 15
 
  16
+        bool FileExists(
  17
+            string folderName,
  18
+            string fileName);
  19
+
16 20
         Stream GetFile(
17 21
             string folderName,
18 22
             string fileName);
2  Website/Services/INuGetExeDownloaderService.cs
@@ -5,7 +5,7 @@ namespace NuGetGallery
5 5
 {
6 6
     public interface INuGetExeDownloaderService
7 7
     {
8  
-        ActionResult CreateNuGetExeDownloadActionnResult();
  8
+        ActionResult CreateNuGetExeDownloadActionResult();
9 9
 
10 10
         void UpdateExecutable(IPackage package);
11 11
     }
33  Website/Services/NuGetExeDownloaderService.cs
... ...
@@ -1,7 +1,6 @@
1 1
 using System;
2 2
 using System.IO;
3 3
 using System.Linq;
4  
-using System.Web;
5 4
 using System.Web.Mvc;
6 5
 using NuGet;
7 6
 
@@ -9,38 +8,25 @@ namespace NuGetGallery
9 8
 {
10 9
     public class NuGetExeDownloaderService : INuGetExeDownloaderService
11 10
     {
12  
-        private static readonly TimeSpan _exeRefreshInterval = TimeSpan.FromDays(1);
13  
-        private static readonly Lazy<string> _defaultNuGetExePath = new Lazy<string>(() => Path.Combine(HttpRuntime.AppDomainAppPath, "App_Data", "NuGet.exe"));
14 11
         private static readonly object fileLock = new object();
15 12
         private readonly IPackageService packageSvc;
16 13
         private readonly IPackageFileService packageFileSvc;
17  
-        private readonly IFileSystemService fileSystem;
18  
-        private string _nugetExePath;
  14
+        private readonly IFileStorageService fileStorageSvc;
19 15
 
20 16
         public NuGetExeDownloaderService(
21 17
             IPackageService packageSvc,
22 18
             IPackageFileService packageFileSvc,
23  
-            IFileSystemService fileSystem)
  19
+            IFileStorageService fileStorageSvc)
24 20
         {
25 21
             this.packageSvc = packageSvc;
26 22
             this.packageFileSvc = packageFileSvc;
27  
-            this.fileSystem = fileSystem;
  23
+            this.fileStorageSvc = fileStorageSvc;
28 24
         }
29 25
 
30  
-        public string NuGetExePath
31  
-        {
32  
-            get { return _nugetExePath ?? _defaultNuGetExePath.Value; }
33  
-            set { _nugetExePath = value; }
34  
-        }
35  
-
36  
-        public ActionResult CreateNuGetExeDownloadActionnResult()
  26
+        public ActionResult CreateNuGetExeDownloadActionResult()
37 27
         {
38 28
             EnsureNuGetExe();
39  
-            var result = new FilePathResult(NuGetExePath, Constants.OctetStreamContentType)
40  
-                         {
41  
-                             FileDownloadName = "NuGet.exe"
42  
-                         };
43  
-            return result;
  29
+            return fileStorageSvc.CreateDownloadFileActionResult(Constants.DownloadsFolderName, "nuget.exe");
44 30
         }
45 31
 
46 32
         public void UpdateExecutable(IPackage zipPackage)
@@ -53,9 +39,9 @@ public void UpdateExecutable(IPackage zipPackage)
53 39
 
54 40
         private void EnsureNuGetExe()
55 41
         {
56  
-            if (fileSystem.FileExists(NuGetExePath) && (DateTime.UtcNow - fileSystem.GetCreationTimeUtc(NuGetExePath)) < _exeRefreshInterval)
  42
+            if (fileStorageSvc.FileExists(Constants.DownloadsFolderName, "nuget.exe"))
57 43
             {
58  
-                // Ensure the file exists and it is recent enough.
  44
+                // Ensure the file exists on blob storage.
59 45
                 return;
60 46
             }
61 47
 
@@ -80,10 +66,9 @@ private void ExtractNuGetExe(IPackage package)
80 66
             var executable = package.GetFiles("tools")
81 67
                                        .First(f => f.Path.Equals(@"tools\NuGet.exe", StringComparison.OrdinalIgnoreCase));
82 68
 
83  
-            using (Stream fileStream = fileSystem.OpenWrite(NuGetExePath),
84  
-                          packageFileStream = executable.GetStream())
  69
+            using (Stream packageFileStream = executable.GetStream())
85 70
             {
86  
-                packageFileStream.CopyTo(fileStream);
  71
+                fileStorageSvc.SaveFile(Constants.DownloadsFolderName, "nuget.exe", packageFileStream);
87 72
             }
88 73
         }
89 74
     }

0 notes on commit 0b51b78

Please sign in to comment.
Something went wrong with that request. Please try again.