Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ public class AppVersion {
private final AippFlowDefinitionService aippFlowDefinitionService;
private final FlowDefinitionService flowDefinitionService;
private final KnowledgeCenterService knowledgeCenterService;
private final String resourcePath;

AppVersion(AppBuilderAppPo data, Dependencies dependencies) {
this.data = data;
Expand Down Expand Up @@ -223,6 +224,7 @@ public class AppVersion {
this.maxQuestionLen = dependencies.getMaxQuestionLen();
this.maxUserContextLen = dependencies.getMaxUserContextLen();
this.knowledgeCenterService = dependencies.getKnowledgeCenterService();
this.resourcePath = dependencies.getResourcePath();
}

/**
Expand Down Expand Up @@ -879,7 +881,7 @@ public void importData(AppExportDto appDto, String appSuiteId, String contextRoo
this.formProperties = AppImExportUtil.getFormProperties(this.config.getConfigProperties());

// 对于有头像的应用数据,需要保存头像文件
String iconPath = appDto.getIconPath(contextRoot, context);
String iconPath = appDto.getIconPath(contextRoot, this.resourcePath, context);
if (!StringUtils.isBlank(iconPath)) {
this.setIcon(iconPath);
this.uploadedFileManageService.addFileRecord(this.getData().getAppId(), context.getAccount(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class AppVersionFactory {
private final Integer maxQuestionLen;
private final Integer maxUserContextLen;
private final KnowledgeCenterService knowledgeCenterService;
private final String resourcePath;

public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository, AppTaskService appTaskService,
AppBuilderConfigRepository configRepository, AppBuilderFormRepository formRepository,
Expand All @@ -81,7 +82,8 @@ public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository
FlowDefinitionService flowDefinitionService,
@Value("${app-engine.question.max-length}") Integer maxQuestionLen,
@Value("${app-engine.user-context.max-length}") Integer maxUserContextLen,
KnowledgeCenterService knowledgeCenterService) {
KnowledgeCenterService knowledgeCenterService,
@Value("${app-engine.resource.path}") String resourcePath) {
this.formPropertyRepository = formPropertyRepository;
this.appTaskService = appTaskService;
this.configRepository = configRepository;
Expand All @@ -106,6 +108,7 @@ public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository
this.maxQuestionLen = maxQuestionLen != null ? maxQuestionLen : 20000;
this.maxUserContextLen = maxUserContextLen != null ? maxUserContextLen : 500;
this.knowledgeCenterService = knowledgeCenterService;
this.resourcePath = resourcePath;
}

/**
Expand Down Expand Up @@ -142,6 +145,7 @@ public AppVersion create(AppBuilderAppPo data, AppVersionRepository appVersionRe
.maxQuestionLen(this.maxQuestionLen)
.maxUserContextLen(this.maxUserContextLen)
.knowledgeCenterService(this.knowledgeCenterService)
.resourcePath(this.resourcePath)
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ public class Dependencies {
private Integer maxQuestionLen;
private Integer maxUserContextLen;
private KnowledgeCenterService knowledgeCenterService;
private String resourcePath;
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,27 @@ public class AppExportDto {
AppExportFlowGraph flowGraph;

/**
* 获取icon路径.
* 获取头像文件的路径。
*
* @param contextRoot 请求上下文根
* @param context 操作人上下文信息.
* @return {@link String} icon路径.
* @param contextRoot 表示请求上下文根的 {@link String}。
* @param context 表示操作人上下文信息的 {@link String}。
* @param resourcePath 表示资源目录的 {@link String}。
* @return 表示获取到的头像文件的路径的 {@link String}。
*/
@JsonIgnore
public String getIconPath(String contextRoot, OperationContext context) {
public String getIconPath(String contextRoot, String resourcePath, OperationContext context) {
Object iconAttr = this.app.getAttributes().get("icon");
String iconContent = iconAttr instanceof Map ? ObjectUtils.cast(
ObjectUtils.<Map<String, Object>>cast(iconAttr).get("content")) : StringUtils.EMPTY;
if (StringUtils.isBlank(iconContent)) {
return iconContent;
}
String iconExtension = ObjectUtils.cast(ObjectUtils.<Map<String, Object>>cast(iconAttr).get("type"));
return AppImExportUtil.saveIconFile(iconContent, iconExtension, context.getTenantId(), contextRoot);
return AppImExportUtil.saveIconFile(iconContent,
iconExtension,
context.getTenantId(),
contextRoot,
resourcePath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public class FileServiceImpl implements FileService, CustomResourceHandler {
private final String formFullPath;
private final String pathPrefix;
private final String groupName;
private final String resourcePathPrefix;
private final String resourceUrlPrefix;

private final HttpClassicClientFactory httpClassicClientFactory;

Expand All @@ -111,7 +111,7 @@ public FileServiceImpl(HttpClassicClientFactory httpClassicClientFactory,
@Value("${app-engine.form.temporary-path}") String temporaryPath,
@Value("${app-engine.form.group-name}") String groupName,
@Value("${app-engine.form.path}") String formPath,
@Value("${app-engine.resource.path-prefix}") String resourcePathPrefix) {
@Value("${app-engine.resource.url-prefix}") String resourceUrlPrefix) {
this.httpClassicClientFactory = httpClassicClientFactory;
this.imageGenModelUrl = imageGenModelUrl;
this.imageGenModel = imageGenModel;
Expand All @@ -122,7 +122,7 @@ public FileServiceImpl(HttpClassicClientFactory httpClassicClientFactory,
this.formFullPath = pathPrefix + formPath;
this.formFullTemporaryPath = pathPrefix + temporaryPath;
this.groupName = groupName;
this.resourcePathPrefix = resourcePathPrefix;
this.resourceUrlPrefix = resourceUrlPrefix;
}

@Override
Expand Down Expand Up @@ -326,12 +326,12 @@ public FileEntity getSmartFormTemplate(HttpClassicServerRequest httpRequest, Ope
public FileEntity handle(String positionName, HttpClassicServerRequest request,
HttpClassicServerResponse response) {
String requestPath = request.path();
int urlPathPrefixIndex = requestPath.indexOf(this.resourcePathPrefix);
int urlPathPrefixIndex = requestPath.indexOf(this.resourceUrlPrefix);
if (urlPathPrefixIndex == -1 || requestPath.contains("..")) {
log.error("Url is invalid. Url={}", requestPath);
throw new IllegalArgumentException(requestPath);
}
String formPath = requestPath.substring(urlPathPrefixIndex + this.resourcePathPrefix.length());
String formPath = requestPath.substring(urlPathPrefixIndex + this.resourceUrlPrefix.length());
String handledFormFullPath = this.getFormFullPath(formPath);
Path path = Paths.get(handledFormFullPath);
if (!path.toFile().exists()) {
Expand All @@ -356,7 +356,7 @@ public FileEntity handle(String positionName, HttpClassicServerRequest request,

@Override
public boolean canHandle(String positionName, HttpClassicServerRequest request) {
int urlPathPrefixIndex = request.path().indexOf(this.resourcePathPrefix);
int urlPathPrefixIndex = request.path().indexOf(this.resourceUrlPrefix);
return urlPathPrefixIndex != -1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,16 +531,18 @@ public static AppBuilderFormProperty convertToAppBuilderFormProperty(AppExportFo
* @param iconExtension 表示图像类型后缀的 {@link String}。
* @param tenantId 表示租户 id 的 {@link String}。
* @param contextRoot 表示请求上下文根的 {@link String}。
* @param resourcePath 表示资源目录的 {@link String}。
* @return 表示构造好的图像的路径,可以存放在 attribute 中的 {@link String}。
*/
public static String saveIconFile(String iconContent, String iconExtension, String tenantId, String contextRoot) {
public static String saveIconFile(String iconContent, String iconExtension, String tenantId, String contextRoot,
String resourcePath) {
boolean isValidExtension = Stream.of(LEGAL_ICON_TYPE)
.anyMatch(type -> StringUtils.equalsIgnoreCase(type, iconExtension));
if (!isValidExtension) {
return StringUtils.EMPTY;
}
String newFileName = UUIDUtil.uuid() + "." + iconExtension;
File iconFile = Paths.get(NAS_SHARE_DIR, newFileName).toFile();
File iconFile = Paths.get(resourcePath, newFileName).toFile();
byte[] iconBytes = iconContent.getBytes(StandardCharsets.UTF_8);
try (InputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(iconBytes))) {
FileUtils.copyInputStreamToFile(inputStream, iconFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ app-engine:
exclude-names:
- 'i18n_appBuilder_*demo*'
resource:
path-prefix: /static/
url-prefix: /static/
path: /var/share
form:
create:
maximumNum: 400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void setUp() {
this.aippModelCenter,
converterFactory,
this.aippFlowDefinitionService,
this.flowDefinitionService, 20000, 300, this.knowledgeCenterService);
this.flowDefinitionService, 20000, 300, this.knowledgeCenterService, "/var/share");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ public static AppVersion mockAppVersion(AppBuilderAppPo appPo) {
null,
20000,
300,
null);
null,
"/var/share");
if (StringUtils.isBlank(appPo.getConfigId())) {
appPo.setConfigId("defaultConfigId");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ void testSaveIconFile() throws IOException {
String iconContent =
new String(Base64.getEncoder().encode("This is an icon png.".getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "png", "123", "/api/jober");
String tempDir = System.getProperty("java.io.tmpdir");
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "png", "123", "/api/jober", tempDir);
assertThat(iconUrl).startsWith("/api/jober/v1/api/123");

String iconPath = AippFileUtils.getFileNameFromIcon(iconUrl);
Expand All @@ -199,10 +200,11 @@ void testIllegalIconSave() {
String iconContent =
new String(Base64.getEncoder().encode("This is an icon png.".getBytes(StandardCharsets.UTF_8)),
StandardCharsets.UTF_8);
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "txt", "123", "/api/jober");
String tempDir = System.getProperty("java.io.tmpdir");
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "txt", "123", "/api/jober", tempDir);
assertThat(iconUrl).isEqualTo(StringUtils.EMPTY);

iconUrl = AppImExportUtil.saveIconFile(iconContent, "../../../.jpg", "123", "/api/jober");
iconUrl = AppImExportUtil.saveIconFile(iconContent, ".jpg", "123", "/api/jober", tempDir);
assertThat(iconUrl).isEqualTo(StringUtils.EMPTY);
}

Expand Down