private async Task <R <Repository> > GetFreshRepositoryAsync(
            string workingFolder, Dictionary <CommitId, GitCommit> gitCommits)
        {
            using (await syncRootAsync.LockAsync())
            {
                Log.Debug("No cached repository");
                MRepository mRepository = new MRepository();
                mRepository.WorkingFolder = workingFolder;

                mRepository.GitCommits = gitCommits ?? new Dictionary <CommitId, GitCommit>();

                Timing t = new Timing();
                await repositoryStructureService.UpdateAsync(mRepository, null, null);

                mRepository.TimeToCreateFresh = t.Elapsed;
                t.Log("Updated mRepository");

                if (mRepository.TimeToCreateFresh > MinCreateTimeBeforeCaching)
                {
                    Log.Usage($"Caching repository ({t.Elapsed} ms)");
                    await cacheService.CacheAsync(mRepository);
                }
                else
                {
                    Log.Usage($"No need for cached repository ({t.Elapsed} ms)");
                    cacheService.TryDeleteCache(workingFolder);
                }

                Repository repository = ToRepository(mRepository);
                t.Log($"Repository {repository.Branches.Count} branches, {repository.Commits.Count} commits");

                return(repository);
            }
        }
예제 #2
0
 public Task CacheAsync <TEntity>(TEntity targetObject) where TEntity : class
 => _storingService.CacheAsync(targetObject);
예제 #3
0
        public CachePackageModule(ICacheService mirror)
            : base("api/cache/v3/package")
        {
            _mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));

            this.Get("/{id}/index.json", async(req, res, routeData) => {
                string id = routeData.As <string>("id");
                IReadOnlyList <string> upstreamVersions = await _mirror.FindUpstreamAsync(id, CancellationToken.None);
                if (upstreamVersions.Any())
                {
                    await res.AsJson(new
                    {
                        Versions = upstreamVersions.ToList()
                    });
                    return;
                }
                res.StatusCode = 404;
            });

            this.Get("/{id}/{version}/{idVersion}.nupkg", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);
                await _mirror.CacheAsync(identity, CancellationToken.None);

                var packageStream = await _mirror.GetPackageStreamAsync(identity);

                await res.FromStream(packageStream, "application/octet-stream");
            });

            this.Get("/{id}/{version}/{id2}.nuspec", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);
                await _mirror.CacheAsync(identity, CancellationToken.None);

                if (!await _mirror.ExistsAsync(new PackageIdentity(id, nugetVersion)))
                {
                    res.StatusCode = 404;
                    return;
                }
                await res.FromStream(await _mirror.GetNuspecStreamAsync(identity), "text/xml");
            });

            this.Get("/{id}/{version}/readme", async(req, res, routeData) => {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                var identity = new PackageIdentity(id, nugetVersion);
                await _mirror.CacheAsync(identity, CancellationToken.None);

                if (!await _mirror.ExistsAsync(new PackageIdentity(id, nugetVersion)))
                {
                    res.StatusCode = 404;
                    return;
                }



                await res.FromStream(await _mirror.GetReadmeStreamAsync(identity), "text/markdown");
            });
        }
예제 #4
0
        public CacheRegistrationIndexModule(ICacheService mirror)
        {
            this._mirror = mirror ?? throw new ArgumentNullException(nameof(mirror));
            this.Get("api/cache/v3/registration/{id}/index.json", async(req, res, routeData) =>
            {
                string id = routeData.As <string>("id");
                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var upstreamPackages = (await _mirror.FindUpstreamMetadataAsync(id, CancellationToken.None)).ToList();
                var versions         = upstreamPackages.Select(p => p.Identity.Version).ToList();

                if (!upstreamPackages.Any())
                {
                    res.StatusCode = 404;
                    return;
                }

                // TODO: Paging of registration items.
                // "Un-paged" example: https://api.nuget.org/v3/registration3/newtonsoft.json/index.json
                // Paged example: https://api.nuget.org/v3/registration3/fake/index.json
                await res.AsJson(new
                {
                    Count          = upstreamPackages.Count,
                    TotalDownloads = upstreamPackages.Sum(p => p.DownloadCount),
                    Items          = new[]
                    {
                        new RegistrationIndexItem(
                            packageId: id,
                            items: upstreamPackages.Select(p => ToRegistrationIndexLeaf(req, p)).ToList(),
                            lower: versions.Min().ToNormalizedString(),
                            upper: versions.Max().ToNormalizedString()
                            ),
                    }
                });
            });

            this.Get("api/cache/v3/registration/{id}/{version}.json", async(req, res, routeData) =>
            {
                string id      = routeData.As <string>("id");
                string version = routeData.As <string>("version");

                if (!NuGetVersion.TryParse(version, out var nugetVersion))
                {
                    res.StatusCode = 400;
                    return;
                }

                var pid = new PackageIdentity(id, nugetVersion);

                // Allow read-through caching to happen if it is confiured.
                await _mirror.CacheAsync(pid, CancellationToken.None);

                var package = await _mirror.FindAsync(new PackageIdentity(id, nugetVersion));

                if (package == null)
                {
                    res.StatusCode = 404;
                    return;
                }

                // Documentation: https://docs.microsoft.com/en-us/nuget/api/registration-base-url-resource
                var result = new RegistrationLeaf(
                    registrationUri: req.PackageRegistration(pid.Id, prefix),
                    listed: package.IsListed,
                    downloads: package.DownloadCount.GetValueOrDefault(),
                    packageContentUri: req.PackageDownload(pid, prefix),
                    published: package.Published.GetValueOrDefault(),
                    registrationIndexUri: req.PackageRegistration(id, prefix));

                await res.AsJson(result);
            });
        }