🎨 文件上传记录功能扩展

🎨 文件上传记录功能扩展
This commit is contained in:
ronger 2022-01-14 12:05:59 +08:00 committed by GitHub
commit a565ed19c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 114 additions and 76 deletions

View File

@ -23,6 +23,17 @@ public class ForestFile {
@Column(name = "id")
private Long id;
/**
* 文件大小
*/
@Column(name = "file_size")
private long fileSize;
/**
* 文件类型-文件后缀
*/
@Column(name = "file_type")
private String fileType;
/**
* 访问路径
*/

View File

@ -13,10 +13,12 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
/**
* 通过md5获取文件访问链接
*
* @param md5Value md5值
* @return
* @param md5Value md5值
* @param createdBy 创建人
* @param createdBy 创建人
* @param fileType 文件类型
*/
String getFileUrlByMd5(@Param("md5Value") String md5Value);
String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileType") String fileType);
/**
* 插入文件对象
@ -25,8 +27,10 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy);
int insertForestFile(@Param("fileUrl") String fileUrl, @Param("filePath") String filePath, @Param("md5Value") String md5Value, @Param("createdBy") long createdBy, @Param("fileSize") long fileSize, @Param("fileType") String fileType);
}

View File

@ -15,10 +15,12 @@ public interface ForestFileService extends Service<ForestFile> {
/**
* 通过md5获取文件访问链接
*
* @param md5Value md5值
* @param md5Value md5值
* @param createdBy 用户id
* @param fileType 文件类型
* @return
*/
String getFileUrlByMd5(String md5Value);
String getFileUrlByMd5(String md5Value, long createdBy, String fileType);
/**
* 插入文件对象
@ -27,7 +29,9 @@ public interface ForestFileService extends Service<ForestFile> {
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy);
int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType);
}

View File

@ -22,12 +22,14 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
/**
* 通过md5获取文件访问链接
*
* @param md5Value md5值
* @param md5Value md5值
* @param createdBy 用户id
* @param fileType 文件类型
* @return
*/
@Override
public String getFileUrlByMd5(String md5Value) {
return forestFileMapper.getFileUrlByMd5(md5Value);
public String getFileUrlByMd5(String md5Value, long createdBy, String fileType) {
return forestFileMapper.getFileUrlByMd5(md5Value, createdBy, fileType);
}
/**
@ -37,10 +39,12 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
* @param filePath 上传路径
* @param md5Value md5值
* @param createdBy 创建人
* @param fileSize 文件大小
* @param fileType 文件类型
* @return
*/
@Override
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy) {
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy);
public int insertForestFile(String fileUrl, String filePath, String md5Value, long createdBy, long fileSize, String fileType) {
return forestFileMapper.insertForestFile(fileUrl, filePath, md5Value, createdBy, fileSize, fileType);
}
}

View File

