|
@@ -2,9 +2,11 @@ package cn.reghao.dfs.store.service;
|
|
|
|
|
|
|
|
import cn.reghao.dfs.store.cache.LocalCache;
|
|
import cn.reghao.dfs.store.cache.LocalCache;
|
|
|
import cn.reghao.dfs.store.db.mapper.DataBlockMapper;
|
|
import cn.reghao.dfs.store.db.mapper.DataBlockMapper;
|
|
|
|
|
+import cn.reghao.dfs.store.db.mapper.FileContentMapper;
|
|
|
import cn.reghao.dfs.store.db.mapper.FileMetaMapper;
|
|
import cn.reghao.dfs.store.db.mapper.FileMetaMapper;
|
|
|
import cn.reghao.dfs.store.model.po.ContentRange;
|
|
import cn.reghao.dfs.store.model.po.ContentRange;
|
|
|
import cn.reghao.dfs.store.model.po.DataBlock;
|
|
import cn.reghao.dfs.store.model.po.DataBlock;
|
|
|
|
|
+import cn.reghao.dfs.store.model.po.FileContent;
|
|
|
import cn.reghao.dfs.store.model.po.FileMeta;
|
|
import cn.reghao.dfs.store.model.po.FileMeta;
|
|
|
import cn.reghao.dfs.store.model.vo.ObjectMeta;
|
|
import cn.reghao.dfs.store.model.vo.ObjectMeta;
|
|
|
import cn.reghao.dfs.store.redis.ds.RedisStringObj;
|
|
import cn.reghao.dfs.store.redis.ds.RedisStringObj;
|
|
@@ -14,6 +16,7 @@ import cn.reghao.jutil.web.ServletUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.tika.Tika;
|
|
import org.apache.tika.Tika;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.StringUtils;
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
@@ -38,6 +41,7 @@ public class ObjectBasicService {
|
|
|
private final IdGenerator objectIdGenerator;
|
|
private final IdGenerator objectIdGenerator;
|
|
|
private final IdGenerator blockIdGenerator;
|
|
private final IdGenerator blockIdGenerator;
|
|
|
private final FileMetaMapper fileMetaMapper;
|
|
private final FileMetaMapper fileMetaMapper;
|
|
|
|
|
+ private FileContentMapper fileContentMapper;
|
|
|
private final DataBlockMapper dataBlockMapper;
|
|
private final DataBlockMapper dataBlockMapper;
|
|
|
// 10MiB
|
|
// 10MiB
|
|
|
private final int partLength = 1024*1024*10;
|
|
private final int partLength = 1024*1024*10;
|
|
@@ -48,13 +52,15 @@ public class ObjectBasicService {
|
|
|
private final RedisStringObj redisStringObj;
|
|
private final RedisStringObj redisStringObj;
|
|
|
private Tika tika = new Tika();
|
|
private Tika tika = new Tika();
|
|
|
|
|
|
|
|
- public ObjectBasicService(FileMetaMapper fileMetaMapper, DataBlockMapper dataBlockMapper,
|
|
|
|
|
|
|
+ public ObjectBasicService(FileMetaMapper fileMetaMapper, FileContentMapper fileContentMapper,
|
|
|
|
|
+ DataBlockMapper dataBlockMapper,
|
|
|
FileStoreService fileStoreService, FileUrlService fileUrlService,
|
|
FileStoreService fileStoreService, FileUrlService fileUrlService,
|
|
|
FileTypeService fileTypeService, LocalCache localCache,
|
|
FileTypeService fileTypeService, LocalCache localCache,
|
|
|
RedisStringObj redisStringObj) {
|
|
RedisStringObj redisStringObj) {
|
|
|
this.objectIdGenerator = new IdGenerator(32, "object-id");
|
|
this.objectIdGenerator = new IdGenerator(32, "object-id");
|
|
|
this.blockIdGenerator = new IdGenerator(32, "block-id");
|
|
this.blockIdGenerator = new IdGenerator(32, "block-id");
|
|
|
this.fileMetaMapper = fileMetaMapper;
|
|
this.fileMetaMapper = fileMetaMapper;
|
|
|
|
|
+ this.fileContentMapper = fileContentMapper;
|
|
|
this.dataBlockMapper = dataBlockMapper;
|
|
this.dataBlockMapper = dataBlockMapper;
|
|
|
this.fileStoreService = fileStoreService;
|
|
this.fileStoreService = fileStoreService;
|
|
|
this.fileUrlService = fileUrlService;
|
|
this.fileUrlService = fileUrlService;
|
|
@@ -63,6 +69,7 @@ public class ObjectBasicService {
|
|
|
this.redisStringObj = redisStringObj;
|
|
this.redisStringObj = redisStringObj;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void putObject(String objectName, File file, String sha256sum) throws Exception {
|
|
public void putObject(String objectName, File file, String sha256sum) throws Exception {
|
|
|
String[] names = objectName.split("/");
|
|
String[] names = objectName.split("/");
|
|
|
String filename = names[names.length-1];
|
|
String filename = names[names.length-1];
|
|
@@ -76,47 +83,62 @@ public class ObjectBasicService {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- log.info("暂未实现 PUT 存储");
|
|
|
|
|
|
|
+ log.info("更新对象操作暂未实现");
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ addParent(objectName);
|
|
|
List<FileMeta> list = fileMetaMapper.findBySha256sum(sha256sum);
|
|
List<FileMeta> list = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
if (list.isEmpty()) {
|
|
if (list.isEmpty()) {
|
|
|
String objectId = objectIdGenerator.stringId();
|
|
String objectId = objectIdGenerator.stringId();
|
|
|
|
|
+ String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
+ FileContent fileContent = new FileContent(contentId, objectId);
|
|
|
long len = file.length();
|
|
long len = file.length();
|
|
|
List<DataBlock> blocks = store(objectId, len, file);
|
|
List<DataBlock> blocks = store(objectId, len, file);
|
|
|
-
|
|
|
|
|
int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
|
FileMeta fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
FileMeta fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
|
- dataBlockMapper.saveAll(blocks);
|
|
|
|
|
|
|
+
|
|
|
fileMetaMapper.save(fileMeta);
|
|
fileMetaMapper.save(fileMeta);
|
|
|
|
|
+ fileContentMapper.save(fileContent);
|
|
|
|
|
+ dataBlockMapper.saveAll(blocks);
|
|
|
} else {
|
|
} else {
|
|
|
FileMeta fileMeta = list.get(0);
|
|
FileMeta fileMeta = list.get(0);
|
|
|
- List<DataBlock> blocks = dataBlockMapper.findByObjectId(fileMeta.getObjectId());
|
|
|
|
|
|
|
+ String contentId = fileContentMapper.findContentId(fileMeta.getObjectId());
|
|
|
|
|
+ String objectId = UUID.randomUUID().toString().replace("-", "");
|
|
|
|
|
+ FileContent fileContent = new FileContent(contentId, objectId);
|
|
|
|
|
+ FileMeta fileMeta1 = new FileMeta(objectName, objectId, filename, fileMeta);
|
|
|
|
|
|
|
|
- FileMeta fileMeta1 = new FileMeta(objectName, filename, fileMeta);
|
|
|
|
|
fileMetaMapper.save(fileMeta1);
|
|
fileMetaMapper.save(fileMeta1);
|
|
|
|
|
+ fileContentMapper.save(fileContent);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void postObject(String objectName, long len, String contentType, InputStream inputStream) throws Exception {
|
|
|
|
|
- String objectId = objectIdGenerator.stringId();
|
|
|
|
|
- List<DataBlock> list = store(objectId, len, inputStream);
|
|
|
|
|
- String sha256sum = DigestUtil.sha256sum(new FileInputStream(list.get(0).getAbsolutePath()));
|
|
|
|
|
- log.info("{} sha256sum {}", objectName, sha256sum);
|
|
|
|
|
- String[] names = objectName.split("/");
|
|
|
|
|
- String filename = names[names.length-1];
|
|
|
|
|
|
|
+ private void addParent(String objectName) {
|
|
|
|
|
+ List<String> list = getParent(objectName);
|
|
|
|
|
+ List<FileMeta> fileMetas = new ArrayList<>();
|
|
|
|
|
+ list.forEach(parentName -> {
|
|
|
|
|
+ FileMeta fileMeta = fileMetaMapper.findByObjectName(parentName);
|
|
|
|
|
+ if (fileMeta == null) {
|
|
|
|
|
+ fileMetas.add(new FileMeta(parentName, 2));
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
|
|
|
- /*FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
|
|
- if (fileMeta == null) {
|
|
|
|
|
- int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
|
|
|
- fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
|
|
|
- dataBlockMapper.saveAll(list);
|
|
|
|
|
- fileMetaMapper.save(fileMeta);
|
|
|
|
|
- } else {
|
|
|
|
|
- FileMeta fileMeta1 = new FileMeta(objectName, filename, fileMeta);
|
|
|
|
|
- fileMetaMapper.save(fileMeta1);
|
|
|
|
|
- }*/
|
|
|
|
|
|
|
+ if (!fileMetas.isEmpty()) {
|
|
|
|
|
+ fileMetaMapper.saveAll(fileMetas);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private List<String> getParent(String objectName) {
|
|
|
|
|
+ String[] arr = objectName.split("/");
|
|
|
|
|
+ List<String> list = new ArrayList<>();
|
|
|
|
|
+ list.add(arr[0] + "/");
|
|
|
|
|
+ for (int i = 1; i < arr.length-1; i++) {
|
|
|
|
|
+ list.add(list.get(i-1) + arr[i] + "/");
|
|
|
|
|
+ }
|
|
|
|
|
+ return list;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public void postObject(String objectName, long len, String contentType, InputStream inputStream) throws Exception {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private List<DataBlock> store(String objectId, long len, File file) throws IOException {
|
|
private List<DataBlock> store(String objectId, long len, File file) throws IOException {
|