Skip to content

Commit

Permalink
pref:插件国际化properties文件value值支持引入文件 TencentBlueKing#9366
Browse files Browse the repository at this point in the history
  • Loading branch information
yjieliang committed Sep 19, 2023
1 parent 7fd04fc commit a3212ce
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AtomReleaseTxtAnalysisUtilTest {
val result = mutableMapOf<String, String>()
AtomReleaseTxtAnalysisUtil.regexAnalysis(
input = input,
atomPath = "",
fileDirPath = "",
pathList = pathList
)
pathList.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}

Expand All @@ -63,7 +57,6 @@ abstract class StoreFileService {
userId: String,
description: String,
client: Client,
language: String,
fileDirPath: String
): String {
val pathList = mutableListOf<String>()
Expand All @@ -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>
): 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<String, String>,
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(
Expand All @@ -160,24 +106,6 @@ abstract class StoreFileService {
result: MutableMap<String, String>
): Map<String, String>

fun logoUrlAnalysis(logoUrl: String): Result<String> {
// 正则解析
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,
Expand All @@ -188,8 +116,4 @@ abstract class StoreFileService {
file: File,
os: String
): Result<Boolean?>

fun buildAtomArchivePath(userId: String, atomDir: String) =
"${getAtomBasePath()}$fileSeparator$BK_CI_ATOM_DIR$fileSeparator$userId$fileSeparator$atomDir" +
"$fileSeparator${UUIDUtil.generate()}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit a3212ce

Please sign in to comment.