|
|
@@ -1,10 +1,12 @@
|
|
|
package cn.reghao.dfs.store.service;
|
|
|
|
|
|
+import cn.reghao.dfs.store.config.OssProperties;
|
|
|
import cn.reghao.dfs.store.db.repository.ObjectRepository;
|
|
|
import cn.reghao.dfs.store.model.po.DataBlock;
|
|
|
import cn.reghao.dfs.store.model.po.FileMeta;
|
|
|
import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.jutil.jdk.shell.Shell;
|
|
|
+import cn.reghao.oss.common.UploadFileRet;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -23,17 +25,23 @@ import java.util.UUID;
|
|
|
public class PutObjectService {
|
|
|
private final ObjectRepository objectRepository;
|
|
|
private final FileStoreService fileStoreService;
|
|
|
+ private final String domain;
|
|
|
|
|
|
- public PutObjectService(ObjectRepository objectRepository, FileStoreService fileStoreService) {
|
|
|
+ public PutObjectService(ObjectRepository objectRepository, FileStoreService fileStoreService, OssProperties ossProperties) {
|
|
|
this.objectRepository = objectRepository;
|
|
|
this.fileStoreService = fileStoreService;
|
|
|
+ this.domain = ossProperties.getDomain();
|
|
|
}
|
|
|
|
|
|
- public void putObject(String pid, String objectName, String contentId, File savedFile, String originalFilename, String sha256sum) {
|
|
|
+ public UploadFileRet putObject(String pid, String objectName, String contentId, File savedFile, String originalFilename, String sha256sum) {
|
|
|
FileMeta fileMeta = objectRepository.getBySha256sum(sha256sum);
|
|
|
if (fileMeta != null) {
|
|
|
copyObject(objectName, originalFilename, fileMeta);
|
|
|
FileUtils.deleteQuietly(savedFile);
|
|
|
+
|
|
|
+ String objectId = objectRepository.getByObjectName(objectName).getObjectId();
|
|
|
+ String url = String.format("https://%s/%s", domain, objectName);
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
} else {
|
|
|
String savedPath = savedFile.getAbsolutePath();
|
|
|
long size = savedFile.length();
|
|
|
@@ -45,6 +53,9 @@ public class PutObjectService {
|
|
|
String blockId = UUID.randomUUID().toString();
|
|
|
List<DataBlock> list = List.of(new DataBlock(contentId, 0, blockId, savedPath));
|
|
|
objectRepository.saveObject(fileMeta, list);
|
|
|
+
|
|
|
+ String url = String.format("https://%s/%s", domain, objectName);
|
|
|
+ return new UploadFileRet(objectId, url);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -80,25 +91,31 @@ public class PutObjectService {
|
|
|
}
|
|
|
|
|
|
private void addParent(String objectName) {
|
|
|
- List<String> list = getParent(objectName);
|
|
|
+ List<String> list = getSortedParent(objectName);
|
|
|
List<FileMeta> fileMetas = new ArrayList<>();
|
|
|
- list.forEach(parentName -> {
|
|
|
+ for (int i = 0; i < list.size(); i++) {
|
|
|
+ String parentName = list.get(i);
|
|
|
FileMeta fileMeta = objectRepository.getByObjectName(parentName);
|
|
|
if (fileMeta == null) {
|
|
|
- String pid = "0";
|
|
|
- int index = parentName.lastIndexOf("/");
|
|
|
+ String pid;
|
|
|
+ if (i == 0) {
|
|
|
+ pid = "0";
|
|
|
+ } else {
|
|
|
+ pid = objectRepository.getByObjectName(list.get(i-1)).getObjectId();
|
|
|
+ }
|
|
|
String objectId = UUID.randomUUID().toString().replace("-", "");
|
|
|
- String filename = parentName.substring(index);
|
|
|
- fileMetas.add(new FileMeta(pid, objectId, objectName, filename));
|
|
|
+ String[] names = parentName.split("/");
|
|
|
+ String filename = names[names.length-1];
|
|
|
+ fileMetas.add(new FileMeta(parentName, objectId, filename, pid));
|
|
|
}
|
|
|
- });
|
|
|
+ }
|
|
|
|
|
|
if (!fileMetas.isEmpty()) {
|
|
|
objectRepository.saveFileMetas(fileMetas);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private List<String> getParent(String objectName) {
|
|
|
+ private List<String> getSortedParent(String objectName) {
|
|
|
String[] arr = objectName.split("/");
|
|
|
List<String> list = new ArrayList<>();
|
|
|
list.add(arr[0] + "/");
|