Commit 0b650739a1da49089c6ae653b792da6d06327bf9
1 parent
1c75665a
✨ feat(file): update file naming strategy to support timestamp as directory or suffix
Showing
2 changed files
with
22 additions
and
22 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java
| @@ -42,19 +42,19 @@ public class FileServiceImpl implements FileService { | @@ -42,19 +42,19 @@ public class FileServiceImpl implements FileService { | ||
| 42 | */ | 42 | */ |
| 43 | static boolean PATH_PREFIX_DATE_ENABLE = true; | 43 | static boolean PATH_PREFIX_DATE_ENABLE = true; |
| 44 | /** | 44 | /** |
| 45 | - * 上传文件的后缀,是否包含时间戳 | 45 | + * 上传文件的后缀,是否启用 |
| 46 | * | 46 | * |
| 47 | - * 目的:保证文件的唯一性,避免覆盖 | 47 | + * 算法:当前时间戳(毫秒)+ 5 位随机数;目的是保证文件的唯一性,避免覆盖 |
| 48 | * 定制:可按需调整成 UUID、或者其他方式 | 48 | * 定制:可按需调整成 UUID、或者其他方式 |
| 49 | */ | 49 | */ |
| 50 | static boolean PATH_SUFFIX_TIMESTAMP_ENABLE = false; | 50 | static boolean PATH_SUFFIX_TIMESTAMP_ENABLE = false; |
| 51 | /** | 51 | /** |
| 52 | - * 时间戳是否作为上级目录 | 52 | + * 后缀是否作为上级目录 |
| 53 | * | 53 | * |
| 54 | - * true:{@code yyyyMMdd/timestamp/原文件名.ext};保留原文件名 | ||
| 55 | - * false:{@code yyyyMMdd/原文件名_timestamp.ext};时间戳拼到文件名 | 54 | + * true:{@code yyyyMMdd/<后缀>/原文件名.ext};保留原文件名 |
| 55 | + * false:{@code yyyyMMdd/原文件名_<后缀>.ext};后缀拼到文件名 | ||
| 56 | */ | 56 | */ |
| 57 | - static boolean PATH_TIMESTAMP_AS_DIRECTORY = true; | 57 | + static boolean PATH_SUFFIX_AS_DIRECTORY = true; |
| 58 | 58 | ||
| 59 | @Resource | 59 | @Resource |
| 60 | private FileConfigService fileConfigService; | 60 | private FileConfigService fileConfigService; |
| @@ -115,7 +115,7 @@ public class FileServiceImpl implements FileService { | @@ -115,7 +115,7 @@ public class FileServiceImpl implements FileService { | ||
| 115 | 115 | ||
| 116 | // 2.1 先拼接 suffix 后缀 | 116 | // 2.1 先拼接 suffix 后缀 |
| 117 | if (StrUtil.isNotEmpty(suffix)) { | 117 | if (StrUtil.isNotEmpty(suffix)) { |
| 118 | - if (PATH_TIMESTAMP_AS_DIRECTORY) { | 118 | + if (PATH_SUFFIX_AS_DIRECTORY) { |
| 119 | name = suffix + StrUtil.SLASH + name; | 119 | name = suffix + StrUtil.SLASH + name; |
| 120 | } else { | 120 | } else { |
| 121 | String ext = FileUtil.extName(name); | 121 | String ext = FileUtil.extName(name); |
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImplTest.java
| @@ -42,7 +42,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -42,7 +42,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 42 | public void setUp() { | 42 | public void setUp() { |
| 43 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; | 43 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; |
| 44 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; | 44 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; |
| 45 | - FileServiceImpl.PATH_TIMESTAMP_AS_DIRECTORY = true; | 45 | + FileServiceImpl.PATH_SUFFIX_AS_DIRECTORY = true; |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | @Test | 48 | @Test |
| @@ -201,7 +201,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -201,7 +201,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 201 | String path = fileService.generateUploadPath(name, directory); | 201 | String path = fileService.generateUploadPath(name, directory); |
| 202 | 202 | ||
| 203 | // 断言 | 203 | // 断言 |
| 204 | - // 格式为:avatar/yyyyMMdd/timestamp/test.jpg | 204 | + // 格式为:avatar/yyyyMMdd/{时间戳+随机数}/test.jpg |
| 205 | assertTrue(path.startsWith(directory + "/")); | 205 | assertTrue(path.startsWith(directory + "/")); |
| 206 | // 包含日期格式:8 位数字,如 20240517 | 206 | // 包含日期格式:8 位数字,如 20240517 |
| 207 | assertTrue(path.matches(directory + "/\\d{8}/\\d+/test\\.jpg")); | 207 | assertTrue(path.matches(directory + "/\\d{8}/\\d+/test\\.jpg")); |
| @@ -237,7 +237,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -237,7 +237,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 237 | String path = fileService.generateUploadPath(name, directory); | 237 | String path = fileService.generateUploadPath(name, directory); |
| 238 | 238 | ||
| 239 | // 断言 | 239 | // 断言 |
| 240 | - // 格式为:avatar/timestamp/test.jpg | 240 | + // 格式为:avatar/{时间戳+随机数}/test.jpg |
| 241 | assertTrue(path.startsWith(directory + "/")); | 241 | assertTrue(path.startsWith(directory + "/")); |
| 242 | assertTrue(path.matches(directory + "/\\d+/test\\.jpg")); | 242 | assertTrue(path.matches(directory + "/\\d+/test\\.jpg")); |
| 243 | } | 243 | } |
| @@ -270,7 +270,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -270,7 +270,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 270 | String path = fileService.generateUploadPath(name, directory); | 270 | String path = fileService.generateUploadPath(name, directory); |
| 271 | 271 | ||
| 272 | // 断言 | 272 | // 断言 |
| 273 | - // 格式为:avatar/yyyyMMdd/timestamp/test | 273 | + // 格式为:avatar/yyyyMMdd/{时间戳+随机数}/test |
| 274 | assertTrue(path.startsWith(directory + "/")); | 274 | assertTrue(path.startsWith(directory + "/")); |
| 275 | assertTrue(path.matches(directory + "/\\d{8}/\\d+/test")); | 275 | assertTrue(path.matches(directory + "/\\d{8}/\\d+/test")); |
| 276 | } | 276 | } |
| @@ -287,58 +287,58 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -287,58 +287,58 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 287 | String path = fileService.generateUploadPath(name, directory); | 287 | String path = fileService.generateUploadPath(name, directory); |
| 288 | 288 | ||
| 289 | // 断言 | 289 | // 断言 |
| 290 | - // 格式为:yyyyMMdd/timestamp/test.jpg | 290 | + // 格式为:yyyyMMdd/{时间戳+随机数}/test.jpg |
| 291 | assertTrue(path.matches("\\d{8}/\\d+/test\\.jpg")); | 291 | assertTrue(path.matches("\\d{8}/\\d+/test\\.jpg")); |
| 292 | } | 292 | } |
| 293 | 293 | ||
| 294 | @Test | 294 | @Test |
| 295 | - public void testGenerateUploadPath_TimestampAsName_AllEnabled() { | 295 | + public void testGenerateUploadPath_SuffixAsName_AllEnabled() { |
| 296 | // 准备参数 | 296 | // 准备参数 |
| 297 | String name = "test.jpg"; | 297 | String name = "test.jpg"; |
| 298 | String directory = "avatar"; | 298 | String directory = "avatar"; |
| 299 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; | 299 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; |
| 300 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; | 300 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; |
| 301 | - FileServiceImpl.PATH_TIMESTAMP_AS_DIRECTORY = false; | 301 | + FileServiceImpl.PATH_SUFFIX_AS_DIRECTORY = false; |
| 302 | 302 | ||
| 303 | // 调用 | 303 | // 调用 |
| 304 | String path = fileService.generateUploadPath(name, directory); | 304 | String path = fileService.generateUploadPath(name, directory); |
| 305 | 305 | ||
| 306 | // 断言 | 306 | // 断言 |
| 307 | - // 格式为:avatar/yyyyMMdd/test_timestamp.jpg | 307 | + // 格式为:avatar/yyyyMMdd/test_{时间戳+随机数}.jpg |
| 308 | assertTrue(path.matches(directory + "/\\d{8}/test_\\d+\\.jpg")); | 308 | assertTrue(path.matches(directory + "/\\d{8}/test_\\d+\\.jpg")); |
| 309 | } | 309 | } |
| 310 | 310 | ||
| 311 | @Test | 311 | @Test |
| 312 | - public void testGenerateUploadPath_TimestampAsName_PrefixDisabled() { | 312 | + public void testGenerateUploadPath_SuffixAsName_PrefixDisabled() { |
| 313 | // 准备参数 | 313 | // 准备参数 |
| 314 | String name = "test.jpg"; | 314 | String name = "test.jpg"; |
| 315 | String directory = "avatar"; | 315 | String directory = "avatar"; |
| 316 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = false; | 316 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = false; |
| 317 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; | 317 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; |
| 318 | - FileServiceImpl.PATH_TIMESTAMP_AS_DIRECTORY = false; | 318 | + FileServiceImpl.PATH_SUFFIX_AS_DIRECTORY = false; |
| 319 | 319 | ||
| 320 | // 调用 | 320 | // 调用 |
| 321 | String path = fileService.generateUploadPath(name, directory); | 321 | String path = fileService.generateUploadPath(name, directory); |
| 322 | 322 | ||
| 323 | // 断言 | 323 | // 断言 |
| 324 | - // 格式为:avatar/test_timestamp.jpg | 324 | + // 格式为:avatar/test_{时间戳+随机数}.jpg |
| 325 | assertTrue(path.matches(directory + "/test_\\d+\\.jpg")); | 325 | assertTrue(path.matches(directory + "/test_\\d+\\.jpg")); |
| 326 | } | 326 | } |
| 327 | 327 | ||
| 328 | @Test | 328 | @Test |
| 329 | - public void testGenerateUploadPath_TimestampAsName_NoExtension() { | 329 | + public void testGenerateUploadPath_SuffixAsName_NoExtension() { |
| 330 | // 准备参数 | 330 | // 准备参数 |
| 331 | String name = "test"; | 331 | String name = "test"; |
| 332 | String directory = "avatar"; | 332 | String directory = "avatar"; |
| 333 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; | 333 | FileServiceImpl.PATH_PREFIX_DATE_ENABLE = true; |
| 334 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; | 334 | FileServiceImpl.PATH_SUFFIX_TIMESTAMP_ENABLE = true; |
| 335 | - FileServiceImpl.PATH_TIMESTAMP_AS_DIRECTORY = false; | 335 | + FileServiceImpl.PATH_SUFFIX_AS_DIRECTORY = false; |
| 336 | 336 | ||
| 337 | // 调用 | 337 | // 调用 |
| 338 | String path = fileService.generateUploadPath(name, directory); | 338 | String path = fileService.generateUploadPath(name, directory); |
| 339 | 339 | ||
| 340 | // 断言 | 340 | // 断言 |
| 341 | - // 格式为:avatar/yyyyMMdd/test_timestamp | 341 | + // 格式为:avatar/yyyyMMdd/test_{时间戳+随机数} |
| 342 | assertTrue(path.matches(directory + "/\\d{8}/test_\\d+")); | 342 | assertTrue(path.matches(directory + "/\\d{8}/test_\\d+")); |
| 343 | } | 343 | } |
| 344 | 344 | ||
| @@ -354,7 +354,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | @@ -354,7 +354,7 @@ public class FileServiceImplTest extends BaseDbUnitTest { | ||
| 354 | String path = fileService.generateUploadPath(name, directory); | 354 | String path = fileService.generateUploadPath(name, directory); |
| 355 | 355 | ||
| 356 | // 断言 | 356 | // 断言 |
| 357 | - // 格式为:yyyyMMdd/timestamp/test.jpg | 357 | + // 格式为:yyyyMMdd/{时间戳+随机数}/test.jpg |
| 358 | assertTrue(path.matches("\\d{8}/\\d+/test\\.jpg")); | 358 | assertTrue(path.matches("\\d{8}/\\d+/test\\.jpg")); |
| 359 | } | 359 | } |
| 360 | 360 |