feat:sync image
This commit is contained in:
parent
a9f7feead3
commit
032ffbd876
@ -13,6 +13,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.security.PermitAll;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
@ -49,9 +50,9 @@ public class ProxyController {
|
||||
}
|
||||
|
||||
@GetMapping("/file/read/do")
|
||||
@PreAuthorize("@ss.hasPermission('crm:proxy:all')")
|
||||
@PermitAll
|
||||
@Operation(summary = "获取图片")
|
||||
public byte[] proxyRead(@RequestParam String fileId, @RequestParam boolean isTemp) throws IOException, URISyntaxException, AllKingdeeException {
|
||||
return proxyService.dpGetImage(fileId, isTemp);
|
||||
public void proxyRead(@RequestParam String fileId, @RequestParam boolean isTemp, HttpServletResponse response) {
|
||||
proxyService.doGetImage(fileId, isTemp, response);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.ensign.crm.module.crm.model;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import org.springframework.boot.context.properties.ConstructorBinding;
|
||||
|
||||
/**
|
||||
* @Description: TODO
|
||||
* @Date: 2024/9/29 9:36
|
||||
* @Created: by ZZSLL
|
||||
*/
|
||||
|
||||
@Data
|
||||
public class ImageStorage {
|
||||
private String fileName;
|
||||
private Long fileSize;
|
||||
private String mediaType;
|
||||
private String fileContent;
|
||||
}
|
@ -1,17 +1,16 @@
|
||||
package com.ensign.crm.module.crm.service;
|
||||
|
||||
import com.ensign.crm.module.crm.model.ImageStorage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@ -27,7 +26,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class ImageService {
|
||||
|
||||
@Resource
|
||||
private RedisTemplate<String, byte[]> redisTemplate;
|
||||
private RedisTemplate<String, ImageStorage> redisTemplate;
|
||||
|
||||
private final String CACHE_PREFIX = "kingdee:temp_file";
|
||||
|
||||
@ -46,11 +45,11 @@ public class ImageService {
|
||||
}
|
||||
}
|
||||
|
||||
public void saveImage(String key, byte[] imageData) {
|
||||
public void saveImage(String key, ImageStorage imageData) {
|
||||
redisTemplate.opsForValue().set(CACHE_PREFIX + key, imageData, 2, TimeUnit.HOURS);
|
||||
}
|
||||
|
||||
public byte[] getImage(String key) {
|
||||
public ImageStorage getImage(String key) {
|
||||
return redisTemplate.opsForValue().get(CACHE_PREFIX + key);
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,12 @@
|
||||
package com.ensign.crm.module.crm.service;
|
||||
|
||||
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ensign.crm.module.crm.exception.AllKingdeeException;
|
||||
import com.ensign.crm.module.crm.model.ImageStorage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
@ -24,19 +26,21 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.MediaTypeFactory;
|
||||
import org.springframework.http.client.ClientHttpRequest;
|
||||
import org.springframework.http.client.ClientHttpResponse;
|
||||
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.MimeType;
|
||||
import org.springframework.util.StreamUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.*;
|
||||
@ -168,7 +172,8 @@ public class ProxyService {
|
||||
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||
HttpPost httpPost = new HttpPost(newUri);
|
||||
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
||||
builder.addPart("file", new FileBody(transferToFile(file)));
|
||||
FileBody fileBody = new FileBody(transferToFile(file));
|
||||
builder.addPart("file", fileBody);
|
||||
HttpEntity multipartEntity = builder.build();
|
||||
httpPost.setEntity(multipartEntity);
|
||||
HttpResponse kResponse = httpClient.execute(httpPost);
|
||||
@ -180,12 +185,29 @@ public class ProxyService {
|
||||
String fileId = extractFileId(url);
|
||||
String downloadUrl = url + "&access_token=" + initAccessToken();
|
||||
byte[] bytes = imageService.downloadImage(downloadUrl);
|
||||
imageService.saveImage(fileId, bytes);
|
||||
ImageStorage imageStorage = new ImageStorage();
|
||||
|
||||
imageStorage.setMediaType(getFileMediaType(fileBody.getFilename()));
|
||||
imageStorage.setFileName(fileBody.getFilename());
|
||||
imageStorage.setFileSize(fileBody.getContentLength());
|
||||
imageStorage.setFileContent(transformToBase64(bytes));
|
||||
|
||||
imageService.saveImage(fileId, imageStorage);
|
||||
return doConvertFileUrl(fileId, true);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getFileMediaType(String filename) {
|
||||
Optional<MediaType> mediaType = MediaTypeFactory.getMediaType(filename);
|
||||
return mediaType.map(MimeType::toString).orElse("");
|
||||
}
|
||||
|
||||
|
||||
private String transformToBase64(byte[] file) {
|
||||
return Base64.encode(file);
|
||||
}
|
||||
|
||||
private String extractFileId(String url) throws AllKingdeeException, URISyntaxException {
|
||||
URI uri = new URI(url);
|
||||
String query = uri.getQuery();
|
||||
@ -378,10 +400,23 @@ public class ProxyService {
|
||||
return "";
|
||||
}
|
||||
|
||||
public byte[] dpGetImage(String fileId, boolean isTemp) {
|
||||
public void doGetImage(String fileId, boolean isTemp, HttpServletResponse response) {
|
||||
if (isTemp) {
|
||||
return imageService.getImage(fileId);
|
||||
ImageStorage image = imageService.getImage(fileId);
|
||||
byte[] bytes = Base64.decode(image.getFileContent());
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user