|
|
@@ -0,0 +1,122 @@
|
|
|
+package cn.tr.module.sys.storage.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.tr.core.utils.JsonUtils;
|
|
|
+import cn.tr.module.sys.storage.dto.SysStorageConfigDTO;
|
|
|
+import cn.tr.module.sys.storage.dto.SysStorageRecordDTO;
|
|
|
+import cn.tr.module.sys.storage.service.IStorageFileService;
|
|
|
+import cn.tr.module.sys.storage.service.ISysStorageConfigService;
|
|
|
+import cn.tr.module.sys.storage.service.ISysStorageRecordService;
|
|
|
+import cn.tr.plugin.file.config.FileClient;
|
|
|
+import cn.tr.plugin.file.config.FileClientConfig;
|
|
|
+import cn.tr.plugin.file.config.FileClientFactory;
|
|
|
+import cn.tr.plugin.file.enums.FileStorageEnum;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.context.annotation.Lazy;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName : StorageFileServiceImpl
|
|
|
+ * @Description :
|
|
|
+ * @Author : LF
|
|
|
+ * @Date: 2023年05月10日
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class StorageFileServiceImpl implements IStorageFileService {
|
|
|
+ //默认配置
|
|
|
+ private SysStorageConfigDTO defaultConfig;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private FileClientFactory fileClientFactory;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysStorageConfigService storageConfigService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ @Lazy
|
|
|
+ private ISysStorageRecordService storageRecordService;
|
|
|
+ @Override
|
|
|
+ @PostConstruct
|
|
|
+ public void initDefaultConfig() {
|
|
|
+ defaultConfig=storageConfigService.selectDefault();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public SysStorageRecordDTO upload(String configId,String cateId,String filename, byte[] content) throws Exception {
|
|
|
+ SysStorageConfigDTO config=defaultConfig;
|
|
|
+ if(StrUtil.isNotEmpty(configId)){
|
|
|
+ config=storageConfigService.selectSysStorageConfigById(configId);
|
|
|
+ }
|
|
|
+ String bizName = createBizName(filename);
|
|
|
+ String suffix = FileUtil.extName(filename);
|
|
|
+ FileClient fileClient = getClient(config);
|
|
|
+ fileClient.upload(content,bizName);
|
|
|
+ String downUrl = fileClient.downUrl(bizName);
|
|
|
+ SysStorageRecordDTO record = SysStorageRecordDTO.builder()
|
|
|
+ .realName(filename)
|
|
|
+ .bizName(bizName)
|
|
|
+ .absolutePath(downUrl)
|
|
|
+ .suffix(suffix)
|
|
|
+ .cateId(cateId)
|
|
|
+ .configId(defaultConfig.getId())
|
|
|
+ .size(content.length)
|
|
|
+ .build();
|
|
|
+ String recordId = storageRecordService.insertSysStorageRecordReturnId(record);
|
|
|
+ record.setId(recordId);
|
|
|
+ return record;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void remove(Collection<String> recordIds) {
|
|
|
+ List<SysStorageRecordDTO> records = storageRecordService.selectSysStorageRecordByIds(recordIds);
|
|
|
+ Map<String, List<SysStorageRecordDTO>> recordByConfigId = records.stream()
|
|
|
+ .collect(Collectors.groupingBy(SysStorageRecordDTO::getConfigId));
|
|
|
+ List<SysStorageConfigDTO> configs = storageConfigService.selectSysStorageConfigByIds(recordByConfigId.keySet());
|
|
|
+ configs.stream()
|
|
|
+ .map(config->getClient(config))
|
|
|
+ .forEach(client->{
|
|
|
+ List<SysStorageRecordDTO> storageRecords = recordByConfigId.get(client.getId());
|
|
|
+ for (SysStorageRecordDTO storageRecord : storageRecords) {
|
|
|
+ client.delete(storageRecord.getBizName());
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String obtainDownloadPath(String recordId) throws Exception {
|
|
|
+ SysStorageRecordDTO record = storageRecordService.selectSysStorageRecordById(recordId);
|
|
|
+ SysStorageConfigDTO config = storageConfigService.selectSysStorageConfigById(record.getConfigId());
|
|
|
+ FileClient fileClient = getClient(config);
|
|
|
+ return fileClient.downUrl(record.getBizName());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private FileClient getClient(SysStorageConfigDTO storageConfig){
|
|
|
+ return fileClientFactory.createOrUpdateFileClient(storageConfig.getId(), storageConfig.getType(),parseClientConfig(storageConfig.getType(), storageConfig.getConfig()));
|
|
|
+ }
|
|
|
+
|
|
|
+ private FileClientConfig parseClientConfig(String storage, Map<String, Object> config) {
|
|
|
+ Class<? extends FileClientConfig> configClass = FileStorageEnum.getByStorage(storage)
|
|
|
+ .getConfigClass();
|
|
|
+ config.put("@Clazz",configClass.getName());
|
|
|
+ return JsonUtils.parseObject(JsonUtils.toJsonString(config), configClass);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String createBizName(String realName){
|
|
|
+ return DateUtil.today() +"/"+ RandomUtil.randomString(4)+"-"+realName;
|
|
|
+ }
|
|
|
+}
|