private PackageFromBuiltInFeedResource AttemptDeltaPush(string fileName, Stream contents, bool replaceExisting)
        {
            if (!repository.HasLink("PackageDeltaSignature"))
            {
                Logger.Info("Server does not support delta compression for package push");
                return(null);
            }

            if (!PackageIdentityParser.TryParsePackageIdAndVersion(Path.GetFileNameWithoutExtension(fileName), out var packageId, out var version))
            {
                Logger.Info("Could not determine the package ID and/or version based on the supplied filename");
                return(null);
            }

            PackageSignatureResource signatureResult;

            try
            {
                Logger.Info($"Requesting signature for delta compression from the server for upload of a package with id '{packageId}' and version '{version}'");
                signatureResult = repository.Client.Get <PackageSignatureResource>(repository.Link("PackageDeltaSignature"), new { packageId, version });
            }
            catch (OctopusResourceNotFoundException)
            {
                Logger.Info("No package with the same ID exists on the server");
                return(null);
            }

            using (var deltaTempFile = new TemporaryFile())
            {
                var shouldUpload = DeltaCompression.CreateDelta(contents, signatureResult, deltaTempFile.FileName);
                if (!shouldUpload)
                {
                    return(null);
                }

                using (var delta = File.OpenRead(deltaTempFile.FileName))
                {
                    var result = repository.Client.Post <FileUpload, PackageFromBuiltInFeedResource>(
                        repository.Link("PackageDeltaUpload"),
                        new FileUpload()
                    {
                        Contents = delta, FileName = Path.GetFileName(fileName)
                    },
                        new { replace = replaceExisting, packageId, signatureResult.BaseVersion });

                    Logger.Info($"Delta transfer completed");
                    return(result);
                }
            }
        }
Example #2
0
        private async Task <FeedPackageResult> AttemptDeltaPush(Stream stream, string originalFilename, bool replace, string region)
        {
            var(packageId, version) = PackageIdAndVersionParser.Parse(Path.GetFileNameWithoutExtension(originalFilename));

            try
            {
                _client.Log.Info($"Requesting signature for delta compression from the server for upload of a package '{packageId}' version '{version}'");

                var signature = await _client.FeedClientWrapper.Get <PackageDeltaSignatureResult>(
                    UrlTemplate.Resolve(
                        _feedRootUri + "/packages/{packageId}/{version}/delta-signature",
                        new { packageId, version }
                        )
                    );

                var tempFile = Path.GetTempFileName();
                try
                {
                    var shouldUpload = DeltaCompression.CreateDelta(_client.Log, stream, signature, tempFile);
                    if (!shouldUpload)
                    {
                        return(null);
                    }

                    using (var delta = File.OpenRead(tempFile))
                    {
                        var result = await _client.FeedClientWrapper.Create <FeedPackageResult>(
                            UrlTemplate.Resolve(
                                _feedRootUri + "/packages/{packageId}/{baseVersion}/delta{?replace,region}",
                                new { packageId, baseVersion = signature.BaseVersion, replace, region }
                                ),
                            delta,
                            originalFilename,
                            new Dictionary <string, string>()
                            );

                        _client.Log.Info($"Delta transfer completed");
                        return(result);
                    }
                }
                finally
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch
                    {
                    }
                }
            }
            catch (FeedzHttpRequestException hre) when(hre.Code == HttpStatusCode.NotFound)
            {
                _client.Log.Info("No package with the same ID exists on the server");
                return(null);
            }
            catch (FeedzHttpRequestException hre) when(hre.Code == HttpStatusCode.Conflict)
            {
                throw new Exception("The package already exists");
            }
            catch (Exception)
            {
                _client.Log.Info("An error occured calculating the package delta");
                return(null);
            }
        }
        private PackageFromBuiltInFeedResource AttemptDeltaPush(string fileName, Stream contents, OverwriteMode overwriteMode)
        {
            if (!repository.HasLink("PackageDeltaSignature"))
            {
                Logger.Info("Server does not support delta compression for package push");
                return(null);
            }

            if (!PackageIdentityParser.TryParsePackageIdAndVersion(Path.GetFileNameWithoutExtension(fileName), out var packageId, out var version))
            {
                Logger.Info("Could not determine the package ID and/or version based on the supplied filename");
                return(null);
            }

            PackageSignatureResource signatureResult;

            try
            {
                Logger.Info($"Requesting signature for delta compression from the server for upload of a package with id '{packageId}' and version '{version}'");
                signatureResult = repository.Client.Get <PackageSignatureResource>(repository.Link("PackageDeltaSignature"), new { packageId, version });
            }
            catch (OctopusResourceNotFoundException)
            {
                Logger.Info("No package with the same ID exists on the server");
                return(null);
            }

            using (var deltaTempFile = new TemporaryFile())
            {
                var shouldUpload = DeltaCompression.CreateDelta(contents, signatureResult, deltaTempFile.FileName);
                if (!shouldUpload)
                {
                    return(null);
                }

                using (var delta = File.OpenRead(deltaTempFile.FileName))
                {
                    var    link = repository.Link("PackageDeltaUpload");
                    object pathParameters;

                    // if the link contains overwriteMode then we're connected to a new server, if not use the old `replace` parameter
                    if (link.Contains(OverwriteModeLink.Link))
                    {
                        pathParameters = new { overwriteMode = overwriteMode, packageId, signatureResult.BaseVersion };
                    }
                    else
                    {
                        pathParameters = new { replace = overwriteMode.ConvertToLegacyReplaceFlag(Logger), packageId, signatureResult.BaseVersion };
                    }

                    var result = repository.Client.Post <FileUpload, PackageFromBuiltInFeedResource>(
                        link,
                        new FileUpload()
                    {
                        Contents = delta, FileName = Path.GetFileName(fileName)
                    },
                        pathParameters);

                    Logger.Info($"Delta transfer completed");
                    return(result);
                }
            }
        }