Commit 18622c5d064a30a1c8eb3db5ceb68df615c8c5d8
Committed by
Gitee
1 parent
ccc4cad1
删除文件 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/mo…
…dule/infra/service/file/FileServiceImpl.java
Showing
1 changed file
with
0 additions
and
96 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.service.file; | |
| 2 | - | |
| 3 | -import cn.hutool.core.lang.Assert; | |
| 4 | -import cn.hutool.core.util.StrUtil; | |
| 5 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 6 | -import cn.iocoder.yudao.framework.common.util.io.FileUtils; | |
| 7 | -import cn.iocoder.yudao.framework.file.core.client.FileClient; | |
| 8 | -import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; | |
| 9 | -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; | |
| 10 | -import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; | |
| 11 | -import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; | |
| 12 | -import lombok.SneakyThrows; | |
| 13 | -import org.springframework.stereotype.Service; | |
| 14 | - | |
| 15 | -import javax.annotation.Resource; | |
| 16 | - | |
| 17 | -import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 18 | -import static cn.iocoder.yudao.module.infra.enums.ErrorCodeConstants.FILE_NOT_EXISTS; | |
| 19 | - | |
| 20 | -/** | |
| 21 | - * 文件 Service 实现类 | |
| 22 | - * | |
| 23 | - * @author 芋道源码 | |
| 24 | - */ | |
| 25 | -@Service | |
| 26 | -public class FileServiceImpl implements FileService { | |
| 27 | - | |
| 28 | - @Resource | |
| 29 | - private FileConfigService fileConfigService; | |
| 30 | - | |
| 31 | - @Resource | |
| 32 | - private FileMapper fileMapper; | |
| 33 | - | |
| 34 | - @Override | |
| 35 | - public PageResult<FileDO> getFilePage(FilePageReqVO pageReqVO) { | |
| 36 | - return fileMapper.selectPage(pageReqVO); | |
| 37 | - } | |
| 38 | - | |
| 39 | - @Override | |
| 40 | - @SneakyThrows | |
| 41 | - public String createFile(String name, String path, byte[] content) { | |
| 42 | - // 计算默认的 path 名,path可增加自定义路径如 aaa/,aaa/bbb/ | |
| 43 | - String type = FileTypeUtils.getMineType(content, name); | |
| 44 | - path = StrUtil.isEmpty(path) ? FileUtils.generatePath(content, name) : path + FileUtils.generatePath(content, name); | |
| 45 | - // 如果 name 为空,则使用 path 填充 | |
| 46 | - if (StrUtil.isEmpty(name)) { | |
| 47 | - name = path; | |
| 48 | - } | |
| 49 | - | |
| 50 | - // 上传到文件存储器 | |
| 51 | - FileClient client = fileConfigService.getMasterFileClient(); | |
| 52 | - Assert.notNull(client, "客户端(master) 不能为空"); | |
| 53 | - String url = client.upload(content, path, type); | |
| 54 | - | |
| 55 | - // 保存到数据库 | |
| 56 | - FileDO file = new FileDO(); | |
| 57 | - file.setConfigId(client.getId()); | |
| 58 | - file.setName(name); | |
| 59 | - file.setPath(path); | |
| 60 | - file.setUrl(url); | |
| 61 | - file.setType(type); | |
| 62 | - file.setSize(content.length); | |
| 63 | - fileMapper.insert(file); | |
| 64 | - return url; | |
| 65 | - } | |
| 66 | - | |
| 67 | - @Override | |
| 68 | - public void deleteFile(Long id) throws Exception { | |
| 69 | - // 校验存在 | |
| 70 | - FileDO file = this.validateFileExists(id); | |
| 71 | - | |
| 72 | - // 从文件存储器中删除 | |
| 73 | - FileClient client = fileConfigService.getFileClient(file.getConfigId()); | |
| 74 | - Assert.notNull(client, "客户端({}) 不能为空", file.getConfigId()); | |
| 75 | - client.delete(file.getPath()); | |
| 76 | - | |
| 77 | - // 删除记录 | |
| 78 | - fileMapper.deleteById(id); | |
| 79 | - } | |
| 80 | - | |
| 81 | - private FileDO validateFileExists(Long id) { | |
| 82 | - FileDO fileDO = fileMapper.selectById(id); | |
| 83 | - if (fileDO == null) { | |
| 84 | - throw exception(FILE_NOT_EXISTS); | |
| 85 | - } | |
| 86 | - return fileDO; | |
| 87 | - } | |
| 88 | - | |
| 89 | - @Override | |
| 90 | - public byte[] getFileContent(Long configId, String path) throws Exception { | |
| 91 | - FileClient client = fileConfigService.getFileClient(configId); | |
| 92 | - Assert.notNull(client, "客户端({}) 不能为空", configId); | |
| 93 | - return client.getContent(path); | |
| 94 | - } | |
| 95 | - | |
| 96 | -} |