|
48 | 48 | import top.continew.starter.extension.crud.service.BaseServiceImpl; |
49 | 49 |
|
50 | 50 | import java.io.File; |
51 | | -import java.io.IOException; |
52 | 51 | import java.util.List; |
53 | 52 | import java.util.Map; |
54 | 53 | import java.util.stream.Collectors; |
@@ -91,65 +90,13 @@ public void beforeDelete(List<Long> ids) { |
91 | 90 | } |
92 | 91 |
|
93 | 92 | @Override |
94 | | - public FileInfo upload(MultipartFile file, String parentPath, String storageCode) throws IOException { |
95 | | - // 校验文件格式 |
96 | | - String extName = FileNameUtil.extName(file.getOriginalFilename()); |
97 | | - return getFileInfo(file, parentPath, storageCode, extName); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * 上传文件并返回上传后的文件信息 |
102 | | - * |
103 | | - * @param file |
104 | | - * @param parentPath |
105 | | - * @param storageCode |
106 | | - * @param extName |
107 | | - * @return |
108 | | - */ |
109 | | - private FileInfo getFileInfo(Object file, String parentPath, String storageCode, String extName) { |
110 | | - List<String> allExtensions = FileTypeEnum.getAllExtensions(); |
111 | | - CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String |
112 | | - .join(StringConstants.COMMA, allExtensions)); |
113 | | - // 构建上传预处理对象 |
114 | | - StorageDO storage = storageService.getByCode(storageCode); |
115 | | - CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode()); |
116 | | - UploadPretreatment uploadPretreatment = fileStorageService.of(file) |
117 | | - .setPlatform(storage.getCode()) |
118 | | - .setHashCalculatorSha256(true) |
119 | | - .putAttr(ClassUtil.getClassName(StorageDO.class, false), storage) |
120 | | - .setPath(this.pretreatmentPath(parentPath)); |
121 | | - // 图片文件生成缩略图 |
122 | | - if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) { |
123 | | - uploadPretreatment.setIgnoreThumbnailException(true, true); |
124 | | - uploadPretreatment.thumbnail(img -> img.size(100, 100)); |
125 | | - } |
126 | | - uploadPretreatment.setProgressMonitor(new ProgressListener() { |
127 | | - @Override |
128 | | - public void start() { |
129 | | - log.info("开始上传"); |
130 | | - } |
131 | | - |
132 | | - @Override |
133 | | - public void progress(long progressSize, Long allSize) { |
134 | | - log.info("已上传 [{}],总大小 [{}]", progressSize, allSize); |
135 | | - } |
136 | | - |
137 | | - @Override |
138 | | - public void finish() { |
139 | | - log.info("上传结束"); |
140 | | - } |
141 | | - }); |
142 | | - // 创建父级目录 |
143 | | - this.createParentDir(parentPath, storage); |
144 | | - // 上传 |
145 | | - return uploadPretreatment.upload(); |
| 93 | + public FileInfo upload(MultipartFile file, String parentPath, String storageCode) { |
| 94 | + return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getOriginalFilename())); |
146 | 95 | } |
147 | 96 |
|
148 | 97 | @Override |
149 | | - public FileInfo upload(File file, String parentPath, String storageCode) throws IOException { |
150 | | - // 校验文件格式 |
151 | | - String extName = FileNameUtil.extName(file.getName()); |
152 | | - return getFileInfo(file, parentPath, storageCode, extName); |
| 98 | + public FileInfo upload(File file, String parentPath, String storageCode) { |
| 99 | + return this.upload(file, parentPath, storageCode, FileNameUtil.extName(file.getName())); |
153 | 100 | } |
154 | 101 |
|
155 | 102 | @Override |
@@ -251,6 +198,54 @@ protected void fill(Object obj) { |
251 | 198 | } |
252 | 199 | } |
253 | 200 |
|
| 201 | + /** |
| 202 | + * 上传文件并返回上传后的文件信息 |
| 203 | + * |
| 204 | + * @param file 文件 |
| 205 | + * @param parentPath 上级目录 |
| 206 | + * @param storageCode 存储引擎编码 |
| 207 | + * @param extName 文件扩展名 |
| 208 | + * @return 文件信息 |
| 209 | + */ |
| 210 | + private FileInfo upload(Object file, String parentPath, String storageCode, String extName) { |
| 211 | + List<String> allExtensions = FileTypeEnum.getAllExtensions(); |
| 212 | + CheckUtils.throwIf(!allExtensions.contains(extName), "不支持的文件类型,仅支持 {} 格式的文件", String |
| 213 | + .join(StringConstants.COMMA, allExtensions)); |
| 214 | + // 构建上传预处理对象 |
| 215 | + StorageDO storage = storageService.getByCode(storageCode); |
| 216 | + CheckUtils.throwIf(DisEnableStatusEnum.DISABLE.equals(storage.getStatus()), "请先启用存储 [{}]", storage.getCode()); |
| 217 | + UploadPretreatment uploadPretreatment = fileStorageService.of(file) |
| 218 | + .setPlatform(storage.getCode()) |
| 219 | + .setHashCalculatorSha256(true) |
| 220 | + .putAttr(ClassUtil.getClassName(StorageDO.class, false), storage) |
| 221 | + .setPath(this.pretreatmentPath(parentPath)); |
| 222 | + // 图片文件生成缩略图 |
| 223 | + if (FileTypeEnum.IMAGE.getExtensions().contains(extName)) { |
| 224 | + uploadPretreatment.setIgnoreThumbnailException(true, true); |
| 225 | + uploadPretreatment.thumbnail(img -> img.size(100, 100)); |
| 226 | + } |
| 227 | + uploadPretreatment.setProgressMonitor(new ProgressListener() { |
| 228 | + @Override |
| 229 | + public void start() { |
| 230 | + log.info("开始上传"); |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public void progress(long progressSize, Long allSize) { |
| 235 | + log.info("已上传 [{}],总大小 [{}]", progressSize, allSize); |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public void finish() { |
| 240 | + log.info("上传结束"); |
| 241 | + } |
| 242 | + }); |
| 243 | + // 创建父级目录 |
| 244 | + this.createParentDir(parentPath, storage); |
| 245 | + // 上传 |
| 246 | + return uploadPretreatment.upload(); |
| 247 | + } |
| 248 | + |
254 | 249 | /** |
255 | 250 | * 处理路径 |
256 | 251 | * |
|
0 commit comments