批量删除文件同步删除远程文件

This commit is contained in:
gxwebsoft
2024-03-19 18:04:00 +08:00
parent 8b4bb2647a
commit 7e0131edcf
7 changed files with 117 additions and 57 deletions

View File

@@ -276,4 +276,6 @@ public class RedisUtil {
map.put("bucketDomain",bucketDomain); map.put("bucketDomain",bucketDomain);
return map; return map;
} }
} }

View File

@@ -32,6 +32,7 @@ import com.gxwebsoft.common.system.service.SettingService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;

View File

@@ -18,6 +18,7 @@ import com.gxwebsoft.common.system.entity.FileRecord;
import com.gxwebsoft.common.system.param.FileRecordParam; import com.gxwebsoft.common.system.param.FileRecordParam;
import com.gxwebsoft.common.system.service.CompanyService; import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.FileRecordService; import com.gxwebsoft.common.system.service.FileRecordService;
import com.gxwebsoft.common.system.service.SettingService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
@@ -53,6 +54,8 @@ public class FileController extends BaseController {
private FileRecordService fileRecordService; private FileRecordService fileRecordService;
@Resource @Resource
private CompanyService companyService; private CompanyService companyService;
@Resource
private SettingService settingService;
@PreAuthorize("hasAuthority('sys:file:upload')") @PreAuthorize("hasAuthority('sys:file:upload')")
@OperationLog @OperationLog
@@ -210,35 +213,17 @@ public class FileController extends BaseController {
@DeleteMapping("/remove/{id}") @DeleteMapping("/remove/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) { public ApiResult<?> remove(@PathVariable("id") Integer id) {
FileRecord record = fileRecordService.getById(id); FileRecord record = fileRecordService.getById(id);
List<FileRecord> fileRecords = new ArrayList<>();
fileRecords.add(record);
if (fileRecordService.removeById(id)) { if (fileRecordService.removeById(id)) {
if (StrUtil.isNotBlank(record.getPath())) { if (StrUtil.isNotBlank(record.getPath())) {
fileRecordService.deleteFileAsync(Arrays.asList( // 删除文件
fileRecordService.deleteFileAsync(Arrays.asList(
new File(getUploadDir(), record.getPath()), new File(getUploadDir(), record.getPath()),
new File(getUploadSmDir(), record.getPath()) new File(getUploadSmDir(), record.getPath())
)); ));
} }
// 释放空间大小 fileRecordService.deleteOssFileAsync(fileRecords);
System.out.println("record = " + record);
if(record.getCompanyId() > 0){
Company company = companyService.getById(record.getCompanyId());
company.setStorage(company.getStorage() - record.getLength());
companyService.updateById(company);
}
// 删除远程文件
// Endpoint以华东1杭州为例其它Region请按实际情况填写。
String endpoint = config.getEndpoint();
String accessKeyId = config.getAccessKeyId();
String accessKeySecret = config.getAccessKeySecret();
String bucketName = config.getBucketName();
String bucketDomain = config.getBucketDomain();
// 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
// 删除云存储上的图片
ossClient.deleteObject(bucketName,StrUtil.replace(record.getPath(), bucketDomain.concat("/"), ""));
return success("删除成功"); return success("删除成功");
} }
return fail("删除失败"); return fail("删除失败");
@@ -262,6 +247,7 @@ public class FileController extends BaseController {
} }
} }
fileRecordService.deleteFileAsync(files); fileRecordService.deleteFileAsync(files);
fileRecordService.deleteOssFileAsync(fileRecords);
return success("删除成功"); return success("删除成功");
} }
return fail("删除失败"); return fail("删除失败");

View File

@@ -55,4 +55,6 @@ public interface FileRecordService extends IService<FileRecord> {
* @param files 文件数组 * @param files 文件数组
*/ */
void deleteFileAsync(List<File> files); void deleteFileAsync(List<File> files);
void deleteOssFileAsync(List<FileRecord> fileRecords);
} }

View File

@@ -56,4 +56,5 @@ public interface SettingService extends IService<Setting> {
Config getConfig(Integer tenantId); Config getConfig(Integer tenantId);
JSONObject getUploadConfig(Integer tenantId);
} }

View File

