🐛 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> {
/**
* 通过md5获取文件对象
* 通过md5获取文件访问链接
*
* @param md5Value md5值
* @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> {
/**
* 通过md5获取文件对象
* 通过md5获取文件访问链接
*
* @param md5Value md5值
* @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.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<ForestFile> implements ForestFileService {
@Resource
@ -22,14 +21,14 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> 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<ForestFile> 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);
}

View File

@ -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) {
@ -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", "上传失败!");
}

View File

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