|
|
@ -3,13 +3,14 @@ package com.zilber.boot.file.service; |
|
|
|
import com.aliyun.oss.*; |
|
|
|
import com.aliyun.oss.*; |
|
|
|
import com.aliyun.oss.model.*; |
|
|
|
import com.aliyun.oss.model.*; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
|
|
import com.cppcc.common.validator.ValidatorUtils; |
|
|
|
import com.zilber.boot.file.dao.FileDao; |
|
|
|
import com.cppcc.files.*; |
|
|
|
|
|
|
|
import com.cppcc.files.dao.FileDao; |
|
|
|
|
|
|
|
import com.zilber.boot.file.entity.*; |
|
|
|
import com.zilber.boot.file.entity.*; |
|
|
|
|
|
|
|
import com.zilber.boot.utils.file.FileUtils; |
|
|
|
|
|
|
|
import com.zilber.boot.utils.sign.Md5Utils; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
|
import org.apache.commons.io.FilenameUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
|
|
|
|
import org.apache.tomcat.util.security.MD5Encoder; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
@ -33,7 +34,7 @@ import java.util.stream.Collectors; |
|
|
|
@Slf4j |
|
|
|
@Slf4j |
|
|
|
public class UploadUtils { |
|
|
|
public class UploadUtils { |
|
|
|
|
|
|
|
|
|
|
|
@Value("${cppcc.localtion}") |
|
|
|
@Value("${zilber.localtion}") |
|
|
|
private String localtion; |
|
|
|
private String localtion; |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
@Resource |
|
|
@ -44,13 +45,20 @@ public class UploadUtils { |
|
|
|
|
|
|
|
|
|
|
|
public Map<String, String> upload(MultipartFile file) { |
|
|
|
public Map<String, String> upload(MultipartFile file) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
|
|
|
|
Map<String, String> objectMap = fileDao.listFileUrl(MD5Encoder.encode(file.getBytes()), 1); |
|
|
|
|
|
|
|
if ( objectMap != null ){ |
|
|
|
|
|
|
|
objectMap.put("name", file.getOriginalFilename()); |
|
|
|
|
|
|
|
return objectMap; |
|
|
|
|
|
|
|
} |
|
|
|
String type = "." + FilenameUtils.getExtension(file.getOriginalFilename()); |
|
|
|
String type = "." + FilenameUtils.getExtension(file.getOriginalFilename()); |
|
|
|
String name = FilenameUtils.getName(file.getOriginalFilename()); |
|
|
|
String name = FilenameUtils.getName(file.getOriginalFilename()); |
|
|
|
String filePath = localtion + "/upload"; |
|
|
|
String filePath = localtion + "/upload"; |
|
|
|
String fileName = UUID.randomUUID() + type; |
|
|
|
String fileName = UUID.randomUUID() + type; |
|
|
|
File desc = getAbsoluteFile(filePath, fileName); |
|
|
|
File desc = getAbsoluteFile(filePath, fileName); |
|
|
|
file.transferTo(desc); |
|
|
|
file.transferTo(desc); |
|
|
|
return getPathFileName(filePath, fileName, name, type); |
|
|
|
Map<String, String> pathFileName = getPathFileName(filePath, fileName, name, type); |
|
|
|
|
|
|
|
fileDao.addFile(MD5Encoder.encode(file.getBytes()), filePath, Integer.parseInt(pathFileName.get("type")), 1); |
|
|
|
|
|
|
|
return pathFileName; |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(e.getLocalizedMessage()); |
|
|
|
log.error(e.getLocalizedMessage()); |
|
|
|
return new ConcurrentHashMap<>(); |
|
|
|
return new ConcurrentHashMap<>(); |
|
|
@ -64,7 +72,7 @@ public class UploadUtils { |
|
|
|
String pathFileName = "/profile" + "/" + currentDir + "/" + fileName; |
|
|
|
String pathFileName = "/profile" + "/" + currentDir + "/" + fileName; |
|
|
|
map.put("url", pathFileName); |
|
|
|
map.put("url", pathFileName); |
|
|
|
map.put("name", name); |
|
|
|
map.put("name", name); |
|
|
|
map.put("type", String.valueOf(SliceUploadsTemplate.getFileType(type))); |
|
|
|
map.put("type", String.valueOf(getFileType(type))); |
|
|
|
return map; |
|
|
|
return map; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -86,7 +94,16 @@ public class UploadUtils { |
|
|
|
* @param module oss目录 |
|
|
|
* @param module oss目录 |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public PutObjectResult upload(MultipartFile file, @Nullable String module) { |
|
|
|
public Map<String, String> upload(MultipartFile file, @Nullable String module) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
Map<String, String> objectMap = fileDao.listFileUrl(MD5Encoder.encode(file.getBytes()), 2); |
|
|
|
|
|
|
|
if ( objectMap != null ){ |
|
|
|
|
|
|
|
objectMap.put("name", file.getOriginalFilename()); |
|
|
|
|
|
|
|
return objectMap; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -97,8 +114,9 @@ public class UploadUtils { |
|
|
|
if ( StringUtils.isBlank(module) ){ |
|
|
|
if ( StringUtils.isBlank(module) ){ |
|
|
|
module = ossProperties.getModule(); |
|
|
|
module = ossProperties.getModule(); |
|
|
|
} |
|
|
|
} |
|
|
|
String extension = file.getName().substring(file.getName().lastIndexOf(".")); |
|
|
|
String extension = "." + FilenameUtils.getExtension(file.getOriginalFilename()); |
|
|
|
String objectName = module + "/" + UUID.randomUUID().toString() + extension; |
|
|
|
String objectName = module + "/" + UUID.randomUUID().toString() + extension; |
|
|
|
|
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
|
// 创建OSSClient实例。
|
|
|
|
// 创建OSSClient实例。
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
|
@ -110,7 +128,15 @@ public class UploadUtils { |
|
|
|
putObjectRequest.setProcess("true"); |
|
|
|
putObjectRequest.setProcess("true"); |
|
|
|
// 上传字符串。
|
|
|
|
// 上传字符串。
|
|
|
|
PutObjectResult result = ossClient.putObject(putObjectRequest); |
|
|
|
PutObjectResult result = ossClient.putObject(putObjectRequest); |
|
|
|
return result; |
|
|
|
if ( result.getResponse().getStatusCode() == 200 ){ |
|
|
|
|
|
|
|
Integer fileType = getFileType(extension); |
|
|
|
|
|
|
|
String uri = result.getResponse().getUri(); |
|
|
|
|
|
|
|
resultMap.put("url", uri); |
|
|
|
|
|
|
|
resultMap.put("name", file.getOriginalFilename()); |
|
|
|
|
|
|
|
resultMap.put("type", String.valueOf(fileType)); |
|
|
|
|
|
|
|
fileDao.addFile(MD5Encoder.encode(file.getBytes()), uri, fileType, 2); |
|
|
|
|
|
|
|
return resultMap; |
|
|
|
|
|
|
|
} |
|
|
|
} catch (OSSException oe) { |
|
|
|
} catch (OSSException oe) { |
|
|
|
throw new ServiceException(oe.getMessage()); |
|
|
|
throw new ServiceException(oe.getMessage()); |
|
|
|
} catch (ClientException ce) { |
|
|
|
} catch (ClientException ce) { |
|
|
@ -145,7 +171,7 @@ public class UploadUtils { |
|
|
|
|
|
|
|
|
|
|
|
/********************************************************************************************/ |
|
|
|
/********************************************************************************************/ |
|
|
|
|
|
|
|
|
|
|
|
public FileUploadDTO sliceUploads(FileUploadsRequestDTO param) { |
|
|
|
public ETag sliceUploads(FileUploadsRequestDTO param) { |
|
|
|
boolean isOk = true; |
|
|
|
boolean isOk = true; |
|
|
|
File root = new File(localtion + "/upload", param.getUUID()); |
|
|
|
File root = new File(localtion + "/upload", param.getUUID()); |
|
|
|
if (!root.exists() || !root.isDirectory()) { |
|
|
|
if (!root.exists() || !root.isDirectory()) { |
|
|
@ -163,40 +189,98 @@ public class UploadUtils { |
|
|
|
map.put(param.getChunk(), param.getUUID()); |
|
|
|
map.put(param.getChunk(), param.getUUID()); |
|
|
|
if (isOk) { |
|
|
|
if (isOk) { |
|
|
|
String sliceName = param.getChunk().toString(); |
|
|
|
String sliceName = param.getChunk().toString(); |
|
|
|
upload(root, sliceName, param); |
|
|
|
return upload(root, sliceName, param); |
|
|
|
} |
|
|
|
} |
|
|
|
return FileUploadDTO.builder().chunkMd5Info(map).build(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 文件检测 |
|
|
|
* 文件检测 |
|
|
|
* |
|
|
|
* { |
|
|
|
|
|
|
|
* "requestId": "63BD04BA1253C53638A9F751", |
|
|
|
|
|
|
|
* "bucketName": "zilber-public", |
|
|
|
|
|
|
|
* "key": "upload/3a3cd46a-d966-4258-a6e7-a3b318cbe133.jpg", |
|
|
|
|
|
|
|
* "uploadId": "70F44C7139E14DE0AB6BD7442F0F4AFE", |
|
|
|
|
|
|
|
* "maxParts": 100, |
|
|
|
|
|
|
|
* "partNumberMarker": 1, |
|
|
|
|
|
|
|
* "storageClass": "Standard", |
|
|
|
|
|
|
|
* "nextPartNumberMarker": 2, |
|
|
|
|
|
|
|
* "parts": [ |
|
|
|
|
|
|
|
* { |
|
|
|
|
|
|
|
* "partNumber": 2, |
|
|
|
|
|
|
|
* "lastModified": "2023-01-10T14:08:37.000+0800", |
|
|
|
|
|
|
|
* "size": 566470, |
|
|
|
|
|
|
|
* "etag": "21DE9DD8AE549FF2285DDC5E23B67867" |
|
|
|
|
|
|
|
* } |
|
|
|
|
|
|
|
* ], |
|
|
|
|
|
|
|
* "truncated": false |
|
|
|
|
|
|
|
* } |
|
|
|
* @param md5 md5 |
|
|
|
* @param md5 md5 |
|
|
|
* @param UUID UUID |
|
|
|
* @param UUID UUID |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @throws IOException |
|
|
|
* @throws IOException |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public FileCheckDTO fileCheck(String md5, String UUID) throws IOException { |
|
|
|
public PartList fileCheck(String md5, String UUID) throws IOException { |
|
|
|
FileCheckDTO fileCheckDTO = new FileCheckDTO(); |
|
|
|
PartList list = new PartList(); |
|
|
|
//根据文件md5查询文件是否存在,如果存在,则直接返回文件路径,否则返回块信息
|
|
|
|
List<ETag> eTags = new ArrayList<>(); |
|
|
|
//TODO
|
|
|
|
File root = new File(localtion + "/upload", UUID); |
|
|
|
Map<String, Object> objectMap = fileDao.listFileUrl(md5); |
|
|
|
if (!root.exists()) { |
|
|
|
if (objectMap != null) { |
|
|
|
return null; |
|
|
|
fileCheckDTO.setUrl(objectMap.get("url").toString()); |
|
|
|
} |
|
|
|
fileCheckDTO.setType(Integer.parseInt(objectMap.get("type").toString())); |
|
|
|
//获取总块数
|
|
|
|
fileCheckDTO.setExist(true); |
|
|
|
// File aConf = new File(root.getAbsolutePath(), UUID + ".conf");
|
|
|
|
return fileCheckDTO; |
|
|
|
// FileReader fr = new FileReader(aConf);
|
|
|
|
|
|
|
|
// char[] chunkCs = new char[(int) aConf.length()];
|
|
|
|
|
|
|
|
// fr.read(chunkCs);
|
|
|
|
|
|
|
|
Long chunks = chunks(root, UUID); |
|
|
|
|
|
|
|
list.setTotal(chunks); |
|
|
|
|
|
|
|
FilenameFilter filter = new FilenameFilter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean accept(File file, String s) { |
|
|
|
|
|
|
|
return s.endsWith(".conf") && !s.contains("-"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
String[] fileNames = root.list(filter); |
|
|
|
|
|
|
|
if (fileNames == null) { |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<String> fileNameList = Arrays.asList(fileNames) |
|
|
|
|
|
|
|
.stream().distinct().collect(Collectors.toList()); |
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(fileNameList)) { |
|
|
|
|
|
|
|
for (String fileName : fileNameList) { |
|
|
|
|
|
|
|
String prefix = fileName.split("\\.")[0]; |
|
|
|
|
|
|
|
ETag tag = new ETag(); |
|
|
|
|
|
|
|
tag.setPartSize(new File(root.getAbsolutePath(), fileName).getTotalSpace()); |
|
|
|
|
|
|
|
tag.setPartNumber(Integer.parseInt(prefix)); |
|
|
|
|
|
|
|
eTags.add(tag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
list.setTags(eTags); |
|
|
|
|
|
|
|
return list; |
|
|
|
} |
|
|
|
} |
|
|
|
//不存在
|
|
|
|
return list; |
|
|
|
return checkUnexist(UUID, fileCheckDTO); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
|
|
|
|
* { |
|
|
|
|
|
|
|
* "partNumber": 2, |
|
|
|
|
|
|
|
* "partSize": 566470, |
|
|
|
|
|
|
|
* "partCRC": 997575365254752264, |
|
|
|
|
|
|
|
* "etag": "21DE9DD8AE549FF2285DDC5E23B67867" |
|
|
|
|
|
|
|
* } |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
* @author liuao |
|
|
|
* @author liuao |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public boolean upload(File root, String name, FileUploadsRequestDTO param) { |
|
|
|
public ETag upload(File root, String name, FileUploadsRequestDTO param) { |
|
|
|
|
|
|
|
|
|
|
|
//保存最大块数
|
|
|
|
//保存最大块数
|
|
|
|
|
|
|
|
ETag tag = new ETag(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
tag.setPartNumber(param.getChunk().intValue()); |
|
|
|
|
|
|
|
tag.setPartSize(param.getChunkSize()); |
|
|
|
|
|
|
|
tag.setETag(MD5Encoder.encode(param.getFile().getBytes())); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
File aConfFile = new File(root.getAbsolutePath(), param.getUUID() + ".conf"); |
|
|
|
File aConfFile = new File(root.getAbsolutePath(), param.getUUID() + ".conf"); |
|
|
|
File sizeFile = new File(root.getAbsolutePath(), "size.conf"); |
|
|
|
File sizeFile = new File(root.getAbsolutePath(), "size.conf"); |
|
|
|
if (!aConfFile.exists()) { |
|
|
|
if (!aConfFile.exists()) { |
|
|
@ -229,7 +313,7 @@ public class UploadUtils { |
|
|
|
accessTmpFile = new RandomAccessFile(tmpFile, "rw"); |
|
|
|
accessTmpFile = new RandomAccessFile(tmpFile, "rw"); |
|
|
|
accessTmpFile.write(param.getFile().getBytes()); |
|
|
|
accessTmpFile.write(param.getFile().getBytes()); |
|
|
|
log.info(param.getUUID() + "文件块" + param.getChunk() + "上传完成"); |
|
|
|
log.info(param.getUUID() + "文件块" + param.getChunk() + "上传完成"); |
|
|
|
return true; |
|
|
|
return tag; |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
log.error(e.getMessage(), e); |
|
|
|
if (tmpConfFile.exists()) { |
|
|
|
if (tmpConfFile.exists()) { |
|
|
@ -251,28 +335,40 @@ public class UploadUtils { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public FileCheckDTO compose(String UUID, String type, String md5) { |
|
|
|
/** |
|
|
|
|
|
|
|
* { |
|
|
|
|
|
|
|
* "requestId": "63BD05267FFDC23733FE3B95", |
|
|
|
|
|
|
|
* "clientCRC": -1453100417491105764, |
|
|
|
|
|
|
|
* "serverCRC": -1453100417491105764, |
|
|
|
|
|
|
|
* "bucketName": "zilber-public", |
|
|
|
|
|
|
|
* "key": "upload/3a3cd46a-d966-4258-a6e7-a3b318cbe133.jpg", |
|
|
|
|
|
|
|
* "location": "http://zilber-public.oss-cn-beijing.aliyuncs.com/upload/3a3cd46a-d966-4258-a6e7-a3b318cbe133.jpg", |
|
|
|
|
|
|
|
* "etag": "8DDB27878F7D65485F8E83A2B5E7F11E-2" |
|
|
|
|
|
|
|
* } |
|
|
|
|
|
|
|
* @param UUID |
|
|
|
|
|
|
|
* @param type |
|
|
|
|
|
|
|
* @param md5 |
|
|
|
|
|
|
|
* @return |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public Complete compose(String UUID, String type, String md5) { |
|
|
|
long startTime = 0, endTime = 0; |
|
|
|
long startTime = 0, endTime = 0; |
|
|
|
log.info(String.valueOf(System.currentTimeMillis())); |
|
|
|
log.info(String.valueOf(System.currentTimeMillis())); |
|
|
|
FileCheckDTO fileCheckDTO = new FileCheckDTO(); |
|
|
|
Complete complete = new Complete(); |
|
|
|
File root = new File(localtion + "/upload", UUID); |
|
|
|
File root = new File(localtion + "/upload", UUID); |
|
|
|
File targetFile = new File(localtion + "/upload", UUID + type); |
|
|
|
File targetFile = new File(localtion + "/upload", UUID + type); |
|
|
|
Long chunks = chunks(root, UUID); |
|
|
|
Long chunks = chunks(root, UUID); |
|
|
|
File[] fileNames = root.listFiles((file1, s1) -> !s1.endsWith(".conf")); |
|
|
|
File[] fileNames = root.listFiles((file1, s1) -> !s1.endsWith(".conf")); |
|
|
|
if (fileNames == null || fileNames.length < chunks) { |
|
|
|
if (fileNames == null || fileNames.length < chunks) { |
|
|
|
return checkUnexist(UUID, new FileCheckDTO()); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
List<File> collect = Arrays.asList(fileNames) |
|
|
|
List<File> collect = Arrays.asList(fileNames) |
|
|
|
.stream().sorted(new Comparator<File>() { |
|
|
|
.stream().sorted((file, t1) -> { |
|
|
|
@Override |
|
|
|
|
|
|
|
public int compare(File file, File t1) { |
|
|
|
|
|
|
|
int f = Integer.parseInt(file.getName()); |
|
|
|
int f = Integer.parseInt(file.getName()); |
|
|
|
int t = Integer.parseInt(t1.getName()); |
|
|
|
int t = Integer.parseInt(t1.getName()); |
|
|
|
return f < t ? -1 : 1; |
|
|
|
return f < t ? -1 : 1; |
|
|
|
} |
|
|
|
|
|
|
|
}).collect(Collectors.toList()); |
|
|
|
}).collect(Collectors.toList()); |
|
|
|
FileOutputStream fos; |
|
|
|
FileOutputStream fos; |
|
|
|
FileInputStream fis; |
|
|
|
FileInputStream fis; |
|
|
@ -304,54 +400,11 @@ public class UploadUtils { |
|
|
|
String currentDir = StringUtils.substring(localtion + "/upload", dirLastIndex); |
|
|
|
String currentDir = StringUtils.substring(localtion + "/upload", dirLastIndex); |
|
|
|
String pathFileName = "/profile" + "/" + currentDir + "/" + UUID + type; |
|
|
|
String pathFileName = "/profile" + "/" + currentDir + "/" + UUID + type; |
|
|
|
Integer t = getFileType(type); |
|
|
|
Integer t = getFileType(type); |
|
|
|
fileDao.addFile(md5, pathFileName, t); |
|
|
|
fileDao.addFile(md5, pathFileName, t, 1); |
|
|
|
fileCheckDTO.setUrl(pathFileName); |
|
|
|
complete.setLocation(pathFileName); |
|
|
|
fileCheckDTO.setType(t); |
|
|
|
complete.setType(t); |
|
|
|
fileCheckDTO.setExist(true); |
|
|
|
|
|
|
|
clearTempFiles(root); |
|
|
|
clearTempFiles(root); |
|
|
|
return fileCheckDTO; |
|
|
|
return complete; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private FileCheckDTO checkUnexist(String UUID, FileCheckDTO fileCheckDTO) { |
|
|
|
|
|
|
|
//不存在
|
|
|
|
|
|
|
|
fileCheckDTO.setExist(false); |
|
|
|
|
|
|
|
File root = new File(localtion + "/upload", UUID); |
|
|
|
|
|
|
|
if (!root.exists()) { |
|
|
|
|
|
|
|
fileCheckDTO.setUploaded(false); |
|
|
|
|
|
|
|
return fileCheckDTO; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
//获取总块数
|
|
|
|
|
|
|
|
// File aConf = new File(root.getAbsolutePath(), UUID + ".conf");
|
|
|
|
|
|
|
|
// FileReader fr = new FileReader(aConf);
|
|
|
|
|
|
|
|
// char[] chunkCs = new char[(int) aConf.length()];
|
|
|
|
|
|
|
|
// fr.read(chunkCs);
|
|
|
|
|
|
|
|
Long chunks = chunks(root, UUID); |
|
|
|
|
|
|
|
FilenameFilter filter = new FilenameFilter() { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public boolean accept(File file, String s) { |
|
|
|
|
|
|
|
return s.endsWith(".conf") && !s.contains("-"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
String[] fileNames = root.list(filter); |
|
|
|
|
|
|
|
if (fileNames == null) { |
|
|
|
|
|
|
|
fileCheckDTO.setUploaded(false); |
|
|
|
|
|
|
|
return fileCheckDTO; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
List<String> fileNameList = Arrays.asList(fileNames) |
|
|
|
|
|
|
|
.stream().distinct().collect(Collectors.toList()); |
|
|
|
|
|
|
|
Map<String, Boolean> chunkInfo = new HashMap<>(); |
|
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(fileNameList)) { |
|
|
|
|
|
|
|
for (String fileName : fileNameList) { |
|
|
|
|
|
|
|
String prefix = fileName.split("\\.")[0]; |
|
|
|
|
|
|
|
chunkInfo.put(prefix, true); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fileCheckDTO.setChunkInfo(chunkInfo); |
|
|
|
|
|
|
|
fileCheckDTO.setUploaded(true); |
|
|
|
|
|
|
|
fileCheckDTO.setChunks(chunks); |
|
|
|
|
|
|
|
return fileCheckDTO; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
fileCheckDTO.setUploaded(false); |
|
|
|
|
|
|
|
return fileCheckDTO; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private Long chunks(File root, String UUID) { |
|
|
|
private Long chunks(File root, String UUID) { |
|
|
@ -396,7 +449,7 @@ public class UploadUtils { |
|
|
|
* @param module 目标文件夹 |
|
|
|
* @param module 目标文件夹 |
|
|
|
* @return 文件唯一标识 |
|
|
|
* @return 文件唯一标识 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public OssPartUpload InitiateMultipartUpload(String fileName, @Nullable String module, OssProperties provided) { |
|
|
|
public OssPartUpload InitiateMultipartUpload(String fileName, @Nullable String module,@Nullable OssProperties provided) { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -404,6 +457,7 @@ public class UploadUtils { |
|
|
|
String accessKeyId = ""; |
|
|
|
String accessKeyId = ""; |
|
|
|
String accessKeySecret = ""; |
|
|
|
String accessKeySecret = ""; |
|
|
|
String bucketName = ""; |
|
|
|
String bucketName = ""; |
|
|
|
|
|
|
|
if ( provided != null ){ |
|
|
|
if ( StringUtils.isNotBlank(provided.getEndpoint())){ |
|
|
|
if ( StringUtils.isNotBlank(provided.getEndpoint())){ |
|
|
|
endpoint = provided.getEndpoint(); |
|
|
|
endpoint = provided.getEndpoint(); |
|
|
|
}else { |
|
|
|
}else { |
|
|
@ -424,10 +478,17 @@ public class UploadUtils { |
|
|
|
}else { |
|
|
|
}else { |
|
|
|
bucketName = ossProperties.getBucketName(); |
|
|
|
bucketName = ossProperties.getBucketName(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
endpoint = ossProperties.getEndpoint(); |
|
|
|
|
|
|
|
accessKeyId = ossProperties.getKeyId(); |
|
|
|
|
|
|
|
accessKeySecret = ossProperties.getKeySecret(); |
|
|
|
|
|
|
|
bucketName = ossProperties.getBucketName(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ( StringUtils.isBlank(module) ){ |
|
|
|
if ( StringUtils.isBlank(module) ){ |
|
|
|
module = ossProperties.getModule(); |
|
|
|
module = ossProperties.getModule(); |
|
|
|
} |
|
|
|
} |
|
|
|
String extension = fileName.substring(fileName.lastIndexOf(".")); |
|
|
|
String extension = "." + FilenameUtils.getExtension(fileName); |
|
|
|
String objectName = module + "/" + UUID.randomUUID().toString() + extension; |
|
|
|
String objectName = module + "/" + UUID.randomUUID().toString() + extension; |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
|
|
|
|
|
|
|
@ -449,8 +510,8 @@ public class UploadUtils { |
|
|
|
* @param upload 分片信息 |
|
|
|
* @param upload 分片信息 |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public UploadPartResult uploadPart(OssPartUpload upload) { |
|
|
|
public PartETag uploadPart(OssPartUpload upload) { |
|
|
|
ValidatorUtils.validateEntity(upload, DefaultGroup.class); |
|
|
|
//ValidatorUtils.validateEntity(upload);
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -493,7 +554,7 @@ public class UploadUtils { |
|
|
|
// 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出此范围,OSS将返回InvalidArgument错误码。
|
|
|
|
// 设置分片号。每一个上传的分片都有一个分片号,取值范围是1~10000,如果超出此范围,OSS将返回InvalidArgument错误码。
|
|
|
|
uploadPartRequest.setPartNumber(upload.getPartNumber()); |
|
|
|
uploadPartRequest.setPartNumber(upload.getPartNumber()); |
|
|
|
// 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
|
|
|
|
// 每个分片不需要按顺序上传,甚至可以在不同客户端上传,OSS会按照分片号排序组成完整的文件。
|
|
|
|
return ossClient.uploadPart(uploadPartRequest); |
|
|
|
return ossClient.uploadPart(uploadPartRequest).getPartETag(); |
|
|
|
} catch (IOException e) { |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
@ -506,7 +567,7 @@ public class UploadUtils { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public PartListing list(OssListParts parts) { |
|
|
|
public PartListing list(OssListParts parts) { |
|
|
|
ValidatorUtils.validateEntity(parts, DefaultGroup.class); |
|
|
|
//ValidatorUtils.validateEntity(parts);
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -558,7 +619,7 @@ public class UploadUtils { |
|
|
|
* @return |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public CompleteMultipartUploadResult completeUpload(OssComplete complete) { |
|
|
|
public CompleteMultipartUploadResult completeUpload(OssComplete complete) { |
|
|
|
ValidatorUtils.validateEntity(complete, DefaultGroup.class); |
|
|
|
//ValidatorUtils.validateEntity(complete, DefaultGroup.class);
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -586,7 +647,13 @@ public class UploadUtils { |
|
|
|
}else { |
|
|
|
}else { |
|
|
|
bucketName = ossProperties.getBucketName(); |
|
|
|
bucketName = ossProperties.getBucketName(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
List<PartETag> partETags = new ArrayList<>(); |
|
|
|
|
|
|
|
if ( CollectionUtils.isNotEmpty(complete.getTags())){ |
|
|
|
|
|
|
|
for ( ETag e : complete.getTags()){ |
|
|
|
|
|
|
|
PartETag partETag = new PartETag(e.getPartNumber(), e.getETag(), e.getPartSize(), e.getPartCRC()); |
|
|
|
|
|
|
|
partETags.add(partETag); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// 创建OSSClient实例。
|
|
|
|
// 创建OSSClient实例。
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); |
|
|
|
// 在执行完成分片上传操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
|
|
|
|
// 在执行完成分片上传操作时,需要提供所有有效的partETags。OSS收到提交的partETags后,会逐一验证每个分片的有效性。当所有的数据分片验证通过后,OSS将把这些分片组合成一个完整的文件。
|
|
|
@ -594,7 +661,7 @@ public class UploadUtils { |
|
|
|
new CompleteMultipartUploadRequest(bucketName, |
|
|
|
new CompleteMultipartUploadRequest(bucketName, |
|
|
|
complete.getKey(), |
|
|
|
complete.getKey(), |
|
|
|
complete.getUploadId(), |
|
|
|
complete.getUploadId(), |
|
|
|
complete.getTags()); |
|
|
|
partETags); |
|
|
|
// 完成分片上传。
|
|
|
|
// 完成分片上传。
|
|
|
|
return ossClient.completeMultipartUpload(completeMultipartUploadRequest); |
|
|
|
return ossClient.completeMultipartUpload(completeMultipartUploadRequest); |
|
|
|
} |
|
|
|
} |
|
|
@ -604,7 +671,7 @@ public class UploadUtils { |
|
|
|
* @param cancel |
|
|
|
* @param cancel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void abortUpload(OssCancel cancel) { |
|
|
|
public void abortUpload(OssCancel cancel) { |
|
|
|
ValidatorUtils.validateEntity(cancel, DefaultGroup.class); |
|
|
|
//ValidatorUtils.validateEntity(cancel, DefaultGroup.class);
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 获取oss的属性 |
|
|
|
* 获取oss的属性 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|