存储管理
This commit is contained in:
@@ -27,7 +27,7 @@ import java.util.Map;
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "工具:本地存储管理")
|
||||
@RequestMapping("/api/localStorage")
|
||||
@RequestMapping("/api/localStorage2")
|
||||
public class LocalStorageController {
|
||||
private final LocalStorageService localStorageService;
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package org.nl.system.controller.tools;
|
||||
|
||||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.common.TableDataInfo;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.modules.logging.annotation.Log;
|
||||
import org.nl.modules.tools.domain.LocalStorage;
|
||||
import org.nl.system.service.tools.IToolLocalStorageService;
|
||||
import org.nl.system.service.tools.dao.ToolLocalStorage;
|
||||
import org.nl.system.service.tools.dto.ToolLocalStorageQuery;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 本地存储表 前端控制器
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2022-12-20
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Api(tags = "工具:本地存储管理")
|
||||
@RequestMapping("/api/localStorage")
|
||||
public class ToolLocalStorageController {
|
||||
private final IToolLocalStorageService localStorageService;
|
||||
|
||||
@ApiOperation("查询文件")
|
||||
@GetMapping
|
||||
// @SaCheckPermission("storage:list")
|
||||
public ResponseEntity<Object> query(ToolLocalStorageQuery criteria, PageQuery pageable) {
|
||||
return new ResponseEntity<>(TableDataInfo.build(localStorageService.queryAll(criteria, pageable)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@ApiOperation("上传文件")
|
||||
@PostMapping
|
||||
@SaIgnore
|
||||
// @SaCheckPermission("storage:add")
|
||||
public ResponseEntity<Object> create(@RequestParam String name, @RequestParam("file") MultipartFile file) {
|
||||
return new ResponseEntity<>(localStorageService.create(name, file), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@PostMapping("/pictures")
|
||||
@ApiOperation("上传图片")
|
||||
public ResponseEntity<Object> upload(@RequestParam MultipartFile file) {
|
||||
// 判断文件是否为图片
|
||||
String suffix = FileUtil.getExtensionName(file.getOriginalFilename());
|
||||
if (!FileUtil.IMAGE.equals(FileUtil.getFileType(suffix))) {
|
||||
throw new BadRequestException("只能上传图片");
|
||||
}
|
||||
return new ResponseEntity<>(localStorageService.create(null, file), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Log("修改文件")
|
||||
@ApiOperation("修改文件")
|
||||
@PutMapping
|
||||
@SaCheckPermission("storage:edit")
|
||||
public ResponseEntity<Object> update(@Validated @RequestBody ToolLocalStorage resources) {
|
||||
localStorageService.update(resources);
|
||||
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||
}
|
||||
|
||||
@Log("删除文件")
|
||||
@DeleteMapping
|
||||
@ApiOperation("多选删除")
|
||||
public ResponseEntity<Object> delete(@RequestBody Set<String> ids) {
|
||||
localStorageService.deleteAll(ids);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.nl.system.service.tools;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.system.service.tools.dao.ToolLocalStorage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import org.nl.system.service.tools.dto.ToolLocalStorageQuery;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 本地存储表 服务类
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2022-12-20
|
||||
*/
|
||||
public interface IToolLocalStorageService extends IService<ToolLocalStorage> {
|
||||
|
||||
IPage<ToolLocalStorage> queryAll(ToolLocalStorageQuery criteria, PageQuery pageable);
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
* @param name
|
||||
* @param file
|
||||
* @return
|
||||
*/
|
||||
Object create(String name, MultipartFile file);
|
||||
|
||||
/**
|
||||
* 修改文件
|
||||
* @param resources
|
||||
*/
|
||||
void update(ToolLocalStorage resources);
|
||||
|
||||
/**
|
||||
* 删除文件
|
||||
* @param ids
|
||||
*/
|
||||
void deleteAll(Set<String> ids);
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package org.nl.system.service.tools.dao;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 本地存储表
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2022-12-20
|
||||
*/
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@TableName("tool_local_storage")
|
||||
public class ToolLocalStorage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 标识
|
||||
*/
|
||||
@TableId(value = "storage_id")
|
||||
private String storageId;
|
||||
|
||||
/**
|
||||
* 文件真实的名称
|
||||
*/
|
||||
private String realName;
|
||||
|
||||
/**
|
||||
* 文件名
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 后缀
|
||||
*/
|
||||
private String suffix;
|
||||
|
||||
/**
|
||||
* 路径
|
||||
*/
|
||||
private String path;
|
||||
|
||||
/**
|
||||
* 类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 大小
|
||||
*/
|
||||
private String size;
|
||||
|
||||
/**
|
||||
* 创建人标识
|
||||
*/
|
||||
private String createId;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
*/
|
||||
private String createName;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private String createTime;
|
||||
|
||||
/**
|
||||
* 修改人标识
|
||||
*/
|
||||
private String updateId;
|
||||
|
||||
/**
|
||||
* 修改人
|
||||
*/
|
||||
private String updateName;
|
||||
|
||||
/**
|
||||
* 修改时间
|
||||
*/
|
||||
private String updateTime;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.nl.system.service.tools.dao.mapper;
|
||||
|
||||
import org.nl.system.service.tools.dao.ToolLocalStorage;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 本地存储表 Mapper 接口
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2022-12-20
|
||||
*/
|
||||
public interface ToolLocalStorageMapper extends BaseMapper<ToolLocalStorage> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="org.nl.system.service.tools.dao.mapper.ToolLocalStorageMapper">
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.nl.system.service.tools.dto;
|
||||
|
||||
import lombok.Data;
|
||||
import org.nl.common.domain.query.BaseQuery;
|
||||
import org.nl.system.service.tools.dao.ToolLocalStorage;
|
||||
|
||||
/**
|
||||
* @Author: lyd
|
||||
* @Description:
|
||||
* @Date: 2022/12/20
|
||||
*/
|
||||
@Data
|
||||
public class ToolLocalStorageQuery extends BaseQuery<ToolLocalStorage> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package org.nl.system.service.tools.impl;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.nl.common.domain.query.PageQuery;
|
||||
import org.nl.modules.common.config.FileProperties;
|
||||
import org.nl.modules.common.exception.BadRequestException;
|
||||
import org.nl.modules.common.utils.FileUtil;
|
||||
import org.nl.modules.common.utils.SecurityUtils;
|
||||
import org.nl.system.service.tools.dao.ToolLocalStorage;
|
||||
import org.nl.system.service.tools.dao.mapper.ToolLocalStorageMapper;
|
||||
import org.nl.system.service.tools.IToolLocalStorageService;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.nl.system.service.tools.dto.ToolLocalStorageQuery;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Set;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 本地存储表 服务实现类
|
||||
* </p>
|
||||
*
|
||||
* @author generator
|
||||
* @since 2022-12-20
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class ToolLocalStorageServiceImpl extends ServiceImpl<ToolLocalStorageMapper, ToolLocalStorage> implements IToolLocalStorageService {
|
||||
|
||||
private final FileProperties properties;
|
||||
private final ToolLocalStorageMapper localStorageMapper;
|
||||
|
||||
@Override
|
||||
public IPage<ToolLocalStorage> queryAll(ToolLocalStorageQuery criteria, PageQuery pageable) {
|
||||
return this.page(pageable.build(ToolLocalStorage.class), criteria.build());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Object create(String name, MultipartFile multipartFile) {
|
||||
FileUtil.checkSize(properties.getMaxSize(), multipartFile.getSize());
|
||||
String suffix = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
|
||||
String type = FileUtil.getFileType(suffix);
|
||||
File file = FileUtil.upload(multipartFile, properties.getPath().getPath() + type + File.separator);
|
||||
if(ObjectUtil.isNull(file)){
|
||||
throw new BadRequestException("上传失败");
|
||||
}
|
||||
try {
|
||||
String userId = SecurityUtils.getCurrentUserId();
|
||||
String nickName = SecurityUtils.getCurrentNickName();
|
||||
String now = DateUtil.now();
|
||||
name = StrUtil.isEmpty(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
|
||||
ToolLocalStorage localStorage = new ToolLocalStorage();
|
||||
localStorage.setStorageId(IdUtil.getSnowflake(1,1).nextIdStr());
|
||||
localStorage.setRealName(file.getName());
|
||||
localStorage.setName(name);
|
||||
localStorage.setSuffix(suffix);
|
||||
localStorage.setPath(file.getPath());
|
||||
localStorage.setType(type);
|
||||
localStorage.setSize(FileUtil.getSize(multipartFile.getSize()));
|
||||
localStorage.setCreateId(userId);
|
||||
localStorage.setCreateName(nickName);
|
||||
localStorage.setCreateTime(now);
|
||||
localStorage.setUpdateId(userId);
|
||||
localStorage.setUpdateName(nickName);
|
||||
localStorage.setUpdateTime(now);
|
||||
localStorageMapper.insert(localStorage);
|
||||
return localStorage;
|
||||
}catch (Exception e){
|
||||
FileUtil.del(file);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(ToolLocalStorage resources) {
|
||||
ToolLocalStorage storage = localStorageMapper.selectById(resources.getStorageId());
|
||||
if (ObjectUtil.isEmpty(storage)) throw new BadRequestException("文件信息不存在");
|
||||
resources.setUpdateId(SecurityUtils.getCurrentUserId());
|
||||
resources.setUpdateName(SecurityUtils.getCurrentNickName());
|
||||
resources.setUpdateTime(DateUtil.now());
|
||||
localStorageMapper.updateById(resources);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteAll(Set<String> ids) {
|
||||
ids.forEach(id -> {
|
||||
ToolLocalStorage localStorage = localStorageMapper.selectById(id);
|
||||
if (ObjectUtil.isNotEmpty(localStorage)) {
|
||||
FileUtil.del(localStorage.getPath());
|
||||
localStorageMapper.deleteById(localStorage);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user