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); } }
public async Task <Stream> Download(string packageId, string version, string similarPackagePath = null) { if (similarPackagePath != null) { _client.Log.Info("Attempting delta compression download"); if (File.Exists(similarPackagePath)) { _client.Log.Info($"Using {similarPackagePath} as the base file"); using (var f = File.OpenRead(similarPackagePath)) { var result = await AttemptDeltaDownload(packageId, version, f); if (result != null) { return(result); } } } else if (Directory.Exists(similarPackagePath)) { var file = Directory.GetFiles(similarPackagePath, packageId + "*.*") .Select(f => { try { return(new { File = f, Version = PackageIdAndVersionParser.Parse(Path.GetFileName(f)).version }); } catch { return(null); } }) .Where(v => v != null) .OrderByDescending(v => v.Version) .FirstOrDefault(); if (file == null) { _client.Log.Info("Could not find a candidate file to base the delta on"); } else { _client.Log.Info($"Using {file.File} as the base file"); using (var f = File.OpenRead(file.File)) { var result = await AttemptDeltaDownload(packageId, version, f); if (result != null) { return(result); } } } } } _client.Log.Info($"Downloading the full {packageId} {version} package"); return(await _client.FeedClientWrapper.Get <Stream>( UrlTemplate.Resolve(_feedRootUri + "/packages/{packageId}/{version}/download", new { packageId, version }) )); }