Skip to content

Commit 72f67a9

Browse files
wuayeewuayee
andauthored
[appBuilder] 头像路径配置化 (#221)
Co-authored-by: wuayee <wuayee@noreply.gitcode.com>
1 parent c420073 commit 72f67a9

File tree

10 files changed

+40
-22
lines changed

10 files changed

+40
-22
lines changed

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/domains/appversion/AppVersion.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ public class AppVersion {
192192
private final AippFlowDefinitionService aippFlowDefinitionService;
193193
private final FlowDefinitionService flowDefinitionService;
194194
private final KnowledgeCenterService knowledgeCenterService;
195+
private final String resourcePath;
195196

196197
AppVersion(AppBuilderAppPo data, Dependencies dependencies) {
197198
this.data = data;
@@ -223,6 +224,7 @@ public class AppVersion {
223224
this.maxQuestionLen = dependencies.getMaxQuestionLen();
224225
this.maxUserContextLen = dependencies.getMaxUserContextLen();
225226
this.knowledgeCenterService = dependencies.getKnowledgeCenterService();
227+
this.resourcePath = dependencies.getResourcePath();
226228
}
227229

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

881883
// 对于有头像的应用数据,需要保存头像文件
882-
String iconPath = appDto.getIconPath(contextRoot, context);
884+
String iconPath = appDto.getIconPath(contextRoot, this.resourcePath, context);
883885
if (!StringUtils.isBlank(iconPath)) {
884886
this.setIcon(iconPath);
885887
this.uploadedFileManageService.addFileRecord(this.getData().getAppId(), context.getAccount(),

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/domains/appversion/AppVersionFactory.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public class AppVersionFactory {
6868
private final Integer maxQuestionLen;
6969
private final Integer maxUserContextLen;
7070
private final KnowledgeCenterService knowledgeCenterService;
71+
private final String resourcePath;
7172

7273
public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository, AppTaskService appTaskService,
7374
AppBuilderConfigRepository configRepository, AppBuilderFormRepository formRepository,
@@ -81,7 +82,8 @@ public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository
8182
FlowDefinitionService flowDefinitionService,
8283
@Value("${app-engine.question.max-length}") Integer maxQuestionLen,
8384
@Value("${app-engine.user-context.max-length}") Integer maxUserContextLen,
84-
KnowledgeCenterService knowledgeCenterService) {
85+
KnowledgeCenterService knowledgeCenterService,
86+
@Value("${app-engine.resource.path}") String resourcePath) {
8587
this.formPropertyRepository = formPropertyRepository;
8688
this.appTaskService = appTaskService;
8789
this.configRepository = configRepository;
@@ -106,6 +108,7 @@ public AppVersionFactory(AppBuilderFormPropertyRepository formPropertyRepository
106108
this.maxQuestionLen = maxQuestionLen != null ? maxQuestionLen : 20000;
107109
this.maxUserContextLen = maxUserContextLen != null ? maxUserContextLen : 500;
108110
this.knowledgeCenterService = knowledgeCenterService;
111+
this.resourcePath = resourcePath;
109112
}
110113

111114
/**
@@ -142,6 +145,7 @@ public AppVersion create(AppBuilderAppPo data, AppVersionRepository appVersionRe
142145
.maxQuestionLen(this.maxQuestionLen)
143146
.maxUserContextLen(this.maxUserContextLen)
144147
.knowledgeCenterService(this.knowledgeCenterService)
148+
.resourcePath(this.resourcePath)
145149
.build());
146150
}
147151
}

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/domains/appversion/Dependencies.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ public class Dependencies {
6767
private Integer maxQuestionLen;
6868
private Integer maxUserContextLen;
6969
private KnowledgeCenterService knowledgeCenterService;
70+
private String resourcePath;
7071
}

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/dto/export/AppExportDto.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,27 @@ public class AppExportDto {
4646
AppExportFlowGraph flowGraph;
4747

4848
/**
49-
* 获取icon路径.
49+
* 获取头像文件的路径。
5050
*
51-
* @param contextRoot 请求上下文根
52-
* @param context 操作人上下文信息.
53-
* @return {@link String} icon路径.
51+
* @param contextRoot 表示请求上下文根的 {@link String}。
52+
* @param context 表示操作人上下文信息的 {@link String}。
53+
* @param resourcePath 表示资源目录的 {@link String}。
54+
* @return 表示获取到的头像文件的路径的 {@link String}。
5455
*/
5556
@JsonIgnore
56-
public String getIconPath(String contextRoot, OperationContext context) {
57+
public String getIconPath(String contextRoot, String resourcePath, OperationContext context) {
5758
Object iconAttr = this.app.getAttributes().get("icon");
5859
String iconContent = iconAttr instanceof Map ? ObjectUtils.cast(
5960
ObjectUtils.<Map<String, Object>>cast(iconAttr).get("content")) : StringUtils.EMPTY;
6061
if (StringUtils.isBlank(iconContent)) {
6162
return iconContent;
6263
}
6364
String iconExtension = ObjectUtils.cast(ObjectUtils.<Map<String, Object>>cast(iconAttr).get("type"));
64-
return AppImExportUtil.saveIconFile(iconContent, iconExtension, context.getTenantId(), contextRoot);
65+
return AppImExportUtil.saveIconFile(iconContent,
66+
iconExtension,
67+
context.getTenantId(),
68+
contextRoot,
69+
resourcePath);
6570
}
6671

6772
/**

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/service/impl/FileServiceImpl.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public class FileServiceImpl implements FileService, CustomResourceHandler {
9595
private final String formFullPath;
9696
private final String pathPrefix;
9797
private final String groupName;
98-
private final String resourcePathPrefix;
98+
private final String resourceUrlPrefix;
9999

100100
private final HttpClassicClientFactory httpClassicClientFactory;
101101

@@ -111,7 +111,7 @@ public FileServiceImpl(HttpClassicClientFactory httpClassicClientFactory,
111111
@Value("${app-engine.form.temporary-path}") String temporaryPath,
112112
@Value("${app-engine.form.group-name}") String groupName,
113113
@Value("${app-engine.form.path}") String formPath,
114-
@Value("${app-engine.resource.path-prefix}") String resourcePathPrefix) {
114+
@Value("${app-engine.resource.url-prefix}") String resourceUrlPrefix) {
115115
this.httpClassicClientFactory = httpClassicClientFactory;
116116
this.imageGenModelUrl = imageGenModelUrl;
117117
this.imageGenModel = imageGenModel;
@@ -122,7 +122,7 @@ public FileServiceImpl(HttpClassicClientFactory httpClassicClientFactory,
122122
this.formFullPath = pathPrefix + formPath;
123123
this.formFullTemporaryPath = pathPrefix + temporaryPath;
124124
this.groupName = groupName;
125-
this.resourcePathPrefix = resourcePathPrefix;
125+
this.resourceUrlPrefix = resourceUrlPrefix;
126126
}
127127

128128
@Override
@@ -326,12 +326,12 @@ public FileEntity getSmartFormTemplate(HttpClassicServerRequest httpRequest, Ope
326326
public FileEntity handle(String positionName, HttpClassicServerRequest request,
327327
HttpClassicServerResponse response) {
328328
String requestPath = request.path();
329-
int urlPathPrefixIndex = requestPath.indexOf(this.resourcePathPrefix);
329+
int urlPathPrefixIndex = requestPath.indexOf(this.resourceUrlPrefix);
330330
if (urlPathPrefixIndex == -1 || requestPath.contains("..")) {
331331
log.error("Url is invalid. Url={}", requestPath);
332332
throw new IllegalArgumentException(requestPath);
333333
}
334-
String formPath = requestPath.substring(urlPathPrefixIndex + this.resourcePathPrefix.length());
334+
String formPath = requestPath.substring(urlPathPrefixIndex + this.resourceUrlPrefix.length());
335335
String handledFormFullPath = this.getFormFullPath(formPath);
336336
Path path = Paths.get(handledFormFullPath);
337337
if (!path.toFile().exists()) {
@@ -356,7 +356,7 @@ public FileEntity handle(String positionName, HttpClassicServerRequest request,
356356

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

app-builder/jane/plugins/aipp-plugin/src/main/java/modelengine/fit/jober/aipp/util/AppImExportUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,16 +531,18 @@ public static AppBuilderFormProperty convertToAppBuilderFormProperty(AppExportFo
531531
* @param iconExtension 表示图像类型后缀的 {@link String}。
532532
* @param tenantId 表示租户 id 的 {@link String}。
533533
* @param contextRoot 表示请求上下文根的 {@link String}。
534+
* @param resourcePath 表示资源目录的 {@link String}。
534535
* @return 表示构造好的图像的路径,可以存放在 attribute 中的 {@link String}。
535536
*/
536-
public static String saveIconFile(String iconContent, String iconExtension, String tenantId, String contextRoot) {
537+
public static String saveIconFile(String iconContent, String iconExtension, String tenantId, String contextRoot,
538+
String resourcePath) {
537539
boolean isValidExtension = Stream.of(LEGAL_ICON_TYPE)
538540
.anyMatch(type -> StringUtils.equalsIgnoreCase(type, iconExtension));
539541
if (!isValidExtension) {
540542
return StringUtils.EMPTY;
541543
}
542544
String newFileName = UUIDUtil.uuid() + "." + iconExtension;
543-
File iconFile = Paths.get(NAS_SHARE_DIR, newFileName).toFile();
545+
File iconFile = Paths.get(resourcePath, newFileName).toFile();
544546
byte[] iconBytes = iconContent.getBytes(StandardCharsets.UTF_8);
545547
try (InputStream inputStream = new ByteArrayInputStream(Base64.getDecoder().decode(iconBytes))) {
546548
FileUtils.copyInputStreamToFile(inputStream, iconFile);

app-builder/jane/plugins/aipp-plugin/src/main/resources/application-prod.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ app-engine:
3838
exclude-names:
3939
- 'i18n_appBuilder_*demo*'
4040
resource:
41-
path-prefix: /static/
41+
url-prefix: /static/
42+
path: /var/share
4243
form:
4344
create:
4445
maximumNum: 400

app-builder/jane/plugins/aipp-plugin/src/test/java/modelengine/fit/jober/aipp/domains/appversion/AppVersionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void setUp() {
197197
this.aippModelCenter,
198198
converterFactory,
199199
this.aippFlowDefinitionService,
200-
this.flowDefinitionService, 20000, 300, this.knowledgeCenterService);
200+
this.flowDefinitionService, 20000, 300, this.knowledgeCenterService, "/var/share");
201201
}
202202

203203
/**

app-builder/jane/plugins/aipp-plugin/src/test/java/modelengine/fit/jober/aipp/service/AppBuilderAppServiceImplTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ public static AppVersion mockAppVersion(AppBuilderAppPo appPo) {
353353
null,
354354
20000,
355355
300,
356-
null);
356+
null,
357+
"/var/share");
357358
if (StringUtils.isBlank(appPo.getConfigId())) {
358359
appPo.setConfigId("defaultConfigId");
359360
}

app-builder/jane/plugins/aipp-plugin/src/test/java/modelengine/fit/jober/aipp/util/AppImExportUtilTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ void testSaveIconFile() throws IOException {
181181
String iconContent =
182182
new String(Base64.getEncoder().encode("This is an icon png.".getBytes(StandardCharsets.UTF_8)),
183183
StandardCharsets.UTF_8);
184-
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "png", "123", "/api/jober");
184+
String tempDir = System.getProperty("java.io.tmpdir");
185+
String iconUrl = AppImExportUtil.saveIconFile(iconContent, "png", "123", "/api/jober", tempDir);
185186
assertThat(iconUrl).startsWith("/api/jober/v1/api/123");
186187

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

205-
iconUrl = AppImExportUtil.saveIconFile(iconContent, "../../../.jpg", "123", "/api/jober");
207+
iconUrl = AppImExportUtil.saveIconFile(iconContent, ".jpg", "123", "/api/jober", tempDir);
206208
assertThat(iconUrl).isEqualTo(StringUtils.EMPTY);
207209
}
208210

0 commit comments

Comments
 (0)