Commit f45fdb3a3590444caf6a6cec1b6e4beb69f363af
1 parent
8fa694a0
feat: 上传图片接口
Showing
10 changed files
with
470 additions
and
7 deletions
sql/init.sql
... | ... | @@ -369,7 +369,7 @@ INSERT INTO `admin_user_role` VALUES (1,1, 1); |
369 | 369 | INSERT INTO `admin_user_role` VALUES (2,2, 2); |
370 | 370 | COMMIT; |
371 | 371 | |
372 | - | |
372 | +DROP TABLE IF EXISTS `dictionary`; | |
373 | 373 | # 字典表 |
374 | 374 | CREATE TABLE `dictionary` ( |
375 | 375 | `id` bigint NOT NULL AUTO_INCREMENT, |
... | ... | @@ -409,4 +409,22 @@ CREATE TABLE `sys_log` ( |
409 | 409 | PRIMARY KEY (`id`) , |
410 | 410 | KEY `log_create_time_index` (`create_time`), |
411 | 411 | KEY `inx_log_type` (`log_type`) |
412 | -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统日志'; | |
413 | 412 | \ No newline at end of file |
413 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='系统日志'; | |
414 | + | |
415 | +DROP TABLE IF EXISTS `local_storage`; | |
416 | +CREATE TABLE `local_storage` ( | |
417 | + `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID', | |
418 | + `real_name` varchar(255) DEFAULT NULL COMMENT '文件真实的名称', | |
419 | + `name` varchar(255) DEFAULT NULL COMMENT '文件名', | |
420 | + `suffix` varchar(255) DEFAULT NULL COMMENT '后缀', | |
421 | + `path` varchar(255) DEFAULT NULL COMMENT '路径', | |
422 | + `type` varchar(255) DEFAULT NULL COMMENT '类型', | |
423 | + `size` varchar(100) DEFAULT NULL COMMENT '大小', | |
424 | + `enable_flag` INT NOT NULL COMMENT '是否可用 10-可用 20-删除', | |
425 | + `create_time` DATETIME NOT NULL COMMENT '创建时间', | |
426 | + `create_by` varchar(64) NOT NULL COMMENT '创建人', | |
427 | + `modify_time` DATETIME DEFAULT NULL COMMENT '修改时间', | |
428 | + `modify_by` varchar(64) DEFAULT NULL COMMENT '修改人', | |
429 | + `version` INT DEFAULT NULL COMMENT '版本号--乐观锁预留字段', | |
430 | + PRIMARY KEY (`id`) USING BTREE | |
431 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='本地存储'; | |
414 | 432 | \ No newline at end of file | ... | ... |
src/main/java/com/order/erp/common/utils/FileUtil.java
... | ... | @@ -195,15 +195,15 @@ public class FileUtil extends cn.hutool.core.io.FileUtil { |
195 | 195 | String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg"; |
196 | 196 | String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg"; |
197 | 197 | if (image.contains(type)) { |
198 | - return "图片"; | |
198 | + return "images"; | |
199 | 199 | } else if (documents.contains(type)) { |
200 | - return "文档"; | |
200 | + return "docs"; | |
201 | 201 | } else if (music.contains(type)) { |
202 | - return "音乐"; | |
202 | + return "musics"; | |
203 | 203 | } else if (video.contains(type)) { |
204 | - return "视频"; | |
204 | + return "videos"; | |
205 | 205 | } else { |
206 | - return "其他"; | |
206 | + return "others"; | |
207 | 207 | } |
208 | 208 | } |
209 | 209 | ... | ... |
src/main/java/com/order/erp/controller/LocalStorageController.java
0 → 100644
1 | +package com.order.erp.controller; | |
2 | + | |
3 | +import com.order.erp.common.annotation.AnonymousAccess; | |
4 | +import com.order.erp.common.constant.ServerResult; | |
5 | +import com.order.erp.service.LocalStorageService; | |
6 | +import io.swagger.annotations.ApiOperation; | |
7 | +import org.springframework.web.bind.annotation.PostMapping; | |
8 | +import org.springframework.web.bind.annotation.RequestMapping; | |
9 | +import org.springframework.web.bind.annotation.RequestParam; | |
10 | +import org.springframework.web.bind.annotation.RestController; | |
11 | +import org.springframework.web.multipart.MultipartFile; | |
12 | + | |
13 | +import javax.annotation.Resource; | |
14 | + | |
15 | +/** | |
16 | + * | |
17 | + */ | |
18 | +@RestController | |
19 | +@RequestMapping("/api/localStorage") | |
20 | +public class LocalStorageController { | |
21 | + | |
22 | + @Resource | |
23 | + private LocalStorageService localStorageService; | |
24 | + | |
25 | + @ApiOperation("上传文件") | |
26 | + @PostMapping(value = "/upload") | |
27 | + @AnonymousAccess | |
28 | + public ServerResult upload(@RequestParam String name, @RequestParam("file") MultipartFile file) { | |
29 | + return localStorageService.create(name, file); | |
30 | + } | |
31 | + | |
32 | +} | |
0 | 33 | \ No newline at end of file | ... | ... |
src/main/java/com/order/erp/domain/dto/LocalStorageDO.java
0 → 100644
1 | +package com.order.erp.domain.dto; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.annotation.TableName; | |
4 | +import lombok.*; | |
5 | +import lombok.experimental.SuperBuilder; | |
6 | + | |
7 | +import java.io.Serializable; | |
8 | + | |
9 | +/** | |
10 | + * 本地存储(LocalStorage)实体类 | |
11 | + * | |
12 | + * @author makejava | |
13 | + * @since 2023-10-08 14:52:15 | |
14 | + */ | |
15 | +@TableName("local_storage") | |
16 | +@Data | |
17 | +@AllArgsConstructor | |
18 | +@ToString | |
19 | +@NoArgsConstructor | |
20 | +@EqualsAndHashCode(callSuper = false) | |
21 | +@SuperBuilder | |
22 | +public class LocalStorageDO extends BaseDO implements Serializable { | |
23 | + private static final long serialVersionUID = 995302526235363711L; | |
24 | + /** | |
25 | + * ID | |
26 | + */ | |
27 | + private Long id; | |
28 | + /** | |
29 | + * 文件真实的名称 | |
30 | + */ | |
31 | + private String realName; | |
32 | + /** | |
33 | + * 文件名 | |
34 | + */ | |
35 | + private String name; | |
36 | + /** | |
37 | + * 后缀 | |
38 | + */ | |
39 | + private String suffix; | |
40 | + /** | |
41 | + * 路径 | |
42 | + */ | |
43 | + private String path; | |
44 | + /** | |
45 | + * 类型 | |
46 | + */ | |
47 | + private String type; | |
48 | + /** | |
49 | + * 大小 | |
50 | + */ | |
51 | + private String size; | |
52 | + | |
53 | +} | ... | ... |
src/main/java/com/order/erp/domain/vo/LocalStorageQueryVO.java
0 → 100644
1 | +package com.order.erp.domain.vo; | |
2 | + | |
3 | +import lombok.*; | |
4 | +import lombok.experimental.SuperBuilder; | |
5 | + | |
6 | +import java.io.Serializable; | |
7 | +import java.util.List; | |
8 | + | |
9 | +/** | |
10 | + * 本地存储(LocalStorage)实体类 | |
11 | + * | |
12 | + * @author makejava | |
13 | + * @since 2023-10-08 14:52:31 | |
14 | + */ | |
15 | +@Data | |
16 | +@AllArgsConstructor | |
17 | +@ToString | |
18 | +@NoArgsConstructor | |
19 | +@EqualsAndHashCode(callSuper = false) | |
20 | +@SuperBuilder | |
21 | +public class LocalStorageQueryVO extends BasePageVO implements Serializable { | |
22 | + private static final long serialVersionUID = -64176392551926441L; | |
23 | + | |
24 | + private List<Long> ids; | |
25 | + | |
26 | + /** | |
27 | + * ID | |
28 | + */ | |
29 | + private Long id; | |
30 | + /** | |
31 | + * 文件真实的名称 | |
32 | + */ | |
33 | + private String realName; | |
34 | + /** | |
35 | + * 文件名 | |
36 | + */ | |
37 | + private String name; | |
38 | + /** | |
39 | + * 后缀 | |
40 | + */ | |
41 | + private String suffix; | |
42 | + /** | |
43 | + * 路径 | |
44 | + */ | |
45 | + private String path; | |
46 | + /** | |
47 | + * 类型 | |
48 | + */ | |
49 | + private String type; | |
50 | + /** | |
51 | + * 大小 | |
52 | + */ | |
53 | + private String size; | |
54 | + | |
55 | + | |
56 | +} | |
57 | + | ... | ... |
src/main/java/com/order/erp/domain/vo/LocalStorageVO.java
0 → 100644
1 | +package com.order.erp.domain.vo; | |
2 | + | |
3 | +import lombok.*; | |
4 | +import lombok.experimental.SuperBuilder; | |
5 | + | |
6 | +import java.io.Serializable; | |
7 | + | |
8 | +/** | |
9 | + * 本地存储(LocalStorage)实体类 | |
10 | + * | |
11 | + * @author makejava | |
12 | + * @since 2023-10-08 14:52:27 | |
13 | + */ | |
14 | +@Data | |
15 | +@AllArgsConstructor | |
16 | +@ToString | |
17 | +@NoArgsConstructor | |
18 | +@EqualsAndHashCode(callSuper = false) | |
19 | +@SuperBuilder | |
20 | +public class LocalStorageVO implements Serializable { | |
21 | + private static final long serialVersionUID = 329772830619051307L; | |
22 | + /** | |
23 | + * ID | |
24 | + */ | |
25 | + private Long id; | |
26 | + /** | |
27 | + * 文件真实的名称 | |
28 | + */ | |
29 | + private String realName; | |
30 | + /** | |
31 | + * 文件名 | |
32 | + */ | |
33 | + private String name; | |
34 | + /** | |
35 | + * 后缀 | |
36 | + */ | |
37 | + private String suffix; | |
38 | + /** | |
39 | + * 路径 | |
40 | + */ | |
41 | + private String path; | |
42 | + /** | |
43 | + * 类型 | |
44 | + */ | |
45 | + private String type; | |
46 | + /** | |
47 | + * 大小 | |
48 | + */ | |
49 | + private String size; | |
50 | + | |
51 | + | |
52 | +} | ... | ... |
src/main/java/com/order/erp/mapper/LocalStorageMapper.java
0 → 100644
1 | +package com.order.erp.mapper; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.core.mapper.BaseMapper; | |
4 | +import com.order.erp.domain.dto.LocalStorageDO; | |
5 | + | |
6 | +/** | |
7 | + * 本地存储(LocalStorage)表数据库访问层 | |
8 | + * | |
9 | + * @author makejava | |
10 | + * @since 2023-10-08 14:52:35 | |
11 | + */ | |
12 | +public interface LocalStorageMapper extends BaseMapper<LocalStorageDO> { | |
13 | + | |
14 | + | |
15 | +} | |
16 | + | ... | ... |
src/main/java/com/order/erp/service/LocalStorageService.java
0 → 100644
1 | +package com.order.erp.service; | |
2 | + | |
3 | +import com.baomidou.mybatisplus.extension.service.IService; | |
4 | +import com.order.erp.common.constant.ServerResult; | |
5 | +import com.order.erp.domain.dto.LocalStorageDO; | |
6 | +import com.order.erp.domain.vo.LocalStorageQueryVO; | |
7 | +import com.order.erp.domain.vo.LocalStorageVO; | |
8 | +import org.springframework.web.multipart.MultipartFile; | |
9 | + | |
10 | +/** | |
11 | + * 本地存储(LocalStorage)表服务接口 | |
12 | + * | |
13 | + * @author makejava | |
14 | + * @since 2023-10-08 14:52:36 | |
15 | + */ | |
16 | +public interface LocalStorageService extends IService<LocalStorageDO> { | |
17 | + | |
18 | + | |
19 | + ServerResult create(String name, MultipartFile file); | |
20 | + | |
21 | + /** | |
22 | + * 通过ID查询单条数据 | |
23 | + * | |
24 | + * @param localStorageQueryVO 主键 | |
25 | + * @return 实例对象 | |
26 | + */ | |
27 | + ServerResult queryById(LocalStorageQueryVO localStorageQueryVO); | |
28 | + | |
29 | + /** | |
30 | + * 分页查询 | |
31 | + * | |
32 | + * @param localStorageQueryVO 筛选条件 | |
33 | + * @return 查询结果 | |
34 | + */ | |
35 | + ServerResult list(LocalStorageQueryVO localStorageQueryVO); | |
36 | + | |
37 | + /** | |
38 | + * 新增数据 | |
39 | + * | |
40 | + * @param localStorageVO 数据VO | |
41 | + * @return 新增结果 | |
42 | + */ | |
43 | + ServerResult add(LocalStorageVO localStorageVO); | |
44 | + | |
45 | + /** | |
46 | + * 修改数据 | |
47 | + * | |
48 | + * @param localStorageVO 数据VO | |
49 | + * @return 编辑结果 | |
50 | + */ | |
51 | + ServerResult edit(LocalStorageVO localStorageVO); | |
52 | + | |
53 | + /** | |
54 | + * 通过主键删除数据 | |
55 | + * | |
56 | + * @param localStorageQueryVO 筛选条件 | |
57 | + * @return 是否成功 | |
58 | + */ | |
59 | + ServerResult deleteById(LocalStorageQueryVO localStorageQueryVO); | |
60 | + | |
61 | +} | ... | ... |
src/main/java/com/order/erp/service/impl/LocalStorageServiceImpl.java
0 → 100644
1 | +package com.order.erp.service.impl; | |
2 | + | |
3 | +import cn.hutool.core.bean.BeanUtil; | |
4 | +import cn.hutool.core.collection.CollUtil; | |
5 | +import cn.hutool.core.util.ObjectUtil; | |
6 | +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | |
7 | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |
8 | +import com.order.erp.common.constant.Constant; | |
9 | +import com.order.erp.common.constant.ServerResult; | |
10 | +import com.order.erp.common.exception.BusinessException; | |
11 | +import com.order.erp.common.utils.FileUtil; | |
12 | +import com.order.erp.common.utils.StringUtils; | |
13 | +import com.order.erp.domain.dto.LocalStorageDO; | |
14 | +import com.order.erp.domain.vo.LocalStorageQueryVO; | |
15 | +import com.order.erp.domain.vo.LocalStorageVO; | |
16 | +import com.order.erp.mapper.LocalStorageMapper; | |
17 | +import com.order.erp.service.LocalStorageService; | |
18 | +import lombok.extern.slf4j.Slf4j; | |
19 | +import org.springframework.beans.factory.annotation.Value; | |
20 | +import org.springframework.stereotype.Service; | |
21 | +import org.springframework.web.multipart.MultipartFile; | |
22 | + | |
23 | +import java.io.File; | |
24 | +import java.util.List; | |
25 | +import java.util.Objects; | |
26 | + | |
27 | +/** | |
28 | + * 本地存储(LocalStorage)表服务实现类 | |
29 | + * | |
30 | + * @author makejava | |
31 | + * @since 2023-10-08 14:52:36 | |
32 | + */ | |
33 | +@Slf4j | |
34 | +@Service | |
35 | +public class LocalStorageServiceImpl extends ServiceImpl<LocalStorageMapper, LocalStorageDO> implements LocalStorageService { | |
36 | + | |
37 | + | |
38 | + @Value("${file.path}") | |
39 | + private String path; | |
40 | + | |
41 | + @Value("${file.host}") | |
42 | + private String host; | |
43 | + | |
44 | + @Value("${file.maxSize}") | |
45 | + private long maxSize; | |
46 | + | |
47 | + @Override | |
48 | + public ServerResult create(String name, MultipartFile multipartFile) { | |
49 | + FileUtil.checkSize(maxSize, multipartFile.getSize()); | |
50 | + String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename()); | |
51 | + String type = FileUtil.getFileType(suffix); | |
52 | + File file = FileUtil.upload(multipartFile, path + type + File.separator); | |
53 | + if (ObjectUtil.isNull(file)) { | |
54 | + throw new BusinessException("上传失败"); | |
55 | + } | |
56 | + try { | |
57 | + name = StringUtils.isBlank(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name; | |
58 | + LocalStorageDO localStorage = LocalStorageDO.builder() | |
59 | + .realName(file.getName()) | |
60 | + .name(name) | |
61 | + .suffix(suffix) | |
62 | + .path(file.getPath()) | |
63 | + .type(type) | |
64 | + .size(FileUtil.getSize(multipartFile.getSize())).build(); | |
65 | + save(localStorage); | |
66 | + return ServerResult.success(host + "/images/" + file.getName()); | |
67 | + } catch (Exception e) { | |
68 | + FileUtil.del(file); | |
69 | + throw e; | |
70 | + } | |
71 | + } | |
72 | + | |
73 | + /** | |
74 | + * 通过ID查询单条数据 | |
75 | + * <p> | |
76 | + * localStorageQueryVO 主键 | |
77 | + * | |
78 | + * @return 实例对象 | |
79 | + */ | |
80 | + @Override | |
81 | + public ServerResult queryById(LocalStorageQueryVO localStorageQueryVO) { | |
82 | + if (Objects.isNull(localStorageQueryVO.getId())) { | |
83 | + return ServerResult.fail("id 不能为空"); | |
84 | + } | |
85 | + LocalStorageDO LocalStorageDo = getById(localStorageQueryVO.getId()); | |
86 | + if (Objects.isNull(LocalStorageDo)) { | |
87 | + return ServerResult.success(null); | |
88 | + } | |
89 | + return ServerResult.success(BeanUtil.copyProperties(LocalStorageDo, LocalStorageVO.class)); | |
90 | + } | |
91 | + | |
92 | + /** | |
93 | + * 分页查询 | |
94 | + * | |
95 | + * @param localStorageQueryVO 筛选条件 | |
96 | + * @return 查询结果 | |
97 | + */ | |
98 | + @Override | |
99 | + public ServerResult list(LocalStorageQueryVO localStorageQueryVO) { | |
100 | + | |
101 | + return ServerResult.success(); | |
102 | + } | |
103 | + | |
104 | + /** | |
105 | + * 新增数据 | |
106 | + * | |
107 | + * @param localStorageVO 实例对象 | |
108 | + * @return 实例对象 | |
109 | + */ | |
110 | + @Override | |
111 | + public ServerResult add(LocalStorageVO localStorageVO) { | |
112 | + //todo 校验 | |
113 | + if (Objects.nonNull(localStorageVO.getId())) { | |
114 | + localStorageVO.setId(null); | |
115 | + } | |
116 | + LocalStorageDO localStorageDo = BeanUtil.copyProperties(localStorageVO, LocalStorageDO.class); | |
117 | + | |
118 | + save(localStorageDo); | |
119 | + | |
120 | + return ServerResult.success(); | |
121 | + } | |
122 | + | |
123 | + /** | |
124 | + * 修改数据 | |
125 | + * | |
126 | + * @param localStorageVO 实例对象 | |
127 | + * @return 实例对象 | |
128 | + */ | |
129 | + @Override | |
130 | + public ServerResult edit(LocalStorageVO localStorageVO) { | |
131 | + //todo 校验 | |
132 | + if (Objects.isNull(localStorageVO.getId())) { | |
133 | + return ServerResult.fail("id 不能为空"); | |
134 | + } | |
135 | + LocalStorageDO localStorageDo = BeanUtil.copyProperties(localStorageVO, LocalStorageDO.class); | |
136 | + | |
137 | + updateById(localStorageDo); | |
138 | + | |
139 | + return ServerResult.success(); | |
140 | + } | |
141 | + | |
142 | + /** | |
143 | + * 通过主键删除数据 | |
144 | + * | |
145 | + * @param localStorageQueryVO 筛选条件 | |
146 | + * @return 是否成功 | |
147 | + */ | |
148 | + @Override | |
149 | + public ServerResult deleteById(LocalStorageQueryVO localStorageQueryVO) { | |
150 | + List<Long> ids = localStorageQueryVO.getIds(); | |
151 | + if (CollUtil.isEmpty(ids)) { | |
152 | + return ServerResult.fail("ids 参数不能为空"); | |
153 | + } | |
154 | + List<LocalStorageDO> localStorageList = listByIds(ids); | |
155 | + if (CollUtil.isEmpty(localStorageList)) { | |
156 | + return ServerResult.success(); | |
157 | + } | |
158 | + //todo 校验是否可以逻辑删除 | |
159 | + LambdaUpdateWrapper<LocalStorageDO> updateWrapper = new LambdaUpdateWrapper<LocalStorageDO>() | |
160 | + .in(LocalStorageDO::getId, ids) | |
161 | + .set(LocalStorageDO::getEnableFlag, Constant.UNABLE_TWENTY); | |
162 | + update(updateWrapper); | |
163 | + return ServerResult.success(); | |
164 | + } | |
165 | +} | ... | ... |
src/main/resources/application-local.yml
... | ... | @@ -158,3 +158,12 @@ system: |
158 | 158 | |
159 | 159 | openai: |
160 | 160 | token: Bearer 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 |
161 | + | |
162 | +# 文件存储路径 | |
163 | +file: | |
164 | + path: /home/canrd/order-erp/files/ | |
165 | + host: http://39.108.227.113 | |
166 | + avatar: /home/order-erp/avatar/ | |
167 | + # 文件大小 /M | |
168 | + maxSize: 100 | |
169 | + avatarMaxSize: 5 | ... | ... |