🐛 add service

This commit is contained in:
毛毛虫 2022-01-13 12:33:13 +08:00
parent 245b359481
commit dc34fded6f
5 changed files with 35 additions and 16 deletions

View File

@ -11,12 +11,12 @@ import org.apache.ibatis.annotations.Param;
public interface ForestFileMapper extends Mapper<ForestFile> { public interface ForestFileMapper extends Mapper<ForestFile> {
/** /**
* 通过md5获取文件对象 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @return * @return
*/ */
ForestFile getForestFileByMd5(@Param("md5Value") String md5Value); String getFileUrlByMd5(@Param("md5Value") String md5Value);
/** /**
* 插入文件对象 * 插入文件对象

View File

@ -13,12 +13,12 @@ import com.rymcu.forest.entity.ForestFile;
public interface ForestFileService extends Service<ForestFile> { public interface ForestFileService extends Service<ForestFile> {
/** /**
* 通过md5获取文件对象 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @return * @return
*/ */
ForestFile getForestFileByMd5(String md5Value); String getFileUrlByMd5(String md5Value);
/** /**
* 插入文件对象 * 插入文件对象

View File

@ -4,8 +4,8 @@ import com.rymcu.forest.core.service.AbstractService;
import com.rymcu.forest.entity.ForestFile; import com.rymcu.forest.entity.ForestFile;
import com.rymcu.forest.mapper.ForestFileMapper; import com.rymcu.forest.mapper.ForestFileMapper;
import com.rymcu.forest.service.ForestFileService; import com.rymcu.forest.service.ForestFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -14,7 +14,6 @@ import javax.annotation.Resource;
* @date 2022-1-12 22:34:55 * @date 2022-1-12 22:34:55
*/ */
@Service @Service
@Slf4j
public class ForestFileServiceImpl extends AbstractService<ForestFile> implements ForestFileService { public class ForestFileServiceImpl extends AbstractService<ForestFile> implements ForestFileService {
@Resource @Resource
@ -22,14 +21,14 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
/** /**
* 通过md5获取文件对象 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @return * @return
*/ */
@Override @Override
public ForestFile getForestFileByMd5(String md5Value) { public String getFileUrlByMd5(String md5Value) {
return forestFileMapper.getForestFileByMd5(md5Value); return forestFileMapper.getFileUrlByMd5(md5Value);
} }
/** /**
@ -42,6 +41,7 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
* @return * @return
*/ */
@Override @Override
@Transactional(rollbackFor = Exception.class)
public int insert(String fileUrl, String filePath, String md5Value, long createdBy) { public int insert(String fileUrl, String filePath, String md5Value, long createdBy) {
return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy); return forestFileMapper.insert(fileUrl, filePath, md5Value, createdBy);
} }

View File

@ -5,6 +5,7 @@ import com.rymcu.forest.core.result.GlobalResultGenerator;
import com.rymcu.forest.dto.LinkToImageUrlDTO; import com.rymcu.forest.dto.LinkToImageUrlDTO;
import com.rymcu.forest.dto.TokenUser; import com.rymcu.forest.dto.TokenUser;
import com.rymcu.forest.jwt.def.JwtConstants; import com.rymcu.forest.jwt.def.JwtConstants;
import com.rymcu.forest.service.ForestFileService;
import com.rymcu.forest.util.FileUtils; import com.rymcu.forest.util.FileUtils;
import com.rymcu.forest.util.SpringContextHolder; import com.rymcu.forest.util.SpringContextHolder;
import com.rymcu.forest.util.UserUtils; 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.codec.binary.Base64;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils; import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
@ -42,6 +45,9 @@ public class UploadController {
private static Environment env = SpringContextHolder.getBean(Environment.class); private static Environment env = SpringContextHolder.getBean(Environment.class);
@Resource
private ForestFileService forestFileService;
@PostMapping("/file") @PostMapping("/file")
public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) { public GlobalResult uploadPicture(@RequestParam(value = "file", required = false) MultipartFile multipartFile, @RequestParam(defaultValue = "1") Integer type, HttpServletRequest request) {
if (multipartFile == null) { if (multipartFile == null) {
@ -161,9 +167,9 @@ public class UploadController {
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException { public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException {
String url = linkToImageUrlDTO.getUrl(); String url = linkToImageUrlDTO.getUrl();
URL link = new URL(url); URL link = new URL(url);
HttpURLConnection conn = (HttpURLConnection)link.openConnection(); HttpURLConnection conn = (HttpURLConnection) link.openConnection();
//设置超时间为3秒 //设置超时间为3秒
conn.setConnectTimeout(3*1000); conn.setConnectTimeout(3 * 1000);
//防止屏蔽程序抓取而返回403错误 //防止屏蔽程序抓取而返回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("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", ""); conn.setRequestProperty("referer", "");
@ -172,6 +178,19 @@ public class UploadController {
InputStream inputStream = conn.getInputStream(); InputStream inputStream = conn.getInputStream();
//获取自己数组 //获取自己数组
byte[] getData = readInputStream(inputStream); 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(); Integer type = linkToImageUrlDTO.getType();
if (Objects.isNull(type)) { if (Objects.isNull(type)) {
type = 1; type = 1;
@ -185,18 +204,18 @@ public class UploadController {
file.mkdirs();// 创建文件根目录 file.mkdirs();// 创建文件根目录
} }
String localPath = Utils.getProperty("resource.file-path") + "/" + typePath + "/";
String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url); String fileName = System.currentTimeMillis() + "." + FileUtils.getExtend(url);
fileUrl = Utils.getProperty("resource.file-path") + "/" + typePath + "/" + fileName;
String savePath = file.getPath() + File.separator + fileName; String savePath = file.getPath() + File.separator + fileName;
Map data = new HashMap(2);
File saveFile = new File(savePath); File saveFile = new File(savePath);
try { try {
FileCopyUtils.copy(getData, saveFile); FileCopyUtils.copy(getData, saveFile);
forestFileService.insert(fileUrl, savePath, md5, 1);
data.put("originalURL", url); data.put("originalURL", url);
data.put("url", localPath + fileName); data.put("url", fileUrl);
} catch (IOException e) { } catch (IOException e) {
data.put("message", "上传失败!"); data.put("message", "上传失败!");
} }

View File

@ -16,8 +16,8 @@
values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy}) values (#{md5Value}, #{filePath}, #{fileUrl}, sysdate(), #{createdBy})
</insert> </insert>
<select id="getForestFileByMd5" resultMap="BaseResultMap"> <select id="getFileUrlByMd5" resultType="string">
select * select file_url
from forest_file from forest_file
where md5_value = #{md5Value} where md5_value = #{md5Value}
</select> </select>