文件存储

This commit is contained in:
2022-12-06 14:53:22 +08:00
parent 8f2f400eab
commit 16ac69ace9
16 changed files with 418 additions and 22 deletions

View File

@@ -41,7 +41,6 @@ public class ConfigurerAdapter implements WebMvcConfigurer {
this.properties = properties;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
FileProperties.ElPath path = properties.getPath();

View File

@@ -2,6 +2,7 @@ package org.nl.config.saconfig;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
@@ -23,15 +24,14 @@ public class CorsFilter implements Filter {
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
// 允许指定域访问跨域资源
response.setHeader("Access-Control-Allow-Origin", "*");
// 允许所有请求方式
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Allow-Methods", "*");
// 有效时间
response.setHeader("Access-Control-Max-Age", "3600");
// 允许的header参数
response.setHeader("Access-Control-Allow-Headers", "x-requested-with,satoken");
response.setHeader("Access-Control-Allow-Headers", "*");
response.setHeader("Access-Control-Allow-Credentials", "true");
// 如果是预检请求,直接返回

View File

@@ -51,5 +51,5 @@ public class GenConfig implements Serializable {
private String prefix;
@ApiModelProperty(value = "是否覆盖")
private Boolean cover = false;
private String cover = "0";
}

View File

@@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.nl.modules.common.generator.domain.GenConfig;
import org.nl.modules.common.generator.service.GenConfigService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

View File

@@ -1,7 +1,6 @@
package org.nl.modules.common.generator.service;
import com.alibaba.fastjson.JSONObject;
import org.nl.modules.common.generator.domain.GenConfig;
/**
* @author: lyd

View File

@@ -2,7 +2,6 @@ package org.nl.modules.common.generator.service.impl;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.nl.modules.common.generator.domain.GenConfig;

View File

@@ -160,6 +160,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
* @param quartzJob /
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void updateIsPause(JSONObject quartzJob) {
WQLObject jobTab = WQLObject.getWQLObject("sys_quartz_job");
if (quartzJob.getString("is_pause").equals("1")) {
@@ -207,6 +208,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
* @param ids
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void delete(Set<Long> ids) {
WQLObject jobTab = WQLObject.getWQLObject("sys_quartz_job");
WQLObject logTab = WQLObject.getWQLObject("sys_quartz_log");

View File

@@ -0,0 +1,34 @@
package org.nl.modules.tools.config;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.MultipartConfigElement;
import java.io.File;
/**
* @author: lyd
* @description:
* @Date: 2022/12/6
*/
@Configuration
public class MultipartConfig {
/**
* 文件上传临时路径
*/
@Bean
MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
String location = System.getProperty("user.home") + "/.eladmin/file/tmp";
File tmpFile = new File(location);
if (!tmpFile.exists()) {
if (!tmpFile.mkdirs()) {
System.out.println("create was not successful.");
}
}
factory.setLocation(location);
return factory.createMultipartConfig();
}
}

View File

@@ -0,0 +1,78 @@
package org.nl.modules.tools.domain;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import org.nl.modules.common.utils.SecurityUtils;
import java.io.Serializable;
/**
* @author: lyd
* @description:
* @Date: 2022/12/6
*/
@Data
public class LocalStorage implements Serializable {
/** 标识 */
/** 防止精度丢失 */
@JsonSerialize(using= ToStringSerializer.class)
private Long storage_id;
/** 文件真实的名称 */
private String real_name;
/** 文件名 */
private String name;
/** 后缀 */
private String suffix;
/** 路径 */
private String path;
/** 类型 */
private String type;
/** 大小 */
private String size;
/** 创建人标识 */
private String create_id;
/** 创建人 */
private String create_name;
/** 创建时间 */
private String create_time;
/** 修改人标识 */
private String update_optid;
/** 修改人 */
private String update_optname;
/** 修改时间 */
private String update_time;
public LocalStorage(String realName,String name, String suffix, String path, String type, String size) {
this.storage_id = IdUtil.getSnowflake(1,1).nextId();
this.real_name = realName;
this.name = name;
this.suffix = suffix;
this.path = path;
this.type = type;
this.size = size;
this.create_id = this.update_optid = SecurityUtils.getCurrentUserId().toString();
this.create_name = this.update_optname = SecurityUtils.getCurrentNickName();
this.create_time = this.update_time = DateUtil.now();
}
public void copy(LocalStorage source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
}
}

View File

@@ -0,0 +1,77 @@
package org.nl.modules.tools.rest;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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.modules.tools.service.LocalStorageService;
import org.springframework.data.domain.Pageable;
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.Map;
/**
* @author: lyd
* @description: 本地存储管理
* @Date: 2022/12/5
*/
@RestController
@RequiredArgsConstructor
@Api(tags = "工具:本地存储管理")
@RequestMapping("/api/localStorage")
public class LocalStorageController {
private final LocalStorageService localStorageService;
@ApiOperation("查询文件")
@GetMapping
// @SaCheckPermission("storage:list")
public ResponseEntity<Object> query(@RequestParam Map criteria, Pageable pageable) {
return new ResponseEntity<>(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 LocalStorage resources) {
localStorageService.update(resources);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@Log("删除文件")
@DeleteMapping
@ApiOperation("多选删除")
public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
localStorageService.deleteAll(ids);
return new ResponseEntity<>(HttpStatus.OK);
}
}

View File

@@ -0,0 +1,51 @@
package org.nl.modules.tools.service;
import com.alibaba.fastjson.JSONObject;
import org.nl.modules.tools.domain.LocalStorage;
import org.springframework.data.domain.Pageable;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
/**
* @author: lyd
* @description:
* @Date: 2022/12/5
*/
public interface LocalStorageService {
/**
*
* @param criteria
* @param pageable
* @return
*/
Object queryAll(Map criteria, Pageable pageable);
/**
* 上传
* @param name 文件名称
* @param file 文件
* @return
*/
JSONObject create(String name, MultipartFile multipartFile);
/**
* 编辑
* @param resources 文件信息
*/
void update(LocalStorage resources);
/**
* 通过id找文件信息
* @param id
* @return
*/
JSONObject findById(String id);
/**
* 多选删除
* @param ids /
*/
void deleteAll(Long[] ids);
}

View File

@@ -0,0 +1,154 @@
package org.nl.modules.tools.service.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.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
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.modules.common.utils.ValidationUtil;
import org.nl.modules.tools.domain.LocalStorage;
import org.nl.modules.tools.service.LocalStorageService;
import org.nl.modules.wql.core.bean.ResultBean;
import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;
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.Map;
/**
* @author: lyd
* @description:
* @Date: 2022/12/5
*/
@Service
@RequiredArgsConstructor
public class LocalStorageServiceImpl implements LocalStorageService {
private final FileProperties properties;
/**
* @param criteria
* @param pageable
* @return
*/
@Override
public Object queryAll(Map criteria, Pageable pageable) {
WQLObject toolTab = WQLObject.getWQLObject("tool_local_storage");
String param = "1=1";
if (ObjectUtil.isNotEmpty(criteria.get("blurry"))) {
param = param + " AND name = '%" + criteria.get("blurry").toString() + "%'";
}
ResultBean rb = toolTab.pagequery(WqlUtil.getHttpContext(pageable), param, "create_time desc");
final JSONObject json = rb.pageResult();
return json;
}
/**
* 上传
*
* @param name 文件名称
* @param multipartFile 文件
* @return
*/
@Override
@Transactional(rollbackFor = Exception.class)
public JSONObject create(String name, MultipartFile multipartFile) {
WQLObject toolTab = WQLObject.getWQLObject("tool_local_storage");
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 {
Long userId = SecurityUtils.getCurrentUserId();
String nickName = SecurityUtils.getCurrentNickName();
String now = DateUtil.now();
name = StrUtil.isEmpty(name) ? FileUtil.getFileNameNoEx(multipartFile.getOriginalFilename()) : name;
JSONObject localStorage = new JSONObject();
localStorage.put("storage_id", IdUtil.getSnowflake(1,1).nextId());
localStorage.put("real_name", file.getName());
localStorage.put("name", name);
localStorage.put("suffix", suffix);
localStorage.put("path", file.getPath());
localStorage.put("type", type);
localStorage.put("size", FileUtil.getSize(multipartFile.getSize()));
localStorage.put("create_id", userId);
localStorage.put("update_optid", userId);
localStorage.put("create_name", nickName);
localStorage.put("update_optname", nickName);
localStorage.put("create_time", now);
localStorage.put("update_time", now);
// LocalStorage localStorage = new LocalStorage(
// file.getName(),
// name,
// suffix,
// file.getPath(),
// type,
// FileUtil.getSize(multipartFile.getSize())
// );
// JSONObject json = JSONObject.parseObject(JSON.toJSONString(localStorage));
toolTab.insert(localStorage);
return localStorage;
}catch (Exception e){
FileUtil.del(file);
throw e;
}
}
/**
* 编辑
*
* @param resources 文件信息
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void update(LocalStorage resources) {
JSONObject entity = this.findById(resources.getCreate_id());
if (ObjectUtil.isEmpty(entity)) throw new BadRequestException("文件信息不存在");
WQLObject toolTab = WQLObject.getWQLObject("tool_local_storage");
JSONObject json = JSONObject.parseObject(JSON.toJSONString(resources));
toolTab.update(json);
}
/**
* 通过id找文件信息
*
* @param id
* @return
*/
@Override
public JSONObject findById(String id) {
WQLObject toolTab = WQLObject.getWQLObject("tool_local_storage");
JSONObject object = toolTab.query("storage_id = '" + id + "'").uniqueResult(0);
return object;
}
/**
* 多选删除
*
* @param ids /
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteAll(Long[] ids) {
WQLObject toolTab = WQLObject.getWQLObject("tool_local_storage");
for (Long id : ids) {
JSONObject storage = this.findById(id.toString());
if (ObjectUtil.isEmpty(storage)) continue;
FileUtil.del(storage.getString("path"));
toolTab.delete(storage);
}
}
}