Skip to content

Commit

Permalink
pref:对文件上传接口的文件存储路径进行调整(优化) #10919
Browse files Browse the repository at this point in the history
  • Loading branch information
Kzhou991 committed Sep 14, 2024
1 parent 7704e7a commit 93d9532
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ class UserBkRepoStaticResourceImpl @Autowired constructor(
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,8 +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,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,8 +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(fileTypeStr,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 @@ -74,8 +74,8 @@ object DefaultPathUtils {
}


//根据上传时间来生成文件路径(全路径)
fun getUploadPathByTime(filePath: String?, fileType: FileTypeEnum?, type: String): String {
// 根据上传时间来生成文件路径(全路径)
fun getUploadPathByTime(filePath: String?, fileType: String?, type: String): String {
val filePathSb = StringBuilder()
val today = LocalDate.now()
val formatter = DateTimeFormatter.ofPattern(DateTimeUtil.YYYYMMDD )
Expand All @@ -84,37 +84,17 @@ object DefaultPathUtils {
val path = if (filePath.isNullOrBlank()) {
filePathSb.append("file/")
if (fileType == null){
filePathSb.append(baseUrl).toString();
filePathSb.append(baseUrl).toString()
}else{
filePathSb.append("${fileType.fileType.lowercase()}/").append(baseUrl).toString();
filePathSb.append("${fileType.lowercase()}/").append(baseUrl).toString()
}
} else {
filePath;
filePath
}
LOG.info("upload path:$path")
return path;

}


/**
* 根据上传时间来生成文件路径(全路径)
*/
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;

return path
}




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

0 comments on commit 93d9532

Please sign in to comment.