Exemplo n.º 1
0
        public override Task <DownloadResourceResult> GetDownloadResourceResultAsync(
            PackageIdentity identity,
            ISettings settings,
            ILogger logger,
            CancellationToken token)
        {
            // Find the package from the local folder
            LocalPackageInfo packageInfo = null;

            var sourcePackage = identity as SourcePackageDependencyInfo;

            if (sourcePackage?.DownloadUri != null)
            {
                // Get the package directly if the full path is known
                packageInfo = _localResource.GetPackage(sourcePackage.DownloadUri, logger, token);
            }
            else
            {
                // Search for the local package
                packageInfo = _localResource.GetPackage(identity, logger, token);
            }

            if (packageInfo != null)
            {
                var stream = File.OpenRead(packageInfo.Path);
                return(Task.FromResult(new DownloadResourceResult(stream, packageInfo.GetReader())));
            }
            else
            {
                return(Task.FromResult(new DownloadResourceResult(DownloadResourceResultStatus.NotFound)));
            }
        }
Exemplo n.º 2
0
        public override Task <DownloadResourceResult> GetDownloadResourceResultAsync(
            PackageIdentity identity,
            PackageDownloadContext downloadContext,
            string globalPackagesFolder,
            ILogger logger,
            CancellationToken token)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            var stopwatch = Stopwatch.StartNew();

            try
            {
                // Find the package from the local folder
                LocalPackageInfo packageInfo = null;

                var sourcePackage = identity as SourcePackageDependencyInfo;

                if (sourcePackage?.DownloadUri != null)
                {
                    // Get the package directly if the full path is known
                    packageInfo = _localResource.GetPackage(sourcePackage.DownloadUri, logger, token);
                }
                else
                {
                    // Search for the local package
                    packageInfo = _localResource.GetPackage(identity, logger, token);
                }

                if (packageInfo != null)
                {
                    var stream = File.OpenRead(packageInfo.Path);
                    return(Task.FromResult(new DownloadResourceResult(stream, packageInfo.GetReader(), _localResource.Root)));
                }
                else
                {
                    return(Task.FromResult(new DownloadResourceResult(DownloadResourceResultStatus.NotFound)));
                }
            }
            finally
            {
                ProtocolDiagnostics.RaiseEvent(new ProtocolDiagnosticResourceEvent(
                                                   _source,
                                                   resourceType: nameof(DownloadResource),
                                                   type: nameof(LocalDownloadResource),
                                                   method: nameof(GetDownloadResourceResultAsync),
                                                   stopwatch.Elapsed));
            }
        }
Exemplo n.º 3
0
        private const int FiveMegabytes = 5242880; // 1024 * 1024 * 5, 5MB

        public string LoadFileAsText(string path)
        {
            string fileContent = null;

            try
            {
                if (_package.GetReader() is PackageArchiveReader reader) // This will never be anything else in reality. The search resource always uses a PAR
                {
                    var entry = reader.GetEntry(PathUtility.StripLeadingDirectorySeparators(path));
                    if (entry != null)
                    {
                        if (entry.Length >= FiveMegabytes)
                        {
                            fileContent = string.Format(CultureInfo.CurrentCulture, Strings.LoadFileFromNupkg_FileTooLarge, path, "5");
                        }
                        else
                        {
                            using (var licenseStream = entry.Open())
                                using (TextReader textReader = new StreamReader(licenseStream))
                                {
                                    fileContent = textReader.ReadToEnd();
                                }
                        }
                    }
                    else
                    {
                        fileContent = string.Format(CultureInfo.CurrentCulture, Strings.LoadFileFromNupkg_FileNotFound, path);
                    }
                }
            }
            catch
            {
                fileContent = string.Format(CultureInfo.CurrentCulture, Strings.LoadFileFromNupkg_UnknownProblemLoadingTheFile, path);
            }
            finally
            {
                if (fileContent == null)
                {
                    fileContent = string.Format(CultureInfo.CurrentCulture, Strings.LoadFileFromNupkg_UnknownProblemLoadingTheFile, path);
                }
            }
            return(fileContent);
        }
Exemplo n.º 4
0
        public override Task <DownloadResourceResult> GetDownloadResourceResultAsync(
            PackageIdentity identity,
            PackageDownloadContext downloadContext,
            string globalPackagesFolder,
            ILogger logger,
            CancellationToken token)
        {
            if (identity == null)
            {
                throw new ArgumentNullException(nameof(identity));
            }

            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }

            // Find the package from the local folder
            LocalPackageInfo packageInfo = null;

            var sourcePackage = identity as SourcePackageDependencyInfo;

            if (sourcePackage?.DownloadUri != null)
            {
                // Get the package directly if the full path is known
                packageInfo = _localResource.GetPackage(sourcePackage.DownloadUri, logger, token);
            }
            else
            {
                // Search for the local package
                packageInfo = _localResource.GetPackage(identity, logger, token);
            }

            if (packageInfo != null)
            {
                var stream = File.OpenRead(packageInfo.Path);
                return(Task.FromResult(new DownloadResourceResult(stream, packageInfo.GetReader(), _localResource.Root)));
            }
            else
            {
                return(Task.FromResult(new DownloadResourceResult(DownloadResourceResultStatus.NotFound)));
            }
        }