1、记录文件体积 2、删除文件同步删除云存储上的文件
This commit is contained in:
@@ -33,6 +33,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
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.io.InputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -56,7 +57,6 @@ public class AliOssController extends BaseController {
|
|||||||
@Resource
|
@Resource
|
||||||
private FileRecordService fileRecordService;
|
private FileRecordService fileRecordService;
|
||||||
|
|
||||||
@OperationLog
|
|
||||||
@ApiOperation("上传文件")
|
@ApiOperation("上传文件")
|
||||||
@PostMapping("/upload")
|
@PostMapping("/upload")
|
||||||
public ApiResult<FileRecord> upload(@RequestParam MultipartFile file, HttpServletRequest request) throws Exception{
|
public ApiResult<FileRecord> upload(@RequestParam MultipartFile file, HttpServletRequest request) throws Exception{
|
||||||
@@ -104,7 +104,6 @@ public class AliOssController extends BaseController {
|
|||||||
requestURL = domain;
|
requestURL = domain;
|
||||||
}
|
}
|
||||||
path = "/".concat(path);
|
path = "/".concat(path);
|
||||||
upload.delete();
|
|
||||||
final String cache = redisUtil.get("setting:upload:" + getTenantId());
|
final String cache = redisUtil.get("setting:upload:" + getTenantId());
|
||||||
final JSONObject jsonObject = JSONObject.parseObject(cache);
|
final JSONObject jsonObject = JSONObject.parseObject(cache);
|
||||||
final String bucketDomain = jsonObject.getString("bucketDomain");
|
final String bucketDomain = jsonObject.getString("bucketDomain");
|
||||||
@@ -122,6 +121,7 @@ public class AliOssController extends BaseController {
|
|||||||
String contentType = FileServerUtil.getContentType(upload);
|
String contentType = FileServerUtil.getContentType(upload);
|
||||||
result.setContentType(contentType);
|
result.setContentType(contentType);
|
||||||
fileRecordService.save(result);
|
fileRecordService.save(result);
|
||||||
|
upload.delete();
|
||||||
return success(result);
|
return success(result);
|
||||||
|
|
||||||
} catch (OSSException oe) {
|
} catch (OSSException oe) {
|
||||||
|
|||||||
@@ -2,6 +2,10 @@ package com.gxwebsoft.common.system.controller;
|
|||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.aliyun.oss.OSS;
|
||||||
|
import com.aliyun.oss.OSSClientBuilder;
|
||||||
|
import com.aliyun.oss.common.auth.CredentialsProvider;
|
||||||
|
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
|
||||||
import com.gxwebsoft.common.core.annotation.OperationLog;
|
import com.gxwebsoft.common.core.annotation.OperationLog;
|
||||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||||
import com.gxwebsoft.common.core.utils.FileServerUtil;
|
import com.gxwebsoft.common.core.utils.FileServerUtil;
|
||||||
@@ -209,6 +213,19 @@ public class FileController extends BaseController {
|
|||||||
new File(getUploadSmDir(), record.getPath())
|
new File(getUploadSmDir(), record.getPath())
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
// 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("删除失败");
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.gxwebsoft.common.system.entity;
|
package com.gxwebsoft.common.system.entity;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.DesensitizedUtil;
|
||||||
import com.baomidou.mybatisplus.annotation.*;
|
import com.baomidou.mybatisplus.annotation.*;
|
||||||
import io.swagger.annotations.ApiModel;
|
import io.swagger.annotations.ApiModel;
|
||||||
import io.swagger.annotations.ApiModelProperty;
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
@@ -87,5 +88,4 @@ public class FileRecord implements Serializable {
|
|||||||
@ApiModelProperty("用户头像")
|
@ApiModelProperty("用户头像")
|
||||||
@TableField(exist = false)
|
@TableField(exist = false)
|
||||||
private String avatar;
|
private String avatar;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user