Exemplo n.º 1
0
        internal static async Task <FileInfo> DownloadNupkgV2Async(
            CatalogEntry entry,
            IEnumerable <DirectoryInfo> storagePaths,
            DownloadMode mode,
            ILogger log,
            CancellationToken token)
        {
            FileInfo result = null;

            DirectoryInfo rootDir     = null;
            var           lastCreated = DateTimeOffset.MinValue;

            // Check if the nupkg already exists on another drive.
            foreach (var storagePath in storagePaths)
            {
                var checkOutputDir = Path.Combine(storagePath.FullName, entry.Id.ToLowerInvariant());
                var checkNupkgPath = Path.Combine(checkOutputDir, $"{entry.FileBaseName}.nupkg");

                if (File.Exists(checkNupkgPath))
                {
                    // Use the existing path
                    lastCreated = File.GetCreationTimeUtc(checkNupkgPath);
                    rootDir     = storagePath;
                    break;
                }
            }

            // Does not exist, use the path with the most free space.
            if (rootDir == null)
            {
                rootDir = GetPathWithTheMostFreeSpace(storagePaths);
            }

            // id/id.version.nupkg
            var outputDir = Path.Combine(rootDir.FullName, entry.Id.ToLowerInvariant());
            var nupkgPath = Path.Combine(outputDir, $"{entry.FileBaseName}.nupkg");

            // Download
            var nupkgFile = await entry.DownloadNupkgAsync(outputDir, mode, token);

            if (File.Exists(nupkgPath))
            {
                var currentCreated = File.GetCreationTimeUtc(nupkgPath);

                if (lastCreated < currentCreated)
                {
                    result = nupkgFile;
                    log.LogInformation(nupkgFile.FullName);
                }
                else
                {
                    log.LogDebug($"Skipping. Current file is the same or newer. {lastCreated.ToString("o")} {currentCreated.ToString("o")}" + nupkgFile.FullName);
                }
            }
            else
            {
                log.LogDebug($"Nupkg skipped. " + nupkgFile.FullName);
            }

            return(result);
        }
Exemplo n.º 2
0
        internal static async Task <FileInfo> DownloadNupkgV3Async(
            CatalogEntry entry,
            IEnumerable <DirectoryInfo> storagePaths,
            DownloadMode mode,
            ILogger log,
            ILogger deepLog,
            CancellationToken token)
        {
            FileInfo      result      = null;
            DirectoryInfo rootDir     = null;
            var           lastCreated = DateTimeOffset.MinValue;

            // Check if the nupkg already exists on another drive.
            foreach (var storagePath in storagePaths)
            {
                var currentResolver = new VersionFolderPathResolver(storagePath.FullName);
                var checkNupkgPath  = currentResolver.GetPackageFilePath(entry.Id, entry.Version);

                if (File.Exists(checkNupkgPath))
                {
                    // Use the existing path
                    lastCreated = File.GetCreationTimeUtc(checkNupkgPath);
                    rootDir     = storagePath;
                    break;
                }
            }

            if (rootDir == null)
            {
                // Not found, use the path with the most space
                rootDir = GetPathWithTheMostFreeSpace(storagePaths);
            }

            // id/version/id.version.nupkg
            var versionFolderResolver = new VersionFolderPathResolver(rootDir.FullName);
            var outputDir             = versionFolderResolver.GetInstallPath(entry.Id, entry.Version);
            var hashPath   = versionFolderResolver.GetHashPath(entry.Id, entry.Version);
            var nuspecPath = versionFolderResolver.GetManifestFilePath(entry.Id, entry.Version);
            var nupkgPath  = versionFolderResolver.GetPackageFilePath(entry.Id, entry.Version);

            // Download
            var nupkgFile = await entry.DownloadNupkgAsync(outputDir, mode, token);

            if (File.Exists(nupkgPath))
            {
                var currentCreated = File.GetCreationTimeUtc(nupkgPath);

                // Clean up nuspec and hash if the file changed
                if (lastCreated < currentCreated || !File.Exists(hashPath) || !File.Exists(nuspecPath))
                {
                    result = nupkgFile;
                    log.LogInformation(nupkgFile.FullName);

                    FileUtility.Delete(hashPath);
                    FileUtility.Delete(nuspecPath);

                    using (var fileStream = File.OpenRead(nupkgFile.FullName))
                    {
                        var packageHash = Convert.ToBase64String(new CryptoHashProvider("SHA512").CalculateHash(fileStream));
                        fileStream.Seek(0, SeekOrigin.Begin);

                        // Write nuspec
                        using (var reader = new PackageArchiveReader(fileStream))
                        {
                            var nuspecFile = reader.GetNuspecFile();
                            reader.ExtractFile(nuspecFile, nuspecPath, deepLog);
                        }

                        // Write package hash
                        File.WriteAllText(hashPath, packageHash);
                    }
                }
            }

            return(result);
        }
        public async Task <FileInfo> PersistAsync(
            CatalogEntry entry,
            CancellationToken token
            )
        {
            var storagePaths = _storagePaths;
            var log          = _log;

            FileInfo      result      = null;
            DirectoryInfo rootDir     = null;
            var           lastCreated = DateTimeOffset.MinValue;

            // Check if the nupkg already exists on another drive.
            foreach (var storagePath in storagePaths)
            {
                var packageFilePath = GetPackageFilePath(storagePath, entry);

                if (File.Exists(packageFilePath))
                {
                    // Use the existing path
                    lastCreated = File.GetCreationTimeUtc(packageFilePath);
                    rootDir     = storagePath;
                    break;
                }
            }

            // Not found, use the path with the most space
            if (rootDir == null)
            {
                rootDir = GetPathWithTheMostFreeSpace(storagePaths);
            }

            var packageInfo = CreatePackageInfo(entry, rootDir);

            var nupkgFile = new FileInfo(packageInfo.NupkgPath);

            if (_options.SkipExisting && nupkgFile.Exists)
            {
                log.LogInformation($"SKIPPED (package exists): {packageInfo.NupkgPath}");
            }
            else
            {
                // Download
                nupkgFile = await entry.DownloadNupkgAsync(packageInfo.OutputDir, _mode, token);

                if (File.Exists(packageInfo.NupkgPath))
                {
                    var currentCreated = File.GetCreationTimeUtc(packageInfo.NupkgPath);

                    if (ShouldPersistPackageFile(lastCreated, currentCreated, packageInfo))
                    {
                        result = nupkgFile;
                        log.LogInformation($"DOWNLOADED: {nupkgFile.FullName}");

                        PersistPackageFile(nupkgFile, packageInfo);
                    }
                    else
                    {
                        log.LogDebug($"SKIPPED (current file is the same or newer): {lastCreated.ToString("o")} {currentCreated.ToString("o")} {nupkgFile.FullName}");
                    }
                }
                else
                {
                    log.LogDebug($"SKIPPED (download failure?): {nupkgFile.FullName}");
                }
            }

            return(result);
        }