@@ -1,5 +1,13 @@
package com.gxwebsoft.common.system.service.impl; package com.gxwebsoft.common.system.service.impl;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.utils.CommonUtil; import com.gxwebsoft.common.core.utils.CommonUtil;
@@ -11,12 +19,14 @@ import com.gxwebsoft.common.system.mapper.FileRecordMapper;
import com.gxwebsoft.common.system.param.FileRecordParam; import com.gxwebsoft.common.system.param.FileRecordParam;
import com.gxwebsoft.common.system.service.CompanyService; import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.FileRecordService; import com.gxwebsoft.common.system.service.FileRecordService;
import com.gxwebsoft.common.system.service.SettingService;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.File; import java.io.File;
import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
@@ -27,44 +37,78 @@ import java.util.List;
*/ */
@Service @Service
public class FileRecordServiceImpl extends ServiceImpl<FileRecordMapper, FileRecord> implements FileRecordService { public class FileRecordServiceImpl extends ServiceImpl<FileRecordMapper, FileRecord> implements FileRecordService {
@Resource @Resource
private CompanyService companyService; private CompanyService companyService;
@Resource
private SettingService settingService;
@Override @Override
public PageResult<FileRecord> pageRel(FileRecordParam param) { public PageResult<FileRecord> pageRel(FileRecordParam param) {
PageParam<FileRecord, FileRecordParam> page = new PageParam<>(param); PageParam<FileRecord, FileRecordParam> page = new PageParam<>(param);
page.setDefaultOrder("create_time desc"); page.setDefaultOrder("create_time desc");
return new PageResult<>(baseMapper.selectPageRel(page, param), page.getTotal()); return new PageResult<>(baseMapper.selectPageRel(page, param), page.getTotal());
}
@Override
public List<FileRecord> listRel(FileRecordParam param) {
PageParam<FileRecord, FileRecordParam> page = new PageParam<>(param);
page.setDefaultOrder("create_time desc");
return page.sortRecords(baseMapper.selectListRel(param));
}
@Override
public FileRecord getByIdRel(Integer id) {
FileRecordParam param = new FileRecordParam();
param.setId(id);
return param.getOne(baseMapper.selectListRel(param));
}
@Override
public FileRecord getByIdPath(String path) {
return CommonUtil.listGetOne(baseMapper.getByIdPath(path));
}
@Async
@Override
public void deleteFileAsync(List<File> files) {
for (File file : files) {
try {
file.delete();
} catch (Exception e) {
e.printStackTrace();
}
} }
@Override }
public List<FileRecord> listRel(FileRecordParam param) {
PageParam<FileRecord, FileRecordParam> page = new PageParam<>(param);
page.setDefaultOrder("create_time desc");
return page.sortRecords(baseMapper.selectListRel(param));
}
@Override @Override
public FileRecord getByIdRel(Integer id) { public void deleteOssFileAsync(List<FileRecord> fileRecords) {
FileRecordParam param = new FileRecordParam(); // 读取云存储配置信息
param.setId(id); final FileRecord record = fileRecords.get(0);
return param.getOne(baseMapper.selectListRel(param)); System.out.println("record = " + record);
} final JSONObject uploadConfig = settingService.getUploadConfig(record.getTenantId());
String endpoint = uploadConfig.getString("bucketEndpoint");
@Override String bucketDomain = uploadConfig.getString("bucketDomain");
public FileRecord getByIdPath(String path) { String bucketName = uploadConfig.getString("bucketName");
return CommonUtil.listGetOne(baseMapper.getByIdPath(path)); String accessKeyId = uploadConfig.getString("accessKeyId");
} String accessKeySecret = uploadConfig.getString("accessKeySecret");
CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
@Async OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
@Override for (FileRecord fileRecord : fileRecords) {
public void deleteFileAsync(List<File> files) { fileRecord.setPath(StrUtil.replace(fileRecord.getPath(), bucketDomain.concat("/"), ""));
for (File file : files) { try {
try { // 删除远程文件
file.delete(); ossClient.deleteObject(bucketName, fileRecord.getPath());
} catch (Exception e) { // 释放空间大小
e.printStackTrace(); if (fileRecord.getCompanyId() > 0) {
} Company company = companyService.getById(fileRecord.getCompanyId());
company.setStorage(company.getStorage() - fileRecord.getLength());
companyService.updateById(company);
} }
} catch (Exception e) {
e.printStackTrace();
}
} }
}
} }

View File

@@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.common.core.config.ConfigProperties; import com.gxwebsoft.common.core.config.ConfigProperties;
import com.gxwebsoft.common.core.exception.BusinessException; import com.gxwebsoft.common.core.exception.BusinessException;
import com.gxwebsoft.common.core.utils.RedisUtil;
import com.gxwebsoft.common.core.web.PageParam; import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult; import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.Setting; import com.gxwebsoft.common.system.entity.Setting;
@@ -40,6 +41,8 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
private ConfigProperties pathConfig; private ConfigProperties pathConfig;
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
@Resource
private RedisUtil redisUtil;
@Override @Override
public PageResult<Setting> pageRel(SettingParam param) { public PageResult<Setting> pageRel(SettingParam param) {
@@ -152,4 +155,25 @@ public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> impl
return configMap.get(tenantId.toString()); return configMap.get(tenantId.toString());
} }
@Override
public JSONObject getUploadConfig(Integer tenantId) {
// 读取配置信息
JSONObject settingInfo;
String key3 = "Upload:" + tenantId;
settingInfo = redisUtil.get(key3, JSONObject.class);
if (settingInfo == null) {
settingInfo = getBySettingKey("upload");
if(settingInfo == null){
return null;
}
redisUtil.set(key3,settingInfo);
}
// String endpoint = settingInfo.getString("bucketEndpoint");
// String bucketDomain = settingInfo.getString("bucketDomain");
// String bucketName = settingInfo.getString("bucketName");
// String accessKeyId = settingInfo.getString("accessKeyId");
// String accessKeySecret = settingInfo.getString("accessKeySecret");
return settingInfo;
}
} }