package com.iplatform.file.controller;
|
|
import com.iplatform.base.SystemController;
|
import com.iplatform.base.config.ApiProperties;
|
import com.walker.file.FileInfo;
|
import com.walker.infrastructure.utils.FileCopyUtils;
|
import com.walker.infrastructure.utils.StringUtils;
|
import com.walker.web.ResponseValue;
|
import com.walker.web.util.ServletUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.IOException;
|
import java.util.Map;
|
import java.util.concurrent.ConcurrentHashMap;
|
|
@RestController
|
@RequestMapping("/oss")
|
public class OssFileApi extends SystemController {
|
|
private ApiProperties apiProperties;
|
|
@Autowired
|
public OssFileApi(ApiProperties apiProperties){
|
this.apiProperties = apiProperties;
|
}
|
|
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
|
@ResponseBody
|
public void downloadOssFile(@PathVariable(name = "id") String id){
|
if(StringUtils.isEmpty(id)){
|
throw new IllegalArgumentException("文件参数为空,无法下载文件");
|
}
|
FileInfo fileInfo = this.acquireFileOperateSpi().getFileInfo(Long.parseLong(id));
|
if(fileInfo == null){
|
throw new IllegalArgumentException("文件不存在,id=" + id);
|
}
|
|
// 2024-03-08 为压测提供支持,
|
try {
|
byte[] fileContent = null;
|
if(this.apiProperties.isTimeEnabled()){
|
// 压测模式,记录接口调用时间
|
fileContent = this.acquirePressureTestFile(fileInfo);
|
} else {
|
// 正常模式
|
fileContent = this.getOssFileData(id);
|
}
|
this.downloadSimpleFile(fileContent, fileInfo.getFileName());
|
|
} catch (IOException e) {
|
logger.error("下载oss错误:" + e.getMessage() + ", id=" + id, e);
|
ServletUtils.renderString(getResponse(), "下载oss错误:" + e.getMessage() + ", id=" + id);
|
}
|
}
|
|
/**
|
* 压测时尝试从缓存查找文件,如果未缓存会把默认64个文件缓存。如果超过64个,先清除缓存,并重新开始缓存后续64个,循环往复。
|
* @param fileInfo
|
* @return
|
* @author 时克英
|
* @date 2024-03-08
|
*/
|
private byte[] acquirePressureTestFile(FileInfo fileInfo){
|
byte[] fileData = null;
|
long fileSize = fileInfo.getFileSize();
|
if(fileSize <= maxCacheFileSize){
|
// 当缓存数量达到上限时,清空重新开始缓存新的
|
if(fileContentCache.size() >= maxCacheFileCount){
|
fileContentCache.clear();
|
}
|
// 当文件小于等于1M,并且缓存数量未达到上限时,缓存
|
fileData = this.fileContentCache.get(fileInfo.getId());
|
if(fileData == null){
|
fileData = this.getOssFileData(fileInfo.getId());
|
this.fileContentCache.put(fileInfo.getId(), fileData);
|
logger.debug("缓存一个oss文件:{},大小:{}", fileInfo.getFileName(), fileSize);
|
} else {
|
logger.debug("从缓存加载了oss文件:{}", fileInfo.getId());
|
}
|
} else {
|
fileData = this.getOssFileData(fileInfo.getId());
|
}
|
return fileData;
|
}
|
|
private Map<String, byte[]> fileContentCache = new ConcurrentHashMap<>(128);
|
|
private int maxCacheFileCount = 64;
|
private long maxCacheFileSize = 1024 * 1024;
|
|
/**
|
* 模拟测试。
|
*/
|
@RequestMapping(value = "/test/demo", method = RequestMethod.GET)
|
public void downloadOssDemo(){
|
String file = "F:/app_ocr_demo/ocr_e_005.jpg";
|
try {
|
this.downloadSimpleFile(FileCopyUtils.copyToByteArray(new File(file)), "测试图片");
|
} catch (IOException e) {
|
logger.error("下载oss demo错误:" + e.getMessage() + ", id=" + file, e);
|
ServletUtils.renderString(getResponse(), "下载oss demo错误:" + e.getMessage() + ", id=" + file);
|
}
|
}
|
|
@RequestMapping(value = "/test/upload", method = RequestMethod.GET)
|
public ResponseValue demoUploadOss() throws Exception{
|
File file = new File("F:/app_ocr_demo/ocr_e_005.jpg");
|
FileInputStream inputStream = new FileInputStream(file);
|
FileInfo fileInfo = this.uploadFileToRemote(inputStream, "demo.jpg", null, file.length(), 0, "-1");
|
logger.info("fileInfo = {}", fileInfo);
|
return ResponseValue.success("上传文件成功," + fileInfo.getUrl());
|
}
|
}
|