Skip to content

Commit

Permalink
Merge branch 'FileContentExplorer'
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillkrylov committed Dec 14, 2023
2 parents 2f4a069 + b88e75f commit 0f6ad7c
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 2 deletions.
2 changes: 1 addition & 1 deletion clio/Command/GetVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public bool Runtime

public class GetVersionCommand : Command<GetVersionOptions>
{
private const string _gateVersion = "2.0.0.15";
private const string _gateVersion = "6.0.2.21";
public override int Execute(GetVersionOptions options)
{
if (options is object && options.Clio)
Expand Down
Binary file modified clio/cliogate/cliogate.gz
Binary file not shown.
Binary file modified clio/cliogate/cliogate_netcore.gz
Binary file not shown.
138 changes: 138 additions & 0 deletions cliogate/Files/cs/BpmcliApiGateway.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
Expand All @@ -11,13 +12,16 @@
using Common.Logging;
using Newtonsoft.Json;
using Terrasoft.Common;
using Terrasoft.Core;
using Terrasoft.Core.Configuration;
using Terrasoft.Core.ConfigurationBuild;
using Terrasoft.Core.DB;
using Terrasoft.Core.Entities;
using Terrasoft.Core.Factories;
using Terrasoft.Core.Packages;
using Terrasoft.Core.ServiceModelContract;
using Terrasoft.Web.Common;
using Terrasoft.Web.Http.Abstractions;
#if NETSTANDARD2_0
using System.Globalization;
using Terrasoft.Web.Http.Abstractions;
Expand Down Expand Up @@ -331,6 +335,23 @@ public bool ResetSchemaChangeState(string packageName){
return true;
}

// http://kkrylovn.tscrm.com:40050/rest/CreatioApiGateway/SavePackageFileContent?packageName=CrtBase&filePath=descriptor12345.json&fileContent=123
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public BaseResponse SavePackageFileContent(string packageName, string filePath, string fileContent){
CheckCanManageSolution();

PackageExplorer packageExplorer = new PackageExplorer(packageName);
var result = packageExplorer.SaveFileContent(filePath, fileContent);
return new BaseResponse {
Success = result.isSuccess,
ErrorInfo = new ErrorInfo() {
Message = result.ex?.Message,
StackTrace = result.ex?.StackTrace
}
};
}

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UnlockPackages",
BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json,
Expand Down Expand Up @@ -373,10 +394,69 @@ public bool UpdateDBStructure(){
return CreateInstallUtilities().SaveSchemaDBStructure(invalidSchemas, true);
}

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public BaseResponse UploadFile(Stream stream){
CheckCanManageSolution();
HttpContext contextAccessor = HttpContextAccessor.GetInstance();
HeaderCollection headers = contextAccessor.Request.Headers;
const string fileNameHeader = "X-File-Name";
const string packageNameHeader = "X-Package-Name";
if(!headers.AllKeys.Contains(fileNameHeader)) {
return new BaseResponse {
Success = false,
ErrorInfo = new ErrorInfo() {
Message = $"Error: {fileNameHeader} header missing",
}
};
}
if(!headers.AllKeys.Contains(packageNameHeader)) {
return new BaseResponse {
Success = false,
ErrorInfo = new ErrorInfo() {
Message = $"Error: {packageNameHeader} header missing",
}
};
}
string filename = headers[fileNameHeader];
string packageName = headers[packageNameHeader];

PackageExplorer packageExplorer = new PackageExplorer(packageName);
packageExplorer.SaveFileContent(filename, stream);

return new BaseResponse {
Success = true
};

}
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public BaseResponse DeleteFile(string packageName, string filePath){
CheckCanManageSolution();
PackageExplorer packageExplorer = new PackageExplorer(packageName);
(bool isSuccess, Exception ex) = packageExplorer.DeleteFile(filePath);

if(!isSuccess) {
return new BaseResponse {
Success = isSuccess,
ErrorInfo = {
Message = ex.Message,
StackTrace = ex.StackTrace
}
};
}
return new BaseResponse {
Success = true
};

}
#endregion

}


public class PackageExplorer
{

Expand Down Expand Up @@ -414,6 +494,19 @@ private string PackageDirectoryPath(){
return Path.Combine(_baseDir, "Terrasoft.Configuration", "Pkg", _packageName, "Files");
}

private bool IsPackageUnlocked(string packageName){
var userConnection = ClassFactory.Get<UserConnection>();
string maintainerCode = SysSettings.GetValue(userConnection, "Maintainer", "NonImplemented");
Select select = new Select(userConnection)
.From("SysPackage").WithHints(new NoLockHint())
.Column(Func.Count("Id")).As("Count")
.Where("Name").IsEqual(Column.Parameter(packageName))
.And("InstallType").IsEqual(Column.Parameter(0))
.And("Maintainer").IsEqual(Column.Parameter(maintainerCode))
as Select;
return select.ExecuteScalar<int>() == 1;
}

#endregion

#region Methods: Public
Expand All @@ -428,8 +521,53 @@ public IEnumerable<string> GetPackageFilesDirectoryContent() =>
.GetFiles(PackageDirectoryPath(), "*.*", SearchOption.AllDirectories)
.Select(f => f.Replace(PackageDirectoryPath(), string.Empty));

public (bool isSuccess, Exception ex) SaveFileContent(string filePath, string fileContent){
CheckNameForDeniedSymbols(filePath);
if(!IsPackageUnlocked(_packageName)) {
return (false, new Exception("Cannot save file in a locked package"));
}
string fullPath = Path.Combine(PackageDirectoryPath(), filePath);
string directoryPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directoryPath)) {
Directory.CreateDirectory(directoryPath);
}
File.WriteAllText(fullPath, fileContent);
return (true, null);
}
public (bool isSuccess, Exception ex) SaveFileContent(string filePath, Stream fileContent){
CheckNameForDeniedSymbols(filePath);
if(!IsPackageUnlocked(_packageName)) {
return (false, new Exception("Cannot save file in a locked package"));
}
string fullPath = Path.Combine(PackageDirectoryPath(), filePath);
string directoryPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directoryPath)) {
Directory.CreateDirectory(directoryPath);
}
File.WriteAllBytes(fullPath, fileContent.GetAllBytes());
return (true, null);
}

#endregion

public (bool isSuccess, Exception ex) DeleteFile(string filePath){
CheckNameForDeniedSymbols(filePath);
if(!IsPackageUnlocked(_packageName)) {
return (false, new Exception("Cannot save file in a locked package"));
}
string fullPath = Path.Combine(PackageDirectoryPath(), filePath);
string directoryPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(directoryPath)) {
return (false, new DirectoryNotFoundException(directoryPath));
}
try {
File.Delete(fullPath);
return (true, null);
} catch (Exception ex) {
return (false, ex);
}
}

}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion cliogate/descriptor.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Name": "cliogate",
"Type": 0,
"ProjectPath": "",
"ModifiedOnUtc": "/Date(1701388426000)/",
"ModifiedOnUtc": "/Date(1702588124000)/",
"Maintainer": "Advanced Technologies Foundation",
"DependsOn": []
}
Expand Down

0 comments on commit 0f6ad7c

Please sign in to comment.