Exemple #1
0
        private bool DownloadSinglePackage(PackageItem pkgItem, NuGetRequest request, string destLocation, ProgressTracker progressTracker)
        {
            try
            {
                request.Debug(Messages.DebugInfoCallMethod, "NuGetFilesFeed3", "DownloadSinglePackage");
                if (string.IsNullOrWhiteSpace(pkgItem.PackageFilename) || pkgItem.PackageSource == null || pkgItem.PackageSource.Location == null ||
                    (pkgItem.PackageSource.IsSourceAFile && pkgItem.Package == null))
                {
                    request.WriteError(ErrorCategory.ObjectNotFound, pkgItem.Id, Constants.Messages.UnableToResolvePackage, pkgItem.Id);
                    return(false);
                }

                // this is if the user says -force
                bool force = request.GetOptionValue("Force") != null;

                // combine the path and the file name
                destLocation = Path.Combine(destLocation, pkgItem.PackageFilename);

                // if the file already exists
                if (File.Exists(destLocation))
                {
                    // if no force, just return
                    if (!force)
                    {
                        request.Verbose(Constants.Messages.SkippedDownloadedPackage, pkgItem.Id);
                        request.YieldPackage(pkgItem, pkgItem.PackageSource.Name);
                        return(true);
                    }

                    // here we know it is forced, so delete
                    FileUtility.DeleteFile(destLocation, isThrow: false);

                    // if after we try delete, it is still there, tells the user we can't perform the action
                    if (File.Exists(destLocation))
                    {
                        request.WriteError(ErrorCategory.ResourceUnavailable, destLocation, Constants.Messages.UnableToOverwriteExistingFile, destLocation);
                        return(false);
                    }
                }

                bool downloadSuccessful = false;

                try
                {
                    // if no repository, we can't do anything
                    if (pkgItem.PackageSource.Repository == null)
                    {
                        return(false);
                    }

                    if (pkgItem.PackageSource.Repository.IsFile)
                    {
                        using (var input = File.OpenRead(pkgItem.Package.FullFilePath))
                        {
                            using (var output = new FileStream(destLocation, FileMode.Create, FileAccess.Write, FileShare.Read))
                            {
                                input.CopyTo(output);
                                downloadSuccessful = true;
                            }
                        }
                    }
                    else
                    {
                        string httpquery = MakeDownloadUri(pkgItem);
                        if (!String.IsNullOrEmpty(httpquery))
                        {
                            downloadSuccessful = NuGetClient.DownloadPackage(pkgItem.Id, pkgItem.Version, destLocation, httpquery, request, pkgItem.PackageSource, progressTracker);
                        }
                        else
                        {
                            downloadSuccessful = false;
                            request.Warning(Messages.FailedToCreateDownloadUri, pkgItem.Id, pkgItem.Version);
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.Dump(request);
                    return(false);
                }

                if (downloadSuccessful)
                {
                    request.Verbose(Resources.Messages.SuccessfullyDownloaded, pkgItem.Id);
                    // provide the directory we save to to yieldpackage
                    request.YieldPackage(pkgItem, pkgItem.PackageSource.Name, Path.GetDirectoryName(destLocation));
                }

                return(downloadSuccessful);
            } finally
            {
                request.Debug(Messages.DebugInfoReturnCall, "NuGetFilesFeed3", "DownloadSinglePackage");
            }
        }