diff --git a/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreFileServiceImpl.kt b/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreFileServiceImpl.kt index d9ba9e01a78..cfaf34bf19a 100644 --- a/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreFileServiceImpl.kt +++ b/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreFileServiceImpl.kt @@ -35,7 +35,6 @@ import com.tencent.devops.common.client.Client import com.tencent.devops.common.service.utils.CommonUtils import com.tencent.devops.common.web.utils.I18nUtil import com.tencent.devops.store.service.common.StoreFileService -import com.tencent.devops.store.utils.StoreUtils import java.io.File import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @@ -66,8 +65,7 @@ class SampleStoreFileServiceImpl : StoreFileService() { logo = true, language = I18nUtil.getLanguage(I18nUtil.getRequestUserId()) ).data - logger.info("uploadFileToPath return fileUrl:$fileUrl") - fileUrl?.let { result[path] = StoreUtils.removeUrlHost(fileUrl) } + fileUrl?.let { result[path] = fileUrl } } else { logger.warn("Resource file does not exist:${file.path}") } diff --git a/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreI18nMessageServiceImpl.kt b/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreI18nMessageServiceImpl.kt index 9d13fa1f72d..dbe4c7e5329 100644 --- a/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreI18nMessageServiceImpl.kt +++ b/src/backend/ci/core/store/biz-store-sample/src/main/kotlin/com/tencent/devops/store/service/common/impl/SampleStoreI18nMessageServiceImpl.kt @@ -33,6 +33,7 @@ import com.tencent.devops.artifactory.constant.BKREPO_DEFAULT_USER import com.tencent.devops.artifactory.constant.BKREPO_STORE_PROJECT_ID import com.tencent.devops.artifactory.constant.REPO_NAME_PLUGIN import com.tencent.devops.common.api.util.OkhttpUtils +import com.tencent.devops.store.utils.AtomReleaseTxtAnalysisUtil import java.io.File import java.net.URLEncoder import org.springframework.stereotype.Service @@ -100,25 +101,24 @@ class SampleStoreI18nMessageServiceImpl : StoreI18nMessageServiceImpl() { val separator = File.separator val fileNameList = getFileNames( projectCode = projectCode, - fileDir = "$fileDir${separator}file$separator$language" + fileDir = "$fileDir${separator}file" ) ?: return description - val fileDirPath = storeFileService.buildAtomArchivePath( + val fileDirPath = AtomReleaseTxtAnalysisUtil.buildAtomArchivePath( userId = userId, atomDir = fileDir - ) + "file$separator$language" + ) fileNameList.forEach { downloadFile( - "$projectCode$separator$fileDir${separator}file$separator$language$separator$it", - File(fileDirPath, it) + "$projectCode$separator$fileDir${separator}file$separator$it", + File("$fileDirPath${separator}file", it) ) } return storeFileService.descriptionAnalysis( - fileDirPath = fileDirPath, + fileDirPath = "$fileDirPath${separator}file", userId = userId, description = description, - client = client, - language = language + client = client ) } } diff --git a/src/backend/ci/core/store/biz-store-sample/src/test/kotlin/com/tencent/devops/store/util/AtomReleaseTxtAnalysisUtilTest.kt b/src/backend/ci/core/store/biz-store-sample/src/test/kotlin/com/tencent/devops/store/util/AtomReleaseTxtAnalysisUtilTest.kt index 187f89e493a..88c10b4d429 100644 --- a/src/backend/ci/core/store/biz-store-sample/src/test/kotlin/com/tencent/devops/store/util/AtomReleaseTxtAnalysisUtilTest.kt +++ b/src/backend/ci/core/store/biz-store-sample/src/test/kotlin/com/tencent/devops/store/util/AtomReleaseTxtAnalysisUtilTest.kt @@ -40,7 +40,7 @@ class AtomReleaseTxtAnalysisUtilTest { val result = mutableMapOf() AtomReleaseTxtAnalysisUtil.regexAnalysis( input = input, - atomPath = "", + fileDirPath = "", pathList = pathList ) pathList.forEach { diff --git a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/atom/impl/OpAtomServiceImpl.kt b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/atom/impl/OpAtomServiceImpl.kt index 3d31a377be1..67f91b941a9 100644 --- a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/atom/impl/OpAtomServiceImpl.kt +++ b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/atom/impl/OpAtomServiceImpl.kt @@ -79,6 +79,7 @@ import com.tencent.devops.store.service.atom.AtomReleaseService import com.tencent.devops.store.service.atom.OpAtomService import com.tencent.devops.store.service.atom.action.AtomDecorateFactory import com.tencent.devops.store.service.common.ClassifyService +import com.tencent.devops.store.service.common.StoreFileService import com.tencent.devops.store.service.common.StoreI18nMessageService import com.tencent.devops.store.service.common.StoreLogoService import com.tencent.devops.store.service.websocket.StoreWebsocketService @@ -113,6 +114,7 @@ class OpAtomServiceImpl @Autowired constructor( private val storeWebsocketService: StoreWebsocketService, private val classifyService: ClassifyService, private val storeI18nMessageService: StoreI18nMessageService, + private val storeFileService: StoreFileService, private val redisOperation: RedisOperation, private val client: Client ) : OpAtomService { @@ -481,9 +483,9 @@ class OpAtomServiceImpl @Autowired constructor( } } // 解析description - releaseInfo.description = AtomReleaseTxtAnalysisUtil.descriptionAnalysis( + releaseInfo.description = storeFileService.descriptionAnalysis( description = releaseInfo.description, - atomPath = atomPath, + fileDirPath = "$atomPath${fileSeparator}file", client = client, userId = userId ) @@ -496,7 +498,7 @@ class OpAtomServiceImpl @Autowired constructor( } try { if (file.exists()) { - val archiveAtomResult = AtomReleaseTxtAnalysisUtil.serviceArchiveAtomFile( + val archiveAtomResult = storeFileService.serviceArchiveAtomFile( userId = userId, projectCode = releaseInfo.projectId, atomCode = atomCode, diff --git a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/StoreFileService.kt b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/StoreFileService.kt index afd612bd1a2..1590a019530 100644 --- a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/StoreFileService.kt +++ b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/StoreFileService.kt @@ -27,18 +27,13 @@ package com.tencent.devops.store.service.common import com.tencent.devops.common.api.pojo.Result -import com.tencent.devops.common.api.util.UUIDUtil import com.tencent.devops.common.client.Client -import com.tencent.devops.common.web.utils.I18nUtil -import com.tencent.devops.store.constant.StoreMessageCode import com.tencent.devops.store.utils.AtomReleaseTxtAnalysisUtil import java.io.File import java.io.FileOutputStream import java.io.IOException import java.io.InputStream import java.net.URL -import java.util.regex.Matcher -import java.util.regex.Pattern import org.slf4j.LoggerFactory import org.springframework.stereotype.Service @@ -51,10 +46,9 @@ import org.springframework.stereotype.Service abstract class StoreFileService { companion object { - const val BK_CI_ATOM_DIR = "bk-atom" const val BK_CI_PATH_REGEX = "(\\\$\\{\\{indexFile\\()(\"[^\"]*\")" val fileSeparator: String = System.getProperty("file.separator") - private val logger = LoggerFactory.getLogger(AtomReleaseTxtAnalysisUtil::class.java) + private val logger = LoggerFactory.getLogger(StoreFileService::class.java) private const val FILE_DEFAULT_SIZE = 1024 } @@ -63,7 +57,6 @@ abstract class StoreFileService { userId: String, description: String, client: Client, - language: String, fileDirPath: String ): String { val pathList = mutableListOf() @@ -90,66 +83,19 @@ abstract class StoreFileService { file.delete() } } - descriptionText = regexAnalysis( + descriptionText = AtomReleaseTxtAnalysisUtil.regexAnalysis( input = descriptionText, - fileDirPath = "$fileDirPath$fileSeparator$language", + fileDirPath = fileDirPath, pathList = pathList ) val uploadFileToPathResult = uploadFileToPath( userId = userId, pathList = pathList, client = client, - fileDirPath = "$fileDirPath$fileSeparator$language", + fileDirPath = fileDirPath, result = result ) - return filePathReplace(uploadFileToPathResult.toMutableMap(), descriptionText) - } - - private fun getAtomBasePath(): String { - return System.getProperty("java.io.tmpdir").removeSuffix(fileSeparator) - } - - fun regexAnalysis( - input: String, - fileDirPath: String, - pathList: MutableList - ): String { - var descriptionContent = input - val pattern: Pattern = Pattern.compile(BK_CI_PATH_REGEX) - val matcher: Matcher = pattern.matcher(descriptionContent) - while (matcher.find()) { - val path = matcher.group(2).replace("\"", "").removePrefix(fileSeparator) - logger.info("regexAnalysis file path:$path") - if (path.endsWith(".md")) { - val file = File("$fileDirPath$fileSeparator$path") - if (file.exists()) { - descriptionContent = regexAnalysis( - input = file.readText(), - fileDirPath = fileDirPath, - pathList = pathList - ) - } - } else { - pathList.add(path) - } - } - return descriptionContent - } - - fun filePathReplace( - result: MutableMap, - descriptionContent: String - ): String { - var content = descriptionContent - // 替换资源路径 - result.forEach { - val analysisPattern: Pattern = Pattern.compile("(\\\$\\{\\{indexFile\\(\"${it.key}\"\\)}})") - val analysisMatcher: Matcher = analysisPattern.matcher(content) - content = analysisMatcher.replaceFirst( - "![${it.key}](${it.value.replace(fileSeparator, "\\$fileSeparator")})" - ) - } - return content + return AtomReleaseTxtAnalysisUtil.filePathReplace(uploadFileToPathResult.toMutableMap(), descriptionText) } abstract fun uploadFileToPath( @@ -160,24 +106,6 @@ abstract class StoreFileService { result: MutableMap ): Map - fun logoUrlAnalysis(logoUrl: String): Result { - // 正则解析 - val pattern: Pattern = Pattern.compile(BK_CI_PATH_REGEX) - val matcher: Matcher = pattern.matcher(logoUrl) - val relativePath = if (matcher.find()) { - matcher.group(2).replace("\"", "") - } else null - return if (relativePath.isNullOrBlank()) { - I18nUtil.generateResponseDataObject( - StoreMessageCode.USER_REPOSITORY_TASK_JSON_FIELD_IS_INVALID, - arrayOf("releaseInfo.logoUrl"), - language = I18nUtil.getLanguage(I18nUtil.getRequestUserId()) - ) - } else { - Result(relativePath) - } - } - abstract fun serviceArchiveAtomFile( userId: String, projectCode: String, @@ -188,8 +116,4 @@ abstract class StoreFileService { file: File, os: String ): Result - - fun buildAtomArchivePath(userId: String, atomDir: String) = - "${getAtomBasePath()}$fileSeparator$BK_CI_ATOM_DIR$fileSeparator$userId$fileSeparator$atomDir" + - "$fileSeparator${UUIDUtil.generate()}" } diff --git a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/impl/StoreI18nMessageServiceImpl.kt b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/impl/StoreI18nMessageServiceImpl.kt index fd361494192..9ac15a93df4 100644 --- a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/impl/StoreI18nMessageServiceImpl.kt +++ b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/service/common/impl/StoreI18nMessageServiceImpl.kt @@ -357,7 +357,6 @@ abstract class StoreI18nMessageServiceImpl : StoreI18nMessageService { repositoryHashId = repositoryHashId, branch = branch ) ?: return content - logger.info("getDescriptionI18nContent fileStr:$fileStr") return descriptionAnalysis( userId = userId, projectCode = projectCode, diff --git a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/utils/AtomReleaseTxtAnalysisUtil.kt b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/utils/AtomReleaseTxtAnalysisUtil.kt index 2da3463f509..7acf4cd732d 100644 --- a/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/utils/AtomReleaseTxtAnalysisUtil.kt +++ b/src/backend/ci/core/store/biz-store/src/main/kotlin/com/tencent/devops/store/utils/AtomReleaseTxtAnalysisUtil.kt @@ -27,79 +27,19 @@ package com.tencent.devops.store.utils -import com.tencent.devops.artifactory.api.ServiceArchiveAtomFileResource -import com.tencent.devops.artifactory.api.service.ServiceFileResource -import com.tencent.devops.artifactory.pojo.enums.FileChannelTypeEnum -import com.tencent.devops.common.api.constant.CommonMessageCode import com.tencent.devops.common.api.pojo.Result -import com.tencent.devops.common.api.util.OkhttpUtils import com.tencent.devops.common.api.util.UUIDUtil -import com.tencent.devops.common.client.Client -import com.tencent.devops.common.service.utils.CommonUtils import com.tencent.devops.common.web.utils.I18nUtil import com.tencent.devops.store.constant.StoreMessageCode import java.io.File -import java.io.FileOutputStream -import java.io.IOException -import java.io.InputStream -import java.net.URL import java.util.regex.Matcher import java.util.regex.Pattern -import org.slf4j.LoggerFactory object AtomReleaseTxtAnalysisUtil { private const val BK_CI_ATOM_DIR = "bk-atom" private const val BK_CI_PATH_REGEX = "(\\\$\\{\\{indexFile\\()(\"[^\"]*\")" private val fileSeparator: String = System.getProperty("file.separator") - private val logger = LoggerFactory.getLogger(AtomReleaseTxtAnalysisUtil::class.java) - private const val FILE_DEFAULT_SIZE = 1024 - - @Suppress("NestedBlockDepth") - fun descriptionAnalysis( - userId: String, - description: String, - atomPath: String, - client: Client - ): String { - val pathList = mutableListOf() - val result = mutableMapOf() - var descriptionText = description - if (description.startsWith("http") && description.endsWith(".md")) { - // 读取远程文件 - var inputStream: InputStream? = null - val file = File("$atomPath${fileSeparator}file${fileSeparator}description.md") - try { - inputStream = URL(description).openStream() - FileOutputStream(file).use { outputStream -> - var read: Int - val bytes = ByteArray(FILE_DEFAULT_SIZE) - while (inputStream.read(bytes).also { read = it } != -1) { - outputStream.write(bytes, 0, read) - } - } - descriptionText = file.readText() - } catch (e: IOException) { - logger.warn("get remote file fail:${e.message}") - } finally { - inputStream?.close() - file.delete() - } - } - descriptionText = regexAnalysis( - input = descriptionText, - atomPath = atomPath, - pathList = pathList - ) - val uploadFileToPathResult = uploadFileToPath( - userId = userId, - pathList = pathList, - client = client, - atomPath = atomPath, - result = result - ) - return filePathReplace(uploadFileToPathResult.toMutableMap(), descriptionText) - } private fun getAtomBasePath(): String { return System.getProperty("java.io.tmpdir").removeSuffix(fileSeparator) @@ -107,7 +47,7 @@ object AtomReleaseTxtAnalysisUtil { fun regexAnalysis( input: String, - atomPath: String, + fileDirPath: String, pathList: MutableList ): String { var descriptionContent = input @@ -116,11 +56,11 @@ object AtomReleaseTxtAnalysisUtil { while (matcher.find()) { val path = matcher.group(2).replace("\"", "").removePrefix(fileSeparator) if (path.endsWith(".md")) { - val file = File("$atomPath${fileSeparator}file$fileSeparator$path") + val file = File("$fileDirPath$fileSeparator$path") if (file.exists()) { descriptionContent = regexAnalysis( input = file.readText(), - atomPath = atomPath, + fileDirPath = fileDirPath, pathList = pathList ) } @@ -147,35 +87,6 @@ object AtomReleaseTxtAnalysisUtil { return content } - private fun uploadFileToPath( - userId: String, - pathList: List, - client: Client, - atomPath: String, - result: MutableMap - ): Map { - client.getServiceUrl(ServiceArchiveAtomFileResource::class) - pathList.forEach { path -> - val file = File("$atomPath${fileSeparator}file$fileSeparator$path") - if (file.exists()) { - val serviceUrlPrefix = client.getServiceUrl(ServiceFileResource::class) - val fileUrl = CommonUtils.serviceUploadFile( - userId = userId, - serviceUrlPrefix = serviceUrlPrefix, - file = file, - fileChannelType = FileChannelTypeEnum.WEB_SHOW.name, - logo = true, - language = I18nUtil.getLanguage(I18nUtil.getRequestUserId()) - ).data - fileUrl?.let { result[path] = StoreUtils.removeUrlHost(fileUrl) } - } else { - logger.warn("Resource file does not exist:${file.path}") - } - file.delete() - } - return result - } - fun logoUrlAnalysis(logoUrl: String): Result { // 正则解析 val pattern: Pattern = Pattern.compile(BK_CI_PATH_REGEX) @@ -194,32 +105,7 @@ object AtomReleaseTxtAnalysisUtil { } } - fun serviceArchiveAtomFile( - userId: String, - projectCode: String, - atomCode: String, - serviceUrlPrefix: String, - releaseType: String, - version: String, - file: File, - os: String - ): Result { - val serviceUrl = "$serviceUrlPrefix/service/artifactories/archiveAtom" + - "?userId=$userId&projectCode=$projectCode&atomCode=$atomCode" + - "&version=$version&releaseType=$releaseType&os=$os" - OkhttpUtils.uploadFile(serviceUrl, file).use { response -> - response.body!!.string() - if (!response.isSuccessful) { - return I18nUtil.generateResponseDataObject( - messageCode = CommonMessageCode.SYSTEM_ERROR, - language = I18nUtil.getLanguage(userId) - ) - } - return Result(true) - } - } - - fun buildAtomArchivePath(userId: String, atomCode: String) = - "${getAtomBasePath()}$fileSeparator$BK_CI_ATOM_DIR$fileSeparator$userId$fileSeparator$atomCode" + - "$fileSeparator${UUIDUtil.generate()}" + fun buildAtomArchivePath(userId: String, atomDir: String) = + "${getAtomBasePath()}$fileSeparator$BK_CI_ATOM_DIR$fileSeparator" + + "$userId$fileSeparator$atomDir$fileSeparator${UUIDUtil.generate()}" }