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")
@PermitAll
@Operation(summary = "获取图片")
public void proxyRead(@RequestParam String fileId, @RequestParam boolean isTemp, HttpServletResponse response) {
proxyService.doGetImage(fileId, isTemp, response);
public void proxyRead(@RequestParam String path, HttpServletResponse response) {
proxyService.doGetImage(path, response);
}
}

View File

@ -42,8 +42,10 @@ import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.TimeUnit;
@ -204,9 +206,9 @@ public class ProxyService {
// log.info("receive response: {}", responseBody);
log.info("receive json response: {}", jsonStr);
JSONObject jsonObject = JSON.parseObject(jsonStr);
if (jsonObject.containsKey("url")) {
String url = jsonObject.getString("url");
String fileId = extractFileId(url);
if (jsonObject.containsKey("data")) {
String realPath = jsonObject.getString("data");
String url = initBasePath() + "/ierp/attachment/download.do?path=" + realPath;
String downloadUrl = url + "&access_token=" + initAccessToken();
byte[] bytes = imageService.downloadImage(downloadUrl);
ImageStorage imageStorage = new ImageStorage();
@ -216,8 +218,8 @@ public class ProxyService {
imageStorage.setFileSize(fileBody.getContentLength());
imageStorage.setFileContent(transformToBase64(bytes));
imageService.saveImage(fileId, imageStorage);
String convertFileUrl = doConvertFileUrl(fileId, true);
imageService.saveImage(realPath, imageStorage);
String convertFileUrl = doConvertFileUrl(realPath);
log.info("convertFileUrl: {}", convertFileUrl);
return convertFileUrl;
}
@ -421,30 +423,25 @@ public class ProxyService {
return "";
}
public String doConvertFileUrl(String fileId, boolean isTemp) {
if (isTemp) {
return "/crm-api/proxy/file/read/do?fileId=" + fileId + "&isTemp=true";
}
return "";
public String doConvertFileUrl(String path) {
return "/crm-api/proxy/file/read/do?path=" + path;
}
public void doGetImage(String fileId, boolean isTemp, HttpServletResponse response) {
if (isTemp) {
ImageStorage image = imageService.getImage(fileId);
byte[] bytes = Base64.decode(image.getFileContent());
public void doGetImage(String path, HttpServletResponse response) {
ImageStorage image = imageService.getImage(path);
byte[] bytes = Base64.decode(image.getFileContent());
try {
response.setContentType(image.getMediaType());
response.setContentLength(image.getFileSize().intValue());
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getFileName() + "\"");
try {
response.setContentType(image.getMediaType());
response.setContentLength(image.getFileSize().intValue());
response.setHeader("Content-Disposition", "inline; filename=\"" + image.getFileName() + "\"");
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
ServletOutputStream outputStream = response.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
}