@ -18,7 +18,7 @@ public class FileUtils {
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
/**
* 获取文件扩展名
* 获取文件扩展名,.
*
* @param filename
* @return
@ -31,6 +31,7 @@ public class FileUtils {
* 获取文件扩展名
*
* @param filename
* @param defExt 默认文件后缀名 .
* @return
*/
public static String getExtend(String filename, String defExt) {
@ -41,7 +42,7 @@ public class FileUtils {
int i = filename.lastIndexOf('.');
if ((i > 0) && (i < (filename.length() - 1))) {
String result = filename.substring(i + 1).toLowerCase();
String result = filename.substring(i).toLowerCase();
if (result.contains("?")) {
return result.split("\\?")[0];
}

View File

@ -42,8 +42,7 @@ public class UploadController {
private final static String UPLOAD_URL = "/api/upload/file/batch";
private final static String LINK_TO_IMAGE_URL = "/api/upload/file/link";
private static Environment env = SpringContextHolder.getBean(Environment.class);
private static final Environment env = SpringContextHolder.getBean(Environment.class);
@Resource
private ForestFileService forestFileService;
@ -120,8 +119,15 @@ public class UploadController {
}
TokenUser tokenUser = getTokenUser(request);
Map data = new HashMap(2);
if (multipartFile.getSize() == 0) {
data.put("message", "上传失败!");
return GlobalResultGenerator.genSuccessResult(data);
}
String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
String fileUrl = forestFileService.getFileUrlByMd5(md5);
String orgName = multipartFile.getOriginalFilename();
String fileType = FileUtils.getExtend(orgName);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
if (StringUtils.isNotEmpty(fileUrl)) {
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
@ -137,15 +143,15 @@ public class UploadController {
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
String orgName = multipartFile.getOriginalFilename();
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
String fileName = System.currentTimeMillis() + fileType;
String savePath = file.getPath() + File.separator + fileName;
fileUrl = localPath + fileName;
File saveFile = new File(savePath);
try {
FileCopyUtils.copy(multipartFile.getBytes(), saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
data.put("url", fileUrl);
} catch (IOException e) {
data.put("message", "上传失败!");
@ -174,21 +180,25 @@ public class UploadController {
for (int i = 0, len = multipartFiles.length; i < len; i++) {
MultipartFile multipartFile = multipartFiles[i];
String orgName = multipartFile.getOriginalFilename();
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(orgName).toLowerCase();
if (multipartFile.getSize() == 0) {
errFiles.add(orgName);
continue;
}
String fileType = FileUtils.getExtend(orgName);
String fileName = System.currentTimeMillis() + fileType;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
try (InputStream in = multipartFiles[i].getInputStream();
OutputStream out = Files.newOutputStream(saveFile.toPath())) {
try (InputStream in = multipartFiles[i].getInputStream(); OutputStream out = Files.newOutputStream(saveFile.toPath())) {
String md5 = DigestUtils.md5DigestAsHex(in);
String fileUrl = forestFileService.getFileUrlByMd5(md5);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
if (StringUtils.isNotEmpty(fileUrl)) {
successMap.put(orgName, fileUrl);
continue;
}
fileUrl = localPath + fileName;
FileCopyUtils.copy(in, out);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), multipartFile.getSize(), fileType);
successMap.put(orgName, localPath + fileName);
} catch (IOException e) {
errFiles.add(orgName);
@ -235,12 +245,9 @@ public class UploadController {
@PostMapping("/file/link")
@Transactional(rollbackFor = Exception.class)
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException, BaseApiException {
//todo 无法获取当前登录用户
// User user = UserUtils.getCurrentUserByToken();
// if (Objects.isNull(user)) {
// throw new BaseApiException(ErrorCode.INVALID_TOKEN);
// }
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO, HttpServletRequest request) throws IOException, BaseApiException {
TokenUser tokenUser = getTokenUser(request);
String url = linkToImageUrlDTO.getUrl();
URL link = new URL(url);
HttpURLConnection conn = (HttpURLConnection) link.openConnection();
@ -249,53 +256,57 @@ public class UploadController {
//防止屏蔽程序抓取而返回403错误
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36");
conn.setRequestProperty("referer", "");
//得到输入流
InputStream inputStream = conn.getInputStream();
// 获取文件md5值
String md5 = DigestUtils.md5DigestAsHex(inputStream);
String fileUrl = forestFileService.getFileUrlByMd5(md5);
Map data = new HashMap(2);
data.put("originalURL", url);
//得到输入流
try (InputStream inputStream = conn.getInputStream()) {
//获取自己数组
byte[] getData = readInputStream(inputStream);
if (getData.length == 0) {
data.put("message", "文件为空!");
return GlobalResultGenerator.genSuccessResult(data);
}
if (StringUtils.isNotEmpty(fileUrl)) {
// 获取文件md5值
String md5 = DigestUtils.md5DigestAsHex(inputStream);
String fileType = FileUtils.getExtend(url);
String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser(), fileType);
data.put("originalURL", url);
if (StringUtils.isNotEmpty(fileUrl)) {
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
}
Integer type = linkToImageUrlDTO.getType();
if (Objects.isNull(type)) {
type = 1;
}
String typePath = getTypePath(type);
//图片存储路径
String ctxHeadPicPath = env.getProperty("resource.pic-path");
String dir = ctxHeadPicPath + "/" + typePath;
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String fileName = System.currentTimeMillis() + fileType;
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
FileCopyUtils.copy(getData, saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser(), getData.length, fileType);
data.put("originalURL", url);
data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data);
} catch (IOException e) {
data.put("message", "上传失败");
return GlobalResultGenerator.genSuccessResult(data);
}
Integer type = linkToImageUrlDTO.getType();
if (Objects.isNull(type)) {
type = 1;
}
String typePath = getTypePath(type);
//图片存储路径
String ctxHeadPicPath = env.getProperty("resource.pic-path");
String dir = ctxHeadPicPath + "/" + typePath;
File file = new File(dir);
if (!file.exists()) {
file.mkdirs();// 创建文件根目录
}
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
String savePath = file.getPath() + File.separator + fileName;
File saveFile = new File(savePath);
try {
//获取自己数组
byte[] getData = readInputStream(inputStream);
FileCopyUtils.copy(getData, saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, 1);
data.put("originalURL", url);
data.put("url", fileUrl);
} catch (IOException e) {
data.put("message", "上传失败!");
}
return GlobalResultGenerator.genSuccessResult(data);
}

View File

@ -3,6 +3,8 @@
<mapper namespace="com.rymcu.forest.mapper.ForestFileMapper">
<resultMap id="BaseResultMap" type="com.rymcu.forest.entity.ForestFile">
<id column="id" property="id"/>
<id column="file_size" property="fileSize"/>
<id column="file_type" property="fileType"/>
<result column="md5_value" property="md5Value"/>
<result column="file_path" property="filePath"/>
<result column="file_url" property="fileUrl"/>
@ -12,14 +14,16 @@
</resultMap>
<insert id="insertForestFile">
insert into forest_file (md5_value, file_path, file_url, created_time, created_by)
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy})
insert into forest_file (md5_value, file_path, file_url, created_time, created_by, file_size, file_type)
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}, #{fileSize}, #{fileType})
</insert>
<select id="getFileUrlByMd5" resultType="java.lang.String">
select file_url
from forest_file
where md5_value = #{md5Value}
and created_by = #{createdBy}
and file_type = #{fileType}
</select>
</mapper>

View File

@ -384,4 +384,3 @@ create table forest_login_record
constraint forest_login_record_id_uindex
unique (id)
) comment '登录记录表';