public static void GenerateNupkgMetadataFile(string nupkgPath, string installPath, string hashPath, string nupkgMetadataPath) { ConcurrencyUtilities.ExecuteWithFileLocked(nupkgPath, action: () => { // make sure new hash file doesn't exists within File lock before actually creating it. if (!File.Exists(nupkgMetadataPath)) { var tempNupkgMetadataFilePath = Path.Combine(installPath, Path.GetRandomFileName()); using (var stream = File.Open(nupkgPath, FileMode.Open, FileAccess.Read, FileShare.Read)) using (var packageReader = new PackageArchiveReader(stream)) { // get hash of unsigned content of signed package var packageHash = packageReader.GetContentHashForSignedPackage(CancellationToken.None); // if null, then it's unsigned package so just read the existing sha512 file if (string.IsNullOrEmpty(packageHash)) { packageHash = File.ReadAllText(hashPath); } // write the new hash file var hashFile = new NupkgMetadataFile() { ContentHash = packageHash }; NupkgMetadataFileFormat.Write(tempNupkgMetadataFilePath, hashFile); File.Move(tempNupkgMetadataFilePath, nupkgMetadataPath); } } }); }
public async Task GetDownloadResourceResultAsync_V3_DownloadsPackage() { // Arrange var sourceRepositoryProvider = TestSourceRepositoryUtility.CreateV3OnlySourceRepositoryProvider(); var v3sourceRepository = sourceRepositoryProvider.GetRepositories().First(); var packageIdentity = new PackageIdentity("jQuery", new NuGetVersion("1.8.2")); // Act using (var cacheContext = new SourceCacheContext()) using (var packagesDirectory = TestDirectory.Create()) using (var downloadResult = await PackageDownloader.GetDownloadResourceResultAsync( v3sourceRepository, packageIdentity, new PackageDownloadContext(cacheContext), packagesDirectory, NullLogger.Instance, CancellationToken.None)) { var targetPackageStream = downloadResult.PackageStream; using (var packageArchiveReader = new PackageArchiveReader(targetPackageStream)) { var contentHash = packageArchiveReader.GetContentHashForSignedPackage(CancellationToken.None); // Assert Assert.Equal(_jQuery182ContentHash, contentHash); Assert.True(targetPackageStream.CanSeek); } } }