1. oss 相关 上传 is
This commit is contained in:
parent
4c6c63ac62
commit
00a0ec2a2a
|
|
@ -0,0 +1,20 @@
|
|||
package com.chushang.oss.entity.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 流文件上传
|
||||
* @auther: zhao
|
||||
* @date: 2024/6/29 18:09
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
public class UploadStreamDTO {
|
||||
|
||||
private InputStream is;
|
||||
|
||||
}
|
||||
|
|
@ -1,17 +1,15 @@
|
|||
package com.chushang.oss.feign;
|
||||
|
||||
import com.chushang.common.core.constant.SecurityConstants;
|
||||
import com.chushang.common.core.web.AjaxResult;
|
||||
import com.chushang.common.core.web.Result;
|
||||
import com.chushang.oss.constants.OssConstants;
|
||||
import com.chushang.oss.entity.dto.UploadBytesDTO;
|
||||
import com.chushang.oss.entity.dto.UploadFileDTO;
|
||||
import com.chushang.oss.entity.dto.UploadStreamDTO;
|
||||
import com.chushang.oss.entity.vo.FileSourceVo;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -23,11 +21,11 @@ public interface RemoteOssService {
|
|||
|
||||
@PostMapping(value = "/upload")
|
||||
Result<List<FileSourceVo>> uploadFile(@RequestBody UploadFileDTO uploadFile,
|
||||
@RequestParam(value = "ocrType", required = false) String ocrType,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
@RequestParam(value = "ocrType", required = false) String ocrType,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
|
||||
@PostMapping(value = "/uploadqccode")
|
||||
|
|
@ -41,12 +39,12 @@ public interface RemoteOssService {
|
|||
*
|
||||
*/
|
||||
@PostMapping(value = "/upload/bytes")
|
||||
Result<List<FileSourceVo>> uploadFile(@RequestBody UploadBytesDTO uploadBytes,
|
||||
@RequestParam(value = "fileName") String fileName,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
Result<List<FileSourceVo>> uploadBytes(@RequestBody UploadBytesDTO uploadBytes,
|
||||
@RequestParam(value = "fileName") String fileName,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
@GetMapping("/file/{fid}")
|
||||
Result<byte[]> getFile(@PathVariable(name = "fid") String fid,@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
|
@ -58,5 +56,20 @@ public interface RemoteOssService {
|
|||
Result<String> delFile(@RequestBody Set<String> fids,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
/**
|
||||
* 通过 文件字节上传,
|
||||
* 需要文件名称, 文件路径,
|
||||
* 不需要 做ocr, 需要做 水印添加
|
||||
* @param uploadStream is
|
||||
*
|
||||
*/
|
||||
@PostMapping(value = "/upload/stream")
|
||||
Result<List<FileSourceVo>> uploadStream(@RequestBody UploadStreamDTO uploadStream,
|
||||
@RequestParam(value = "fileName") String fileName,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestParam(value = "fileType", required = false) String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import com.chushang.common.log.annotation.SysLog;
|
|||
import com.chushang.common.log.enums.BusinessType;
|
||||
import com.chushang.oss.entity.dto.UploadBytesDTO;
|
||||
import com.chushang.oss.entity.dto.UploadFileDTO;
|
||||
import com.chushang.oss.entity.dto.UploadStreamDTO;
|
||||
import com.chushang.oss.entity.vo.FileSourceVo;
|
||||
import com.chushang.oss.feign.RemoteOssService;
|
||||
import com.chushang.oss.service.FileSourceService;
|
||||
|
|
@ -18,6 +19,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|||
|
||||
import javax.annotation.Resource;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
|
|
@ -32,11 +34,11 @@ public class RemoteFileController implements RemoteOssService {
|
|||
@PostMapping(value = "/upload")
|
||||
@InnerAuth
|
||||
public Result<List<FileSourceVo>> uploadFile(@RequestBody UploadFileDTO uploadFile,
|
||||
@RequestParam(value = "ocrType", required = false) String ocrType,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE)String source,
|
||||
@RequestParam(value = "fileType", required = false) String fileType) {
|
||||
@RequestParam(value = "ocrType", required = false) String ocrType,
|
||||
@RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
|
||||
@RequestParam(value = "formats", required = false) String formats,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE)String source,
|
||||
@RequestParam(value = "fileType", required = false) String fileType) {
|
||||
MultipartFile[] files = uploadFile.getFiles();
|
||||
return Result.ok(fileSourceService.addFile(files, ocrType, sealFlag, formats, fileType));
|
||||
}
|
||||
|
|
@ -59,12 +61,12 @@ public class RemoteFileController implements RemoteOssService {
|
|||
@SysLog(value = "feign文件", businessType = BusinessType.EXPORT)
|
||||
@PostMapping(value = "/upload/bytes")
|
||||
@InnerAuth
|
||||
public Result<List<FileSourceVo>> uploadFile(@RequestBody UploadBytesDTO uploadBytes,
|
||||
String fileName,
|
||||
Boolean sealFlag,
|
||||
String formats,
|
||||
String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source)
|
||||
public Result<List<FileSourceVo>> uploadBytes(@RequestBody UploadBytesDTO uploadBytes,
|
||||
String fileName,
|
||||
Boolean sealFlag,
|
||||
String formats,
|
||||
String fileType,
|
||||
@RequestHeader(SecurityConstants.FROM_SOURCE) String source)
|
||||
{
|
||||
byte[] bytes = uploadBytes.getBytes();
|
||||
return Result.ok(fileSourceService.uploadFile(bytes, fileName, sealFlag, formats, fileType));
|
||||
|
|
@ -103,7 +105,12 @@ public class RemoteFileController implements RemoteOssService {
|
|||
return Result.ok( fileSourceService.delFileBatch(fids));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Result<List<FileSourceVo>> uploadStream(UploadStreamDTO uploadStream, String fileName, Boolean sealFlag, String formats, String fileType, String source)
|
||||
{
|
||||
InputStream is = uploadStream.getIs();
|
||||
return Result.ok(fileSourceService.uploadFile(is, fileName, sealFlag, formats, fileType));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -342,7 +342,68 @@ public class FileSourceService
|
|||
|
||||
public List<FileSourceVo> uploadFile(byte[] bytes, String fileName, Boolean sealFlag, String formats, String fileType)
|
||||
{
|
||||
return Collections.singletonList(addFile(bytes, fileName, sealFlag, formats, fileType));
|
||||
return List.of(addFile(bytes, fileName, sealFlag, formats, fileType));
|
||||
}
|
||||
|
||||
public FileSourceVo addFile(InputStream is, String fileName, Boolean sealFlag, String formats, String fileType)
|
||||
{
|
||||
String ip = IPUtils.clientIp(ServletUtils.getRequest());
|
||||
String fid = generateFid();
|
||||
String fName = fileName;
|
||||
Tika t = new Tika();
|
||||
String mimetype = null;
|
||||
try {
|
||||
mimetype = t.detect(is.readAllBytes());
|
||||
if (fName.indexOf(".") < 0) {//如果文件名字没有后缀
|
||||
String ext = MimeTypes.getDefaultMimeTypes().forName(mimetype).getExtension();
|
||||
fName += ext;
|
||||
}
|
||||
} catch (MimeTypeException e) {
|
||||
log.error("文件后缀识别失败");
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
FileSourceInfo info = new FileSourceInfo();
|
||||
info.setFid(fid);
|
||||
info.setName(fName);
|
||||
// mimeType 还不是 后缀 类型
|
||||
info.setMimeType(mimetype);
|
||||
// size 如何获取?
|
||||
try {
|
||||
info.setSize((long) is.available());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
String md5 = FileUtils.getMd5(is);
|
||||
info.setMd5(md5);
|
||||
String path = ossService.getPrefixPath(md5,fName);
|
||||
// 不带https 的路径
|
||||
info.setPath(path);
|
||||
info.setStorage(uploadConfig.getStorage());
|
||||
info.setUploadIp(ip);
|
||||
// 上传到 oss 或者 minio
|
||||
String realPath = ossService.upload(is, info, sealFlag, formats);
|
||||
// 带https 的路径
|
||||
info.setRealPath(realPath);
|
||||
info.setFileType(fileType);
|
||||
info.setBucketName(uploadConfig.getBucketName());
|
||||
RMap<String, FileSourceInfo> map = redissonClient.getMap("OSS-Cache");
|
||||
map.put(fid, info);
|
||||
// 入库
|
||||
save(info);
|
||||
map.remove(info.getFid());
|
||||
|
||||
FileSourceVo vo = new FileSourceVo();
|
||||
vo.setFid(info.getFid());
|
||||
vo.setFilePath(info.getRealPath());
|
||||
vo.setName(fName);
|
||||
vo.setSize(info.getSize());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public List<FileSourceVo> uploadFile(InputStream is, String fileName, Boolean sealFlag, String formats, String fileType)
|
||||
{
|
||||
return List.of(addFile(is, fileName, sealFlag, formats, fileType));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -43,6 +43,14 @@ public interface OssService {
|
|||
* @return 返回http地址
|
||||
*/
|
||||
String upload(InputStream inputStream, FileSourceInfo info);
|
||||
default String upload(InputStream inputStream, FileSourceInfo info, Boolean sealFlag, String formats){
|
||||
// 为true 时 添加水印
|
||||
if (sealFlag && StringUtils.isNotEmpty(formats)){
|
||||
// if (StringUtils.isEmpty(formats)) throw new ResultException("添加水印时, 水印内容不能为空");
|
||||
inputStream = FileUtils.waterSeal(Arrays.stream(formats.split(",")).toList(), inputStream);
|
||||
}
|
||||
return upload(inputStream, info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除 cloud 中的文件
|
||||
|
|
|
|||
Loading…
Reference in New Issue