🐛 通过md5值和当前用户id获取文件url

This commit is contained in:
毛毛虫 2022-01-13 23:45:02 +08:00
parent d79b4f91ac
commit a1a573ad87
5 changed files with 18 additions and 17 deletions

View File

@ -13,10 +13,11 @@ public interface ForestFileMapper extends Mapper<ForestFile> {
/** /**
* 通过md5获取文件访问链接 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @param createdBy 创建人
* @return * @return
*/ */
String getFileUrlByMd5(@Param("md5Value") String md5Value); String getFileUrlByMd5(@Param("md5Value") String md5Value, @Param("createdBy") long createdBy);
/** /**
* 插入文件对象 * 插入文件对象

View File

@ -15,10 +15,11 @@ public interface ForestFileService extends Service<ForestFile> {
/** /**
* 通过md5获取文件访问链接 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @param createdBy 用户id
* @return * @return
*/ */
String getFileUrlByMd5(String md5Value); String getFileUrlByMd5(String md5Value, long createdBy);
/** /**
* 插入文件对象 * 插入文件对象

View File

@ -22,12 +22,13 @@ public class ForestFileServiceImpl extends AbstractService<ForestFile> implement
/** /**
* 通过md5获取文件访问链接 * 通过md5获取文件访问链接
* *
* @param md5Value md5值 * @param md5Value md5值
* @param createdBy 用户id
* @return * @return
*/ */
@Override @Override
public String getFileUrlByMd5(String md5Value) { public String getFileUrlByMd5(String md5Value, long createdBy) {
return forestFileMapper.getFileUrlByMd5(md5Value); return forestFileMapper.getFileUrlByMd5(md5Value, createdBy);
} }
/** /**

View File

@ -121,7 +121,7 @@ public class UploadController {
TokenUser tokenUser = getTokenUser(request); TokenUser tokenUser = getTokenUser(request);
Map data = new HashMap(2); Map data = new HashMap(2);
String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream()); String md5 = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
String fileUrl = forestFileService.getFileUrlByMd5(md5); String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
if (StringUtils.isNotEmpty(fileUrl)) { if (StringUtils.isNotEmpty(fileUrl)) {
data.put("url", fileUrl); data.put("url", fileUrl);
return GlobalResultGenerator.genSuccessResult(data); return GlobalResultGenerator.genSuccessResult(data);
@ -180,7 +180,7 @@ public class UploadController {
try (InputStream in = multipartFiles[i].getInputStream(); try (InputStream in = multipartFiles[i].getInputStream();
OutputStream out = Files.newOutputStream(saveFile.toPath())) { OutputStream out = Files.newOutputStream(saveFile.toPath())) {
String md5 = DigestUtils.md5DigestAsHex(in); String md5 = DigestUtils.md5DigestAsHex(in);
String fileUrl = forestFileService.getFileUrlByMd5(md5); String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
if (StringUtils.isNotEmpty(fileUrl)) { if (StringUtils.isNotEmpty(fileUrl)) {
successMap.put(orgName, fileUrl); successMap.put(orgName, fileUrl);
continue; continue;
@ -235,12 +235,9 @@ public class UploadController {
@PostMapping("/file/link") @PostMapping("/file/link")
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO) throws IOException, BaseApiException { public GlobalResult linkToImageUrl(@RequestBody LinkToImageUrlDTO linkToImageUrlDTO, HttpServletRequest request) throws IOException, BaseApiException {
//todo 无法获取当前登录用户
// User user = UserUtils.getCurrentUserByToken(); TokenUser tokenUser = getTokenUser(request);
// if (Objects.isNull(user)) {
// throw new BaseApiException(ErrorCode.INVALID_TOKEN);
// }
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();
@ -255,7 +252,7 @@ public class UploadController {
// 获取文件md5值 // 获取文件md5值
String md5 = DigestUtils.md5DigestAsHex(inputStream); String md5 = DigestUtils.md5DigestAsHex(inputStream);
String fileUrl = forestFileService.getFileUrlByMd5(md5); String fileUrl = forestFileService.getFileUrlByMd5(md5, tokenUser.getIdUser());
Map data = new HashMap(2); Map data = new HashMap(2);
data.put("originalURL", url); data.put("originalURL", url);
@ -289,7 +286,7 @@ public class UploadController {
//获取自己数组 //获取自己数组
byte[] getData = readInputStream(inputStream); byte[] getData = readInputStream(inputStream);
FileCopyUtils.copy(getData, saveFile); FileCopyUtils.copy(getData, saveFile);
forestFileService.insertForestFile(fileUrl, savePath, md5, 1); forestFileService.insertForestFile(fileUrl, savePath, md5, tokenUser.getIdUser());
data.put("originalURL", url); data.put("originalURL", url);
data.put("url", fileUrl); data.put("url", fileUrl);
} catch (IOException e) { } catch (IOException e) {

View File

@ -20,6 +20,7 @@
select file_url select file_url
from forest_file from forest_file
where md5_value = #{md5Value} where md5_value = #{md5Value}
and created_by = #{createdBy}
</select> </select>
</mapper> </mapper>