✨ 添加新特性。文件管理

This commit is contained in:
huangxutao committed 2019-06-18 18:54:03 +08:00
1 parent 1b3bdd0197
commit 7b2609408d
8 files changed
+573 -2

No files matched your search

@@ -0,0 +1,112 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.admin.api.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.time.LocalDateTime;
/**
* 文件管理
*
* @author Luckly
* @date 2019-06-18 17:18:42
*/
@Data
@TableName("sys_file")
@EqualsAndHashCode(callSuper = true)
@ApiModel(description = "文件管理")
public class SysFile extends Model<SysFile> {
private static final long serialVersionUID = 1L;
/**
* 编号
*/
@TableId
private String id;
/**
* 文件名
*/
@TableField("`file_name`")
private String name;
/**
* 保存路径
*/
private String path;
/**
* 绝对路径
*/
private String absolutePath;
/**
* 上传服务器前缀地址
*/
private String prefix;
/**
* 原文件名
*/
private String original;
/**
* MD5值
*/
private String md5;
/**
* 后缀名
*/
private String suffix;
/**
* 文件类型
*/
private String type;
/**
* 文件大小
*/
@TableField("`file_size`")
private Long fileSize;
/**
* 显示大小
*/
private String displaySize;
/**
* 上传人
*/
private String createUser;
/**
* 上传时间
*/
private LocalDateTime createTime;
/**
* 更新人
*/
private String updateUser;
/**
* 更新时间
*/
private LocalDateTime updateTime;
/**
* 删除标识:1-删除,0-正常
*/
@TableLogic
private Integer delFlag;
}
@@ -22,15 +22,21 @@ import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.pig4cloud.common.minio.service.MinioTemplate;
import com.pig4cloud.pigx.admin.api.entity.SysFile;
import com.pig4cloud.pigx.admin.service.SysFileService;
import com.pig4cloud.pigx.admin.util.FileKit;
import com.pig4cloud.pigx.common.core.constant.CommonConstants;
import com.pig4cloud.pigx.common.core.util.R;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
@@ -50,7 +56,7 @@ import java.util.Map;
@Api(value = "file", tags = "文件管理模块")
public class FileController {
private final MinioTemplate minioTemplate;
private final SysFileService sysFileService;
/**
* 上传文件
* 文件名采用uuid,避免原始文件名中带"-"符号导致下载的时候解析出现异常
@@ -59,7 +65,7 @@ public class FileController {
* @return R(bucketName, filename)
*/
@PostMapping("/upload")
public R upload(@RequestParam("file") MultipartFile file) {
public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String fileName = IdUtil.simpleUUID() + StrUtil.DOT + FileUtil.extName(file.getOriginalFilename());
Map<String, String> resultMap = new HashMap<>(4);
resultMap.put("bucketName", CommonConstants.BUCKET_NAME);
@@ -67,6 +73,10 @@ public class FileController {
try {
minioTemplate.putObject(CommonConstants.BUCKET_NAME, fileName, file.getInputStream());
String tenantId = request.getHeader(CommonConstants.TENANT_ID);
log.debug("获取header中的租户ID为:{}", tenantId);
//文件管理数据记录,收集管理追踪文件
fileLog(file, fileName,tenantId);
} catch (Exception e) {
log.error("上传失败", e);
return R.failed(e.getLocalizedMessage());
@@ -74,6 +84,45 @@ public class FileController {
return R.ok(resultMap);
}
/**
* 文件管理数据记录,收集管理追踪文件
*
* @param file 上传文件格式
* @param fileName 文件名
*/
private void fileLog(MultipartFile file, String fileName, String tenantId) throws IOException {
SysFile sysFile = new SysFile();
//存储文件名
String name = CommonConstants.BUCKET_NAME + StrUtil.DASHED + fileName;
//原文件名
String original = file.getOriginalFilename();
//文件大小
Long size = file.getSize();
//后缀名
String suffix = FileKit.getSuffix(name);
//文件类型
String type = FileKit.getType(name);
//显示大小
String displaySize = FileKit.getDisplaySize(file.getSize());
sysFile.setName(name);
sysFile.setOriginal(original);
sysFile.setFileSize(size);
sysFile.setSuffix(suffix);
sysFile.setType(type);
sysFile.setDisplaySize(displaySize);
sysFile.setMd5(DigestUtils.md5Hex(file.getBytes()));
// if (StrUtil.isNotBlank(tenantId)) {
// sysFile.setTenantId(Integer.parseInt(tenantId));
// } else {
// sysFile.setTenantId(CommonConstants.TENANT_ID_1);
// }
sysFileService.save(sysFile);
}
/**
* 获取文件
*
@@ -0,0 +1,110 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.admin.controller;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.common.log.annotation.SysLog;
import com.pig4cloud.pigx.admin.api.entity.SysFile;
import com.pig4cloud.pigx.admin.service.SysFileService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
/**
* 文件管理
*
* @author Luckly
* @date 2019-06-18 17:18:42
*/
@RestController
@AllArgsConstructor
@RequestMapping("/sysfile" )
@Api(value = "sysfile", tags = "文件管理")
public class SysFileController {
private final SysFileService sysFileService;
/**
* 分页查询
* @param page 分页对象
* @param sysFile 文件管理
* @return
*/
@ApiOperation(value = "分页查询", notes = "分页查询")
@GetMapping("/page" )
public R getSysFilePage(Page page, SysFile sysFile) {
return R.ok(sysFileService.page(page, Wrappers.query(sysFile)));
}
/**
* 通过id查询文件管理
* @param id id
* @return R
*/
@ApiOperation(value = "通过id查询文件管理", notes = "通过id查询文件管理")
@GetMapping("/{id}" )
public R getById(@PathVariable("id" ) String id) {
return R.ok(sysFileService.getById(id));
}
/**
* 新增文件管理
* @param sysFile 文件管理
* @return R
*/
@ApiOperation(value = "新增文件管理", notes = "新增文件管理")
@SysLog("新增文件管理" )
@PostMapping
@PreAuthorize("@pms.hasPermission('admin_sysfile_add')" )
public R save(@RequestBody SysFile sysFile) {
return R.ok(sysFileService.save(sysFile));
}
/**
* 修改文件管理
* @param sysFile 文件管理
* @return R
*/
@ApiOperation(value = "修改文件管理", notes = "修改文件管理")
@SysLog("修改文件管理" )
@PutMapping
@PreAuthorize("@pms.hasPermission('admin_sysfile_edit')" )
public R updateById(@RequestBody SysFile sysFile) {
return R.ok(sysFileService.updateById(sysFile));
}
/**
* 通过id删除文件管理
* @param id id
* @return R
*/
@SysLog("删除文件管理" )
@DeleteMapping("/{id}" )
@PreAuthorize("@pms.hasPermission('admin_sysfile_del')" )
public R removeById(@PathVariable String id) {
return R.ok(sysFileService.removeById(id));
}
}
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.admin.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.pig4cloud.pigx.admin.api.entity.SysFile;
/**
* 文件管理
*
* @author Luckly
* @date 2019-06-18 17:18:42
*/
public interface SysFileMapper extends BaseMapper<SysFile> {
}
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.admin.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.pig4cloud.pigx.admin.api.entity.SysFile;
/**
* 文件管理
*
* @author Luckly
* @date 2019-06-18 17:18:42
*/
public interface SysFileService extends IService<SysFile> {
}
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2018-2025, lengleng All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of the pig4cloud.com developer nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
* Author: lengleng (wangiegie@gmail.com)
*/
package com.pig4cloud.pigx.admin.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.pigx.admin.api.entity.SysFile;
import com.pig4cloud.pigx.admin.mapper.SysFileMapper;
import com.pig4cloud.pigx.admin.service.SysFileService;
import org.springframework.stereotype.Service;
/**
* 文件管理
*
* @author Luckly
* @date 2019-06-18 17:18:42
*/
@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
}
@@ -0,0 +1,160 @@
package com.pig4cloud.pigx.admin.util;
import java.io.File;
import java.math.BigDecimal;
/**
* @author: Luckly
* @date: 2019-06-18
* @description: 文件套件
*/
public class FileKit {
private final static String[] TYPE_IMAGE = new String[]{".png", ".jpg", ".jpeg", ".gif", ".bmp"};
private final static String[] TYPE_VIDEO = new String[]{".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg", ".ogv", ".mov", ".wmv", ".mp4", ".webm"};
private final static String[] TYPE_AUDIO = new String[]{".mp3", ".wav", ".mid", ".ogg"};
private final static String[] TYPE_DOC = new String[]{".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf"};
/**
* 判断文件大小
*
* @param len 文件长度
* @param size 限制大小
* @param unit 限制单位(B,K,M,G)
* @return
*/
public static boolean checkFileSize(Long len, int size, String unit) {
double fileSize = 0;
if ("B".equals(unit.toUpperCase())) {
fileSize = (double) len;
} else if ("K".equals(unit.toUpperCase())) {
fileSize = (double) len / 1024;
} else if ("M".equals(unit.toUpperCase())) {
fileSize = (double) len / 1048576;
} else if ("G".equals(unit.toUpperCase())) {
fileSize = (double) len / 1073741824;
} else {
fileSize = (double) len;
}
if (fileSize > size) {
return false;
}
return true;
}
/**
* 转换文件大小为友好显示
*
* @param size
* @return
*/
public static String getDisplaySize(long size) {
String displaySize = size + "";
if (size < 1024) {
displaySize = size + "B";
} else if (size < 1024 * 1024) {
displaySize = getDisplaySizeKB(size);
} else if (size < 1024 * 1024 * 1024) {
displaySize = getDisplaySizeMB(size);
} else if (size < 1024.0 * 1024 * 1024 * 1024) {
displaySize = getDisplaySizeGB(size);
} else if (size < 1024.0 * 1024 * 1024 * 1024 * 1024) {
displaySize = getDisplaySizeTB(size);
}
return displaySize;
}
/**
* 转换文件大小为友好显示
*
* @param size
* @return
*/
public static String getDisplaySizeKB(long size) {
return new BigDecimal(size).divide(new BigDecimal(1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "KB";
}
/**
* 转换文件大小为友好显示
*
* @param size
* @return
*/
public static String getDisplaySizeMB(long size) {
return new BigDecimal(size).divide(new BigDecimal(1024 * 1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "MB";
}
/**
* 转换文件大小为友好显示
*
* @param size
* @return
*/
public static String getDisplaySizeGB(long size) {
return new BigDecimal(size).divide(new BigDecimal(1024 * 1024 * 1024.0)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "GB";
}
/**
* 转换文件大小为友好显示
*
* @param size
* @return
*/
public static String getDisplaySizeTB(long size) {
return new BigDecimal(size).divide(new BigDecimal(1024.0 * 1024 * 1024 * 1024)).setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue() + "TB";
}
/**
* 获取文件类型
*
* @param path
* @return
*/
public static String getType(String path) {
String type = "file";
String suffix = getSuffix(path);
if (eqSuffix(suffix, TYPE_IMAGE))
type = "image";
else if (eqSuffix(suffix, TYPE_VIDEO))
type = "video";
else if (eqSuffix(suffix, TYPE_AUDIO))
type = "audio";
else if (eqSuffix(suffix, TYPE_DOC))
type = "doc";
return type;
}
/**
* 获取文件后缀名
*
* @param path 文件绝对路径
* @return
*/
public static String getSuffix(String path) {
int idx = path.lastIndexOf(".");
if (idx < 0)
return "";
return path.substring(path.lastIndexOf(".")).toLowerCase();
}
private static boolean eqSuffix(String suffix, String[] types) {
for (String type : types) {
if (type.equals(suffix))
return true;
}
return false;
}
/**
* 获取文件后缀名
*
* @param file 文件
* @return
*/
public static String getSuffix(File file) {
return getSuffix(file.getName());
}
}
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~
~ Copyright (c) 2018-2025, lengleng All rights reserved.
~
~ Redistribution and use in source and binary forms, with or without
~ modification, are permitted provided that the following conditions are met:
~
~ Redistributions of source code must retain the above copyright notice,
~ this list of conditions and the following disclaimer.
~ Redistributions in binary form must reproduce the above copyright
~ notice, this list of conditions and the following disclaimer in the
~ documentation and/or other materials provided with the distribution.
~ Neither the name of the pig4cloud.com developer nor the names of its
~ contributors may be used to endorse or promote products derived from
~ this software without specific prior written permission.
~ Author: lengleng (wangiegie@gmail.com)
~
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.pig4cloud.pigx.admin.mapper.SysFileMapper">
<resultMap id="sysFileMap" type="com.pig4cloud.pigx.admin.api.entity.SysFile">
<id property="id" column="id"/>
<result property="name" column="file_name"/>
<result property="path" column="path"/>
<result property="absolutePath" column="absolute_path"/>
<result property="prefix" column="prefix"/>
<result property="original" column="original"/>
<result property="md5" column="md5"/>
<result property="suffix" column="suffix"/>
<result property="type" column="type"/>
<result property="fileSize" column="file_size"/>
<result property="displaySize" column="display_size"/>
<result property="createUser" column="create_user"/>
<result property="createTime" column="create_time"/>
<result property="updateUser" column="update_user"/>
<result property="updateTime" column="update_time"/>
<result property="delFlag" column="del_flag"/>
</resultMap>
</mapper>