Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pref:对文件上传接口的文件存储路径进行调整 #10919 #10966

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,8 @@ const val REPO_NAME_IMAGE = "image"
const val REPO_NAME_REPORT = "report"
const val REPO_NAME_PLUGIN = "plugin"
const val REPO_NAME_STATIC = "static"



//时间格式化
const val DATE_FORMAT_YYYY_MM_DD = "yyyyMMdd"
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ package com.tencent.devops.artifactory.resources

import com.tencent.devops.artifactory.api.user.UserBkRepoStaticResource
import com.tencent.devops.artifactory.constant.BKREPO_STATIC_PROJECT_ID
import com.tencent.devops.artifactory.constant.DATE_FORMAT_YYYY_MM_DD
import com.tencent.devops.artifactory.pojo.enums.FileChannelTypeEnum
import com.tencent.devops.artifactory.pojo.enums.FileTypeEnum
import com.tencent.devops.artifactory.service.ArchiveFileService
Expand All @@ -38,6 +39,8 @@ import com.tencent.devops.common.web.RestResource
import org.glassfish.jersey.media.multipart.FormDataContentDisposition
import org.springframework.beans.factory.annotation.Autowired
import java.io.InputStream
import java.time.LocalDate
import java.time.format.DateTimeFormatter

@RestResource
class UserBkRepoStaticResourceImpl @Autowired constructor(
Expand All @@ -54,12 +57,15 @@ class UserBkRepoStaticResourceImpl @Autowired constructor(
val index = fileName.lastIndexOf(".")
val fileSuffix = fileName.substring(index + 1)
val filePathSb = StringBuilder("file/")
val today = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern(DATE_FORMAT_YYYY_MM_DD )
val nowTime = today.format(formatter)
val baseUrl="$nowTime/${UUIDUtil.generate()}.$fileSuffix"
val filePath = if (type.isNullOrBlank()) {
filePathSb.append(fileSuffix)
filePathSb.append(baseUrl)
} else {
filePathSb.append("${type.lowercase()}/$fileSuffix")
filePathSb.append("${type.lowercase()}/").append(baseUrl)
}
filePathSb.append("/${UUIDUtil.generate()}.$fileSuffix")
val url = archiveFileService.uploadFile(
userId = userId,
inputStream = inputStream,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class BkRepoArchiveFileServiceImpl @Autowired constructor(
staticFlag: Boolean?
): String {
val pathSplit = file.name.split('.')
val destPath = filePath ?: DefaultPathUtils.randomFileName(pathSplit[pathSplit.size - 1])
val destPath = DefaultPathUtils.getUploadPathByTime(filePath, fileType?.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 @@ -365,7 +365,7 @@ class DiskArchiveFileServiceImpl : ArchiveFileServiceImpl() {
val fileTypeStr = fileType?.fileType ?: "file"
val fileTypeName = file.name.substring(file.name.indexOf(".") + 1)
val destPath = if (null == filePath) {
"${getBasePath()}$fileSeparator$fileTypeStr$fileSeparator$${DefaultPathUtils.randomFileName(fileTypeName)}"
"${getBasePath()}$fileSeparator$${DefaultPathUtils.getUploadPathByTime(filePath,fileType?.fileType,fileTypeName)}"
} else {
// #5176 修正未对上传类型来决定存放路径的问题,统一在此生成归档路径,而不是由外部指定会存在内部路径泄露风险
if (fileType != null && !projectId.isNullOrBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,20 @@

package com.tencent.devops.artifactory.util

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
import java.nio.file.Files
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.regex.Pattern

@Suppress("ALL")
object DefaultPathUtils {
private const val DEFAULT_EXTENSION = "temp"

private val LOG = LoggerFactory.getLogger(this::class.java.name)
fun isFolder(path: String): Boolean {
return path.endsWith("/")
}
Expand Down Expand Up @@ -68,6 +73,28 @@ object DefaultPathUtils {
return "${UUIDUtil.generate()}$suffix"
}


// 根据上传时间来生成文件路径(全路径)
fun getUploadPathByTime(filePath: String?, 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 = if (filePath.isNullOrBlank()) {
filePathSb.append("file/")
if (fileType == null){
filePathSb.append(baseUrl).toString()
}else{
filePathSb.append("${fileType.lowercase()}/").append(baseUrl).toString()
}
} else {
filePath
}
return path
}


fun resolvePipelineId(path: String): String {
val roads = path.removePrefix("/").split("/")
if (roads.size < 2) {
Expand Down
Loading