Skip to content

Commit

Permalink
关于路径重复bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kzhou991 committed Sep 13, 2024
1 parent b846066 commit aa752fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.tencent.devops.artifactory.pojo.enums.FileChannelTypeEnum
import com.tencent.devops.artifactory.pojo.enums.FileTypeEnum
import com.tencent.devops.artifactory.service.ArchiveFileService
import com.tencent.devops.common.api.pojo.Result
import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.api.util.UUIDUtil
import com.tencent.devops.common.web.RestResource
import org.glassfish.jersey.media.multipart.FormDataContentDisposition
Expand Down Expand Up @@ -67,7 +68,7 @@ class UserBkRepoStaticResourceImpl @Autowired constructor(
val baseUrl="$nowTime/${UUIDUtil.generate()}.$fileSuffix"
val filePath = if (type.isNullOrBlank()) {
// filePathSb.append(fileSuffix)
filePathSb.append(baseUrl)
filePathSb.append(baseUrl)
} else {
// filePathSb.append("${type.lowercase()}/$fileSuffix")
filePathSb.append("${type.lowercase()}/").append(baseUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ class BkRepoArchiveFileServiceImpl @Autowired constructor(
staticFlag: Boolean?
): String {
val pathSplit = file.name.split('.')
// val destPath = filePath ?: DefaultPathUtils.randomFileName(pathSplit[pathSplit.size - 1])
val destPath = "file/"+DefaultPathUtils.getUploadPathByTime(filePath, fileType,DefaultPathUtils.randomFileName(pathSplit[pathSplit.size - 1]));
//val destPath = filePath ?: DefaultPathUtils.randomFileName(pathSplit[pathSplit.size - 1])
val destPath = DefaultPathUtils.getUploadPathByTime(filePath, fileType,pathSplit[pathSplit.size - 1]);
val metadata = mutableMapOf<String, String>()
metadata[KEY_SHA_CONTENT] = file.inputStream().use { ShaUtils.sha1InputStream(it) }
props?.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ class DiskArchiveFileServiceImpl : ArchiveFileServiceImpl() {
val fileTypeName = file.name.substring(file.name.indexOf(".") + 1)
val destPath = if (null == filePath) {
// "${getBasePath()}$fileSeparator$fileTypeStr$fileSeparator$${DefaultPathUtils.randomFileName(fileTypeName)}"
"${getBasePath()}$fileSeparator$fileTypeStr$fileSeparator$${DefaultPathUtils.getUploadPathByTime(filePath,fileType,fileTypeName)}"
"${getBasePath()}$fileSeparator$${DefaultPathUtils.getUploadPathByTime(fileTypeStr,fileTypeName)}"
} else {
// #5176 修正未对上传类型来决定存放路径的问题,统一在此生成归档路径,而不是由外部指定会存在内部路径泄露风险
if (fileType != null && !projectId.isNullOrBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.artifactory.util

import com.tencent.devops.artifactory.constant.DATE_FORMAT_YYYY_MM_DD
import com.tencent.devops.artifactory.pojo.enums.FileTypeEnum
import com.tencent.devops.common.api.util.DateTimeUtil
import com.tencent.devops.common.api.util.UUIDUtil
import org.slf4j.LoggerFactory
import java.io.File
Expand Down Expand Up @@ -78,10 +79,11 @@ object DefaultPathUtils {
fun getUploadPathByTime(filePath: String?,fileType: FileTypeEnum?,type: String): String {
val filePathSb = StringBuilder()
val today = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD )
val formatter = DateTimeFormatter.ofPattern(DateTimeUtil.YYYYMMDD)
val nowTime = today.format(formatter)
val baseUrl="$nowTime/${UUIDUtil.generate()}.$type"
val path = if (filePath.isNullOrBlank()) {
filePathSb.append("file/")
if (fileType == null){
filePathSb.append(baseUrl).toString();
}else{
Expand All @@ -95,7 +97,20 @@ object DefaultPathUtils {

}

/**
* 根据上传时间来生成文件路径(全路径)
*/
fun getUploadPathByTime(fileType: String, type: String): String {
val filePathSb = StringBuilder()
val today = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern(DateTimeUtil.YYYYMMDD )
val nowTime = today.format(formatter)
val baseUrl="$nowTime/${UUIDUtil.generate()}.$type"
val path = filePathSb.append(fileType).append("/").append(baseUrl).toString();
LOG.info("upload path:$path")
return path;

}



Expand Down

0 comments on commit aa752fe

Please sign in to comment.