fix: playbook 添加文件校验

This commit is contained in:
zhangshuai
2024-10-29 09:37:17 +08:00
parent 300389ebec
commit 2e4f823211
3 changed files with 27 additions and 3 deletions

View File

@@ -70,6 +70,7 @@ public enum RCode {
// Playbook
PLAYBOOK_ID_CANNOT_EMPTY(302001, "playbook id cannot be empty"),
PLAYBOOK_NAME_DUPLICATE(302002, "playbook name duplicate "),
PLAYBOOK_INVALID_FILE(302003, "playbook Invalid file"),
// Workspace
WORKSPACE_ID_CANNOT_EMPTY(401001, "workspace id cannot be empty"),

View File

@@ -47,7 +47,10 @@ public class PlaybookServiceImpl extends ServiceImpl<PlaybookDao, PlaybookEntity
throw new ASWException(RCode.PLAYBOOK_NAME_DUPLICATE);
}
File destination = T.FileUtil.file(Constants.PLAYBOOK_FILES_DIR, name);
File unzip = null;
PlaybookEntity playbook = new PlaybookEntity();
try {
playbook.setWorkspaceId(workspaceId);
playbook.setName(name);
@@ -56,15 +59,33 @@ public class PlaybookServiceImpl extends ServiceImpl<PlaybookDao, PlaybookEntity
playbook.setUpdateUserId(StpUtil.getLoginIdAsString());
playbook.setCreateTimestamp(System.currentTimeMillis());
playbook.setUpdateTimestamp(System.currentTimeMillis());
// path
File destination = T.FileUtil.file(Constants.PLAYBOOK_FILES_DIR, name);
FileUtils.copyInputStreamToFile(file.getInputStream(), destination);
playbook.setPath(destination.getPath());
unzip = T.ZipUtil.unzip(destination);
List<File> fileList = Arrays.stream(unzip.listFiles()).toList();
if (T.CollUtil.isEmpty(fileList) || fileList.size() != 1) {
throw new ASWException(RCode.PLAYBOOK_INVALID_FILE);
}
File playbookDir = fileList.getFirst();
String dirName = playbookDir.getName();
if (!dirName.endsWith(".air")) {
throw new ASWException(RCode.PLAYBOOK_INVALID_FILE);
}
String pyName = dirName.replace("air", "py");
List<String> fileNameList = Arrays.stream(playbookDir.list()).toList();
if (T.CollUtil.isEmpty(fileNameList) || !fileNameList.contains(pyName)) {
throw new ASWException(RCode.PLAYBOOK_INVALID_FILE);
}
this.save(playbook);
} catch (Exception e) {
log.error(e, "[savePlaybook] [error] [file: {}]", file.getName());
T.FileUtil.del(description);
T.FileUtil.del(destination);
throw new ASWException(RCode.PLAYBOOK_INVALID_FILE);
}finally {
T.FileUtil.del(unzip);
}
return playbook;
}

View File

@@ -145,5 +145,7 @@ INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_
INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (237, '203001', 'GIT_COMMIT_CONFLICT_ERROR', '提交失败;解决冲突,然后提交结果', 'zh', '', 'admin', 1724030366000);
INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (238, '203002', 'GIT_MERGE_FAILED', 'Merge failed', 'en', '', 'admin', 1724030366000);
INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (239, '203002', 'GIT_MERGE_FAILED', '合并失败', 'zh', '', 'admin', 1724030366000);
INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (240, '302003', 'PLAYBOOK_INVALID_FILE', 'playbook Invalid file', 'en', '', 'admin', 1724030366000);
INSERT INTO `sys_i18n`(`id`, `name`, `code`, `value`, `lang`, `remark`, `update_user_id`, `update_timestamp`) VALUES (241, '302003', 'PLAYBOOK_INVALID_FILE', '无效文件', 'zh', '', 'admin', 1724030366000);
SET FOREIGN_KEY_CHECKS = 1;