feat:file proxy

This commit is contained in:
zzs 2024-10-18 09:44:44 +08:00
parent d0ee190726
commit 57644df736
2 changed files with 24 additions and 27 deletions

View File

@ -52,7 +52,7 @@ public class ProxyController {
@GetMapping("/file/read/do") @GetMapping("/file/read/do")
@PermitAll @PermitAll
@Operation(summary = "获取图片") @Operation(summary = "获取图片")
public void proxyRead(@RequestParam String fileId, @RequestParam boolean isTemp, HttpServletResponse response) { public void proxyRead(@RequestParam String path, HttpServletResponse response) {
proxyService.doGetImage(fileId, isTemp, response); proxyService.doGetImage(path, response);
} }
} }

View File

@ -42,8 +42,10 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.math.BigDecimal;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -204,9 +206,9 @@ public class ProxyService {
// log.info("receive response: {}", responseBody); // log.info("receive response: {}", responseBody);
log.info("receive json response: {}", jsonStr); log.info("receive json response: {}", jsonStr);
JSONObject jsonObject = JSON.parseObject(jsonStr); JSONObject jsonObject = JSON.parseObject(jsonStr);
if (jsonObject.containsKey("url")) { if (jsonObject.containsKey("data")) {
String url = jsonObject.getString("url"); String realPath = jsonObject.getString("data");
String fileId = extractFileId(url); String url = initBasePath() + "/ierp/attachment/download.do?path=" + realPath;
String downloadUrl = url + "&access_token=" + initAccessToken(); String downloadUrl = url + "&access_token=" + initAccessToken();
byte[] bytes = imageService.downloadImage(downloadUrl); byte[] bytes = imageService.downloadImage(downloadUrl);
ImageStorage imageStorage = new ImageStorage(); ImageStorage imageStorage = new ImageStorage();
@ -216,8 +218,8 @@ public class ProxyService {
imageStorage.setFileSize(fileBody.getContentLength()); imageStorage.setFileSize(fileBody.getContentLength());
imageStorage.setFileContent(transformToBase64(bytes)); imageStorage.setFileContent(transformToBase64(bytes));
imageService.saveImage(fileId, imageStorage); imageService.saveImage(realPath, imageStorage);
String convertFileUrl = doConvertFileUrl(fileId, true); String convertFileUrl = doConvertFileUrl(realPath);
log.info("convertFileUrl: {}", convertFileUrl); log.info("convertFileUrl: {}", convertFileUrl);
return convertFileUrl; return convertFileUrl;
} }
@ -421,16 +423,12 @@ public class ProxyService {
return ""; return "";
} }
public String doConvertFileUrl(String fileId, boolean isTemp) { public String doConvertFileUrl(String path) {
if (isTemp) { return "/crm-api/proxy/file/read/do?path=" + path;
return "/crm-api/proxy/file/read/do?fileId=" + fileId + "&isTemp=true";
}
return "";
} }
public void doGetImage(String fileId, boolean isTemp, HttpServletResponse response) { public void doGetImage(String path, HttpServletResponse response) {
if (isTemp) { ImageStorage image = imageService.getImage(path);
ImageStorage image = imageService.getImage(fileId);
byte[] bytes = Base64.decode(image.getFileContent()); byte[] bytes = Base64.decode(image.getFileContent());
try { try {
@ -447,4 +445,3 @@ public class ProxyService {
} }
} }
} }
}