/
NuGetExeDownloaderService.cs
75 lines (66 loc) · 2.43 KB
/
NuGetExeDownloaderService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.IO;
using System.Linq;
using System.Web.Mvc;
using NuGet;
namespace NuGetGallery
{
public class NuGetExeDownloaderService : INuGetExeDownloaderService
{
private static readonly object fileLock = new object();
private readonly IPackageService packageSvc;
private readonly IPackageFileService packageFileSvc;
private readonly IFileStorageService fileStorageSvc;
public NuGetExeDownloaderService(
IPackageService packageSvc,
IPackageFileService packageFileSvc,
IFileStorageService fileStorageSvc)
{
this.packageSvc = packageSvc;
this.packageFileSvc = packageFileSvc;
this.fileStorageSvc = fileStorageSvc;
}
public ActionResult CreateNuGetExeDownloadActionResult()
{
EnsureNuGetExe();
return fileStorageSvc.CreateDownloadFileActionResult(Constants.DownloadsFolderName, "nuget.exe");
}
public void UpdateExecutable(IPackage zipPackage)
{
lock (fileLock)
{
ExtractNuGetExe(zipPackage);
}
}
private void EnsureNuGetExe()
{
if (fileStorageSvc.FileExists(Constants.DownloadsFolderName, "nuget.exe"))
{
// Ensure the file exists on blob storage.
return;
}
lock (fileLock)
{
var package = packageSvc.FindPackageByIdAndVersion("NuGet.CommandLine", version: null, allowPrerelease: false);
if (package == null)
{
throw new InvalidOperationException("Unable to find NuGet.CommandLine.");
}
using (var packageStream = packageFileSvc.DownloadPackageFile(package))
{
var zipPackage = new ZipPackage(packageStream);
ExtractNuGetExe(zipPackage);
}
}
}
private void ExtractNuGetExe(IPackage package)
{
var executable = package.GetFiles("tools")
.First(f => f.Path.Equals(@"tools\NuGet.exe", StringComparison.OrdinalIgnoreCase));
using (Stream packageFileStream = executable.GetStream())
{
fileStorageSvc.SaveFile(Constants.DownloadsFolderName, "nuget.exe", packageFileStream);
}
}
}
}