internal static DeployDetails ToDeployDetails(this FileInfo artefactFileInfo, string repository) { var deployDetails = new DeployDetails { //TODO : artefact path should be overwritten by config? artifactPath = artefactFileInfo.Name, file = artefactFileInfo, md5 = MD5CheckSum.GenerateMD5(artefactFileInfo.FullName), sha1 = Sha1Reference.GenerateSHA1(artefactFileInfo.FullName), targetRepository = repository }; return deployDetails; }
private void DeployArtefact(DeployDetails details) { string deploymentPath = _artifactoryUrl + "/" + details.targetRepository + "/" + details.artifactPath; _log.Info("Deploying artifact: " + deploymentPath); if (TryChecksumDeploy(details, _artifactoryUrl)) { return; } //Custom headers WebHeaderCollection headers = createHttpPutMethod(details); headers.Add(HttpRequestHeader.ContentType, "binary/octet-stream"); /* * "100 (Continue)" status is to allow a client that is sending a request message with a request body to determine if the origin server is * willing to accept the request (based on the request headers) before the client sends the request body. */ //headers.Add("Expect", "100-continue"); _httpClient.getHttpClient().SetHeader(headers); byte[] data = File.ReadAllBytes(details.file.FullName); /* Add properties to the artifact, if any */ deploymentPath = deploymentPath + details.properties; HttpResponse response = _httpClient.getHttpClient().Execute(deploymentPath, "PUT", data); //When deploying artifact, Expecting for Created (201) response from Artifactory if ((response._statusCode != HttpStatusCode.OK) && (response._statusCode != HttpStatusCode.Created)) { _log.Error("Error occurred while publishing artifact to Artifactory: " + details.file); throw new WebException("Failed to deploy file:" + response._message); } }
private bool TryChecksumDeploy(DeployDetails details, String uploadUrl) { // Try checksum deploy only on file size greater than CHECKSUM_DEPLOY_MIN_FILE_SIZE if (details.file.Length < CHECKSUM_DEPLOY_MIN_FILE_SIZE) { _log.Debug("Skipping checksum deploy of file size " + details.file.Length + " , falling back to regular deployment."); return false; } string checksumUrlPath = uploadUrl + "/" + details.targetRepository + "/" + details.artifactPath; /* Add properties to the artifact, if any */ checksumUrlPath = checksumUrlPath + details.properties; WebHeaderCollection headers = createHttpPutMethod(details); headers.Add("X-Checksum-Deploy", "true"); headers.Add(HttpRequestHeader.ContentType, "application/vnd.org.jfrog.artifactory.storage.ItemCreated+json"); _httpClient.getHttpClient().SetHeader(headers); HttpResponse response = _httpClient.getHttpClient().Execute(checksumUrlPath, "PUT"); //When sending Checksum deploy, Expecting for Created (201) or Success (200) responses from Artifactory if (response._statusCode == HttpStatusCode.Created || response._statusCode == HttpStatusCode.OK) { _log.Debug(string.Format("Successfully performed checksum deploy of file {0} : {1}", details.file.FullName, details.sha1)); return true; } else { _log.Debug(string.Format("Failed checksum deploy of checksum '{0}' with statusCode: {1}", details.sha1, response._statusCode)); } return false; }
/// <summary> /// Typical PUT header with Checksums, for deploying files to Artifactory /// </summary> private WebHeaderCollection createHttpPutMethod(DeployDetails details) { WebHeaderCollection putHeaders = new WebHeaderCollection(); putHeaders.Add("X-Checksum-Sha1", details.sha1); putHeaders.Add("X-Checksum-Md5", details.md5); return putHeaders; }