文件存储

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);
}
}
}

View File

@@ -46,7 +46,8 @@
<el-checkbox
v-model="data[scope.$index].not_null"
true-label="1"
false-label="0" />
false-label="0"
/>
</template>
</el-table-column>
<el-table-column align="center" label="列表" width="70px">
@@ -54,7 +55,8 @@
<el-checkbox
v-model="data[scope.$index].list_show"
true-label="1"
false-label="0" />
false-label="0"
/>
</template>
</el-table-column>
<el-table-column align="center" label="表单" width="70px">
@@ -62,7 +64,8 @@
<el-checkbox
v-model="data[scope.$index].form_show"
true-label="1"
false-label="0" />
false-label="0"
/>
</template>
</el-table-column>
<el-table-column label="表单类型">
@@ -247,6 +250,7 @@ export default {
this.$nextTick(() => {
this.init()
get(this.tableName).then(data => {
console.log(data)
this.form = data
this.form.cover = this.form.cover.toString()
})

View File

@@ -100,8 +100,8 @@
<template slot-scope="scope">
<el-button v-permission="['admin','timing:edit']" size="mini" style="margin-right: 3px;" type="text" @click="crud.toEdit(scope.row)">编辑</el-button>
<el-button v-permission="['admin','timing:edit']" style="margin-left: -2px" type="text" size="mini" @click="execute(scope.row.job_id)">执行</el-button>
<el-button v-permission="['admin','timing:edit']" style="margin-left: 3px" type="text" size="mini" @click="updateStatus(scope.row.job_id,scope.row.is_pause ? '恢复' : '暂停')">
{{ scope.row.is_pause ? '恢复' : '暂停' }}
<el-button v-permission="['admin','timing:edit']" style="margin-left: 3px" type="text" size="mini" @click="updateStatus(scope.row.job_id,scope.row.is_pause=='1' ? '恢复' : '暂停')">
{{ scope.row.is_pause=='1' ? '恢复' : '暂停' }}
</el-button>
<el-popover
:ref="scope.row.job_id"

View File

@@ -57,7 +57,7 @@
<el-table-column prop="name" label="文件名">
<template slot-scope="scope">
<el-popover
:content="'file/' + scope.row.type + '/' + scope.row.realName"
:content="'file/' + scope.row.type + '/' + scope.row.real_name"
placement="top-start"
title="路径"
width="200"
@@ -65,7 +65,7 @@
>
<a
slot="reference"
:href="baseApi + '/file/' + scope.row.type + '/' + scope.row.realName"
:href="baseApi + '/file/' + scope.row.type + '/' + scope.row.real_name"
class="el-link--primary"
style="word-break:keep-all;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;color: #1890ff;font-size: 13px;"
target="_blank"
@@ -78,8 +78,8 @@
<el-table-column prop="path" label="预览图">
<template slot-scope="{row}">
<el-image
:src=" baseApi + '/file/' + row.type + '/' + row.realName"
:preview-src-list="[baseApi + '/file/' + row.type + '/' + row.realName]"
:src=" baseApi + '/file/' + row.type + '/' + row.real_name"
:preview-src-list="[baseApi + '/file/' + row.type + '/' + row.real_name]"
fit="contain"
lazy
class="el-avatar"
@@ -93,10 +93,10 @@
<el-table-column prop="suffix" label="文件类型" />
<el-table-column prop="type" label="类别" />
<el-table-column prop="size" label="大小" />
<el-table-column prop="operate" label="操作人" />
<el-table-column prop="createTime" label="创建日期">
<el-table-column prop="create_name" label="操作人" />
<el-table-column prop="create_time" label="创建日期">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.createTime) }}</span>
<span>{{ parseTime(scope.row.create_time) }}</span>
</template>
</el-table-column>
</el-table>
@@ -115,11 +115,11 @@ import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'
const defaultForm = { id: null, name: '' }
const defaultForm = { storage_id: null, name: '' }
export default {
components: { pagination, crudOperation, rrOperation, DateRangePicker },
cruds() {
return CRUD({ title: '文件', url: 'api/localStorage', crudMethod: { ...crudFile }})
return CRUD({ title: '文件', idField: 'storage_id', url: 'api/localStorage', crudMethod: { ...crudFile }})
},
mixins: [presenter(), header(), form(defaultForm), crud()],
data() {