Example #1
0
		private object PutPost(DeployFileDto file)
		{
			if (this.RequestContext.Files.Length == 0)
			{
				throw new Exception("Missing file data");
			}
			if (this.RequestContext.Files.Length > 1)
			{
				throw new Exception("Multiple file upload not supported");
			}
			var fileData = TempStreamHelper.ReadAllBytes(this.RequestContext.Files[0].InputStream);
			string fileName = this.RequestContext.Files[0].FileName;
			if (string.IsNullOrEmpty(file.Id) || file.Id.Equals("upload", StringComparison.CurrentCultureIgnoreCase))
			{
				return this._fileManager.CreateFile(fileName, fileData);
			}
			else
			{
				return this._fileManager.UpdateFile(file.Id, fileName, fileData);
			}
		}
Example #2
0
		public object Get(DeployFileDto request)
		{
			if (!string.IsNullOrEmpty(request.Id))
			{
				var file = this._fileManager.GetFile(request.Id);
                if(request.RawData)
                {
                    var result = new HttpResult(_fileManager.GetFileDataStream(request.Id), "application/octet-stream");
                    result.Headers.Add("Content-Disposition", "attachment; filename=" + file.FileName);
                    return result;
                }
                else 
                {
				    return file;
                }
			}
			else
			{
				return this._fileManager.GetFileList();
			}
		}
        private void _btnExport_Click(object sender, EventArgs e)
        {
            try 
            {
                if (string.IsNullOrEmpty(_txtOutputFileName.Text))
                {
                    MessageBox.Show("Please enter an output file name");
                    return;
                }
                if(_chkPublishToWebsite.Checked && string.IsNullOrEmpty(_txtPublishUrl.Text))
                {
                    MessageBox.Show("Please enter a publish URL");
                    return;
                }

                var deployStateDirectory = Path.Combine(_workingDirectory,"states");
                if(!Directory.Exists(deployStateDirectory))
                {
                    MessageBox.Show("No deploy history found");
                    return;
                }
                var stateFileList = Directory.GetFiles(deployStateDirectory, "*.json");
                if(stateFileList.Length == 0)
                {
                    MessageBox.Show("No deploy history found");
                    return;
                }
                var exportDirectory = Path.Combine(_workingDirectory, "exports", Guid.NewGuid().ToString());
                if (!Directory.Exists(exportDirectory))
                {
                    Directory.CreateDirectory(exportDirectory);
                }
                foreach (var fileName in stateFileList)
                {
                    string exportFileName = Path.Combine(exportDirectory, Path.GetFileName(fileName));
                    File.Copy(fileName, exportFileName);
                }
                string zipPath = _txtOutputFileName.Text;
                var zipper = _diFactory.CreateInjectedObject<IZipper>();
                zipper.ZipDirectory(exportDirectory, zipPath);

                if(_chkPublishToWebsite.Checked)
                {
                    var url = _txtPublishUrl.Text;
                    if (!url.EndsWith("/"))
                    {
                        url += "/";
                    }
                    if (!url.EndsWith("/api/", StringComparison.CurrentCultureIgnoreCase))
                    {
                        url += "api/";
                    }
                    var deployFile = new DeployFileDto
                    {
                        FileData = File.ReadAllBytes(zipPath),
                        FileName = Path.GetFileName(zipPath)
                    };

                    Cookie authCookie = null;
                    if (!string.IsNullOrEmpty(_txtUserName.Text) && !string.IsNullOrEmpty(_txtPassword.Text))
                    {
                        authCookie = GetAuthCookie(url, _txtUserName.Text, _txtPassword.Text);
                    }
                
                    using (var client = new JsonServiceClient(url))
			        {
				        var fileToUpload = new FileInfo(zipPath);
						client.AllowAutoRedirect = false;
				        client.Credentials = System.Net.CredentialCache.DefaultCredentials;
				        client.Timeout = TimeSpan.FromMinutes(2);
				        client.ReadWriteTimeout = TimeSpan.FromMinutes(2);
                        if(authCookie != null)
                        {  
                            client.CookieContainer.Add(authCookie);
                        }

                        var offlineDeployment = client.Get<OfflineDeployment>(url + "deploy/offline?deployBatchRequestId=" + _batchRequest.Id);
                        if(offlineDeployment == null)
                        {
                            throw new Exception("Could not find offline deployment record for batch request ID " + _batchRequest.Id);
                        }

				        var fileResponse = client.PostFile<DeployFileDto>(url + "file", fileToUpload, MimeTypes.GetMimeType(fileToUpload.Name));

                        var updateRequest = new OfflineDeployment
                        {
                            Id = offlineDeployment.Id,
                            DeployBatchRequestId = _batchRequest.Id,
                            ResultFileId = fileResponse.Id
                        };
                        client.Post<OfflineDeployment>(url + "deploy/offline", updateRequest);
                    }
                }
                MessageBox.Show("Export Complete");
            }
            catch (Exception err)
            {
                WinFormsHelper.DisplayError("Error exporting history", err);
            }
        }
		private DeployBuild PublishZip(string apiUrl, string projectId, string componentId, string branchId, string version, string zipPath, string userName, string password)
		{
			string url = apiUrl;
			if (!url.EndsWith("/"))
			{
				url += "/";
			}
			if (!url.EndsWith("/api/", StringComparison.CurrentCultureIgnoreCase))
			{
				url += "api/";
			}
			var deployFile = new DeployFileDto
			{
				FileData = File.ReadAllBytes(zipPath),
				FileName = Path.GetFileName(zipPath)
			};
			branchId = FormatBranch(branchId, version);

            Cookie authCookie = null;
            if(!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                authCookie = GetAuthCookie(apiUrl, userName, password);
            }
			using (var client = new JsonServiceClient(url))
			{
				_logger.Debug("Posting file {0} to URL {1}", zipPath, url);
				//var x = client.Send<DeployFileDto>(deployFile);
				var fileToUpload = new FileInfo(zipPath);
				string fileUrl = url + "file";
				client.Credentials = System.Net.CredentialCache.DefaultCredentials;
				client.Timeout = TimeSpan.FromMinutes(5);
				client.ReadWriteTimeout = TimeSpan.FromMinutes(5);
                if(authCookie != null)
                {  
                    client.CookieContainer.Add(authCookie);
                }
				var fileResponse = client.PostFile<DeployFileDto>(fileUrl, fileToUpload, MimeTypes.GetMimeType(fileToUpload.Name));
				_logger.Debug("Done posting file {0} to URL {1}, returned fileId {2} and fileStorageId {3}", zipPath, url, fileResponse.Id, fileResponse.FileStorageId);

				var buildRequest = new DeployBuild
				{
					FileId = fileResponse.Id,
					ProjectId = projectId,
					ProjectBranchId = branchId,
					ProjectComponentId = componentId,
					Version = version
				};
				_logger.Debug("Posting DeployBuild object to URL {0}, sending{1}", url, buildRequest.ToJson());
				string buildUrl = url + "build";
				try 
				{
					var buildResponse = client.Post<DeployBuild>(buildUrl, buildRequest);
                    _logger.Debug("Posting DeployBuild object to URL {0}, returned {1}", url, buildRequest.ToJson());
                    return buildResponse;
                }
				catch(Exception err)
				{
					_logger.WarnException(string.Format("Error posting DeployBuild object to URL {0}: {1}, ERROR: {2}", url, buildRequest.ToJson(), err.ToString()), err);
					throw;
				}
			}

		}
Example #5
0
		public object Put(DeployFileDto file)
		{
			return this.PutPost(file);
		}