From dc34fded6f9e8756c0d0be0e207723d6ca412f2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AF=9B=E6=AF=9B=E8=99=AB?= <1421374934@qq.com> Date: Thu, 13 Jan 2022 12:33:13 +0800 Subject: [PATCH] :bug: add service --- .../rymcu/forest/mapper/ForestFileMapper.java | 4 +-- .../forest/service/ForestFileService.java | 4 +-- .../service/impl/ForestFileServiceImpl.java | 10 +++---- .../web/api/common/UploadController.java | 29 +++++++++++++++---- src/main/java/mapper/ForestFileMapper.xml | 4 +-- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java index 359ff50..0dadde7 100644 --- a/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java +++ b/src/main/java/com/rymcu/forest/mapper/ForestFileMapper.java @@ -11,12 +11,12 @@ import org.apache.ibatis.annotations.Param; public interface ForestFileMapper extends Mapper { /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ - ForestFile getForestFileByMd5(@Param("md5Value") String md5Value); + String getFileUrlByMd5(@Param("md5Value") String md5Value); /** * 插入文件对象 diff --git a/src/main/java/com/rymcu/forest/service/ForestFileService.java b/src/main/java/com/rymcu/forest/service/ForestFileService.java index d4d8362..2c2c6ab 100644 --- a/src/main/java/com/rymcu/forest/service/ForestFileService.java +++ b/src/main/java/com/rymcu/forest/service/ForestFileService.java @@ -13,12 +13,12 @@ import com.rymcu.forest.entity.ForestFile; public interface ForestFileService extends Service { /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ - ForestFile getForestFileByMd5(String md5Value); + String getFileUrlByMd5(String md5Value); /** * 插入文件对象 diff --git a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java index ed87d5d..d3422fd 100644 --- a/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/ForestFileServiceImpl.java @@ -4,8 +4,8 @@ import com.rymcu.forest.core.service.AbstractService; import com.rymcu.forest.entity.ForestFile; import com.rymcu.forest.mapper.ForestFileMapper; import com.rymcu.forest.service.ForestFileService; -import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; @@ -14,7 +14,6 @@ import javax.annotation.Resource; * @date 2022-1-12 22:34:55 */ @Service -@Slf4j public class ForestFileServiceImpl extends AbstractService implements ForestFileService { @Resource @@ -22,14 +21,14 @@ public class ForestFileServiceImpl extends AbstractService implement /** - * 通过md5获取文件对象 + * 通过md5获取文件访问链接 * * @param md5Value md5值 * @return */ @Override - public ForestFile getForestFileByMd5(String md5Value) { - return forestFileMapper.getForestFileByMd5(md5Value); + public String getFileUrlByMd5(String md5Value) { + return forestFileMapper.getFileUrlByMd5(md5Value); } /** @@ -42,6 +41,7 @@ public class ForestFileServiceImpl extends AbstractService implement * @return */ @Override + @Transactional(rollbackFor = Exception.class) public int insert(String fileUrl, String filePath, String md5Value, long createdBy) { return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy); } diff --git a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java index c0e9455..9c9cf20 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/UploadController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/UploadController.java @@ -5,6 +5,7 @@ import com.rymcu.forest.core.result.GlobalResultGenerator; import com.rymcu.forest.dto.LinkToImageUrlDTO; import com.rymcu.forest.dto.TokenUser; import com.rymcu.forest.jwt.def.JwtConstants; +import com.rymcu.forest.service.ForestFileService; import com.rymcu.forest.util.FileUtils; import com.rymcu.forest.util.SpringContextHolder; import com.rymcu.forest.util.UserUtils; @@ -14,10 +15,12 @@ import com.rymcu.forest.web.api.exception.ErrorCode; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang.StringUtils; import org.springframework.core.env.Environment; +import org.springframework.util.DigestUtils; import org.springframework.util.FileCopyUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayOutputStream; import java.io.File; @@ -42,6 +45,9 @@ public class UploadController { private static Environment env = SpringContextHolder.getBean(Environment.class); + @Resource + private ForestFileService forestFileService; + @PostMapping("/file") public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) { if (multipartFile == null) { @@ -161,9 +167,9 @@ public class UploadController { public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException { String url = linkToImageUrlDTO.getUrl(); URL link = new URL(url); - HttpURLConnection conn = (HttpURLConnection)link.openConnection(); + HttpURLConnection conn = (HttpURLConnection) link.openConnection(); //设置超时间为3秒 - conn.setConnectTimeout(3*1000); + conn.setConnectTimeout(3 * 1000); //防止屏蔽程序抓取而返回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", ""); @@ -172,6 +178,19 @@ public class UploadController { InputStream inputStream = conn.getInputStream(); //获取自己数组 byte[] getData = readInputStream(inputStream); + + // 获取文件md5值 + String md5 = DigestUtils.md5DigestAsHex(getData); + String fileUrl = forestFileService.getFileUrlByMd5(md5); + + Map data = new HashMap(2); + 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; @@ -185,18 +204,18 @@ public class UploadController { file.mkdirs();// 创建文件根目录 } - String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/"; String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url); + fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName; String savePath = file.getPath() + File.separator + fileName; - Map data = new HashMap(2); File saveFile = new File(savePath); try { FileCopyUtils.copy(getData, saveFile); + forestFileService.insert(fileUrl, savePath, md5, 1); data.put("originalURL", url); - data.put("url", localPath + fileName); + data.put("url", fileUrl); } catch (IOException e) { data.put("message", "上传失败!"); } diff --git a/src/main/java/mapper/ForestFileMapper.xml b/src/main/java/mapper/ForestFileMapper.xml index 8e9080b..2a103cd 100644 --- a/src/main/java/mapper/ForestFileMapper.xml +++ b/src/main/java/mapper/ForestFileMapper.xml @@ -16,8 +16,8 @@ values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) - + select file_url from forest_file where md5_value = #{md5Value}