update 使用 策略+工厂 重写OSS模块
This commit is contained in:
@@ -1,58 +1,74 @@
|
||||
package com.ruoyi.system.controller;
|
||||
|
||||
|
||||
import com.aliyun.oss.ServiceException;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.annotation.RepeatSubmit;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.enums.BusinessType;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.common.utils.PageUtils;
|
||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import com.ruoyi.system.service.ISysOssService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传 控制层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
@RestController
|
||||
@RequestMapping("/system/oss")
|
||||
public class SysOssController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private ISysOssService iSysOssService;
|
||||
private final ISysOssService iSysOssService;
|
||||
|
||||
/**
|
||||
* 查询文件上传列表
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:oss:list')")
|
||||
@GetMapping("/list")
|
||||
public TableDataInfo list(SysOss sysOss) {
|
||||
List<SysOss> list = iSysOssService.list(sysOss);
|
||||
return PageUtils.buildDataInfo(list);
|
||||
public TableDataInfo<SysOss> list(SysOss sysOss) {
|
||||
return iSysOssService.queryPageList(sysOss);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传图片
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:oss:upload')")
|
||||
@Log(title = "OSS云存储", businessType = BusinessType.INSERT)
|
||||
@RepeatSubmit
|
||||
@PostMapping("/upload")
|
||||
public AjaxResult upload(@RequestParam("file") MultipartFile file) {
|
||||
public AjaxResult<Map<String, String>> upload(@RequestParam("file") MultipartFile file) {
|
||||
if (file.isEmpty()) {
|
||||
throw new CustomException("上传文件不能为空");
|
||||
}
|
||||
Map<String, String> json = iSysOssService.upload(file);
|
||||
return AjaxResult.success(json);
|
||||
SysOss oss = iSysOssService.upload(file);
|
||||
Map<String, String> map = new HashMap<>(2);
|
||||
map.put("url", oss.getUrl());
|
||||
map.put("fileName", oss.getFileName());
|
||||
return AjaxResult.success(map);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除OSS云存储
|
||||
*/
|
||||
@PreAuthorize("@ss.hasPermi('system:oss:remove')")
|
||||
@Log(title = "OSS云存储" , businessType = BusinessType.DELETE)
|
||||
@DeleteMapping("/{ossIds}")
|
||||
public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
|
||||
@PathVariable Long[] ossIds) {
|
||||
return toAjax(iSysOssService.deleteByIds(Arrays.asList(ossIds)) ? 1 : 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,20 +9,24 @@ import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 文件上传表 sys_oss
|
||||
* OSS云存储对象
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@NoArgsConstructor
|
||||
@Accessors(chain = true)
|
||||
@TableName("sys_oss")
|
||||
public class SysOss implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 云存储主键
|
||||
*/
|
||||
@TableId(value = "oss_id")
|
||||
private Long ossId;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
@@ -51,8 +55,21 @@ public class SysOss implements Serializable {
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private String createBy;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private String updateBy;
|
||||
|
||||
/**
|
||||
* 服务商
|
||||
*/
|
||||
private Integer service;
|
||||
private String service;
|
||||
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
package com.ruoyi.system.factory;
|
||||
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
|
||||
/**
|
||||
* 文件上传Factory
|
||||
*/
|
||||
public class OSSFactory {
|
||||
|
||||
// private static ISysConfigService sysConfigService;
|
||||
|
||||
static {
|
||||
// OSSFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class);
|
||||
}
|
||||
|
||||
public static AbstractCloudStorageService build() {
|
||||
// String jsonconfig = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);
|
||||
// // 获取云存储配置信息
|
||||
// CloudStorageConfig config = JSON.parseObject(jsonconfig, CloudStorageConfig.class);
|
||||
// if (config.getType() == CloudConstant.CloudService.QINIU.getValue()) {
|
||||
// return new QiniuCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.ALIYUN.getValue()) {
|
||||
// return new AliyunCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.QCLOUD.getValue()) {
|
||||
// return new QcloudCloudStorageServiceImpl(config);
|
||||
// } else if (config.getType() == CloudConstant.CloudService.MINIO.getValue()) {
|
||||
// return new MinioCloudStorageServiceImpl(config);
|
||||
// }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,7 @@ import com.ruoyi.system.domain.SysOss;
|
||||
/**
|
||||
* 文件上传 数据层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
* @author Lion Li
|
||||
*/
|
||||
public interface SysOssMapper extends BaseMapper<SysOss> {
|
||||
}
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
package com.ruoyi.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 文件上传 服务层
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
* @author Lion Li
|
||||
*/
|
||||
public interface ISysOssService extends IService<SysOss> {
|
||||
/**
|
||||
* 列表查询
|
||||
*/
|
||||
List<SysOss> list(SysOss sysOss);
|
||||
|
||||
Map<String, String> upload(MultipartFile file);
|
||||
TableDataInfo<SysOss> queryPageList(SysOss sysOss);
|
||||
|
||||
SysOss upload(MultipartFile file);
|
||||
|
||||
Boolean deleteByIds(Collection<Long> ids);
|
||||
}
|
||||
|
||||
@@ -1,63 +1,71 @@
|
||||
package com.ruoyi.system.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.oss.config.CloudStorageConfig;
|
||||
import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService;
|
||||
import com.ruoyi.common.utils.PageUtils;
|
||||
import com.ruoyi.oss.factory.OssFactory;
|
||||
import com.ruoyi.oss.service.ICloudStorageService;
|
||||
import com.ruoyi.system.domain.SysOss;
|
||||
import com.ruoyi.system.factory.OSSFactory;
|
||||
import com.ruoyi.system.mapper.SysOssMapper;
|
||||
import com.ruoyi.system.service.ISysOssService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 文件上传 服务层实现
|
||||
*
|
||||
* @author chkj
|
||||
* @date 2019-07-15
|
||||
* @author Lion Li
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SysOssServiceImpl extends ServiceImpl<SysOssMapper, SysOss> implements ISysOssService {
|
||||
|
||||
@Autowired
|
||||
private CloudStorageConfig config;
|
||||
|
||||
@Override
|
||||
public List<SysOss> list(SysOss sysOss) {
|
||||
LambdaQueryWrapper<SysOss> wrapper = new LambdaQueryWrapper<>();
|
||||
return baseMapper.selectList(wrapper);
|
||||
public TableDataInfo<SysOss> queryPageList(SysOss sysOss) {
|
||||
LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery();
|
||||
lqw.like(StrUtil.isNotBlank(sysOss.getFileName()), SysOss::getFileName, sysOss.getFileName());
|
||||
lqw.like(StrUtil.isNotBlank(sysOss.getFileSuffix()), SysOss::getFileSuffix, sysOss.getFileSuffix());
|
||||
lqw.like(StrUtil.isNotBlank(sysOss.getUrl()), SysOss::getUrl, sysOss.getUrl());
|
||||
lqw.like(StrUtil.isNotBlank(sysOss.getService()), SysOss::getService, sysOss.getService());
|
||||
return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> upload(MultipartFile file) {
|
||||
public SysOss upload(MultipartFile file) {
|
||||
String originalfileName = file.getOriginalFilename();
|
||||
String suffix = originalfileName.substring(originalfileName.lastIndexOf("."));
|
||||
String suffix = StrUtil.sub(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
|
||||
try {
|
||||
AbstractCloudStorageService storage = OSSFactory.build();
|
||||
ICloudStorageService storage = OssFactory.instance();
|
||||
String url = storage.uploadSuffix(file.getBytes(), suffix);
|
||||
// 保存文件信息
|
||||
SysOss ossEntity = new SysOss()
|
||||
SysOss oss = new SysOss()
|
||||
.setUrl(url).setFileSuffix(suffix)
|
||||
.setFileName(originalfileName)
|
||||
.setService(storage.getServiceType());
|
||||
save(ossEntity);
|
||||
Map<String, String> map = new HashMap<>(2);
|
||||
map.put("url", ossEntity.getUrl());
|
||||
map.put("fileName", ossEntity.getFileName());
|
||||
return map;
|
||||
save(oss);
|
||||
return oss;
|
||||
} catch (IOException e) {
|
||||
throw new CustomException("文件读取异常!!!", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deleteByIds(Collection<Long> ids) {
|
||||
List<SysOss> list = listByIds(ids);
|
||||
for (SysOss sysOss : list) {
|
||||
ICloudStorageService storage = OssFactory.instance(sysOss.getService());
|
||||
storage.delete(sysOss.getUrl());
|
||||
}
|
||||
return removeByIds(ids);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user