|
|
@@ -9,15 +9,11 @@ import cn.reghao.dfs.store.model.po.FileContent;
|
|
|
import cn.reghao.dfs.store.model.po.FileMeta;
|
|
|
import cn.reghao.dfs.store.redis.ds.RedisStringObj;
|
|
|
import cn.reghao.jutil.jdk.shell.Shell;
|
|
|
-import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.nio.channels.FileChannel;
|
|
|
-import java.nio.channels.WritableByteChannel;
|
|
|
-import java.nio.file.Files;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.UUID;
|
|
|
@@ -29,30 +25,23 @@ import java.util.UUID;
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
public class PutObjectService {
|
|
|
- private final IdGenerator objectIdGenerator;
|
|
|
- private final IdGenerator blockIdGenerator;
|
|
|
private final FileMetaMapper fileMetaMapper;
|
|
|
- private FileContentMapper fileContentMapper;
|
|
|
+ private final FileContentMapper fileContentMapper;
|
|
|
private final DataBlockMapper dataBlockMapper;
|
|
|
- private final FileStoreService fileStoreService;
|
|
|
private final FileUrlService fileUrlService;
|
|
|
- private final FileTypeService fileTypeService;
|
|
|
+ private final FileStoreService fileStoreService;
|
|
|
private final LocalCache localCache;
|
|
|
private final RedisStringObj redisStringObj;
|
|
|
|
|
|
public PutObjectService(FileMetaMapper fileMetaMapper, FileContentMapper fileContentMapper,
|
|
|
DataBlockMapper dataBlockMapper,
|
|
|
FileStoreService fileStoreService, FileUrlService fileUrlService,
|
|
|
- FileTypeService fileTypeService, LocalCache localCache,
|
|
|
- RedisStringObj redisStringObj) {
|
|
|
- this.objectIdGenerator = new IdGenerator(32, "object-id");
|
|
|
- this.blockIdGenerator = new IdGenerator(32, "block-id");
|
|
|
+ LocalCache localCache, RedisStringObj redisStringObj) {
|
|
|
this.fileMetaMapper = fileMetaMapper;
|
|
|
this.fileContentMapper = fileContentMapper;
|
|
|
this.dataBlockMapper = dataBlockMapper;
|
|
|
this.fileStoreService = fileStoreService;
|
|
|
this.fileUrlService = fileUrlService;
|
|
|
- this.fileTypeService = fileTypeService;
|
|
|
this.localCache = localCache;
|
|
|
this.redisStringObj = redisStringObj;
|
|
|
}
|
|
|
@@ -76,21 +65,21 @@ public class PutObjectService {
|
|
|
}
|
|
|
|
|
|
addParent(objectName);
|
|
|
- List<FileMeta> list = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
- if (list.isEmpty()) {
|
|
|
- String objectId = objectIdGenerator.stringId();
|
|
|
+ FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
+ if (fileMeta == null) {
|
|
|
+ String objectId = UUID.randomUUID().toString().replace("-", "");
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
FileContent fileContent = new FileContent(contentId, objectId);
|
|
|
- long len = file.length();
|
|
|
- List<DataBlock> blocks = store(contentId, len, file);
|
|
|
- int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
|
- FileMeta fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
|
+ long size = file.length();
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ List<DataBlock> blocks = store(contentId, size, fis);
|
|
|
+ file.delete();
|
|
|
+ fileMeta = new FileMeta(objectName, objectId, filename, size, contentType, sha256sum);
|
|
|
|
|
|
fileMetaMapper.save(fileMeta);
|
|
|
fileContentMapper.save(fileContent);
|
|
|
dataBlockMapper.saveAll(blocks);
|
|
|
} else {
|
|
|
- FileMeta fileMeta = list.get(0);
|
|
|
String contentId = fileContentMapper.findContentId(fileMeta.getObjectId());
|
|
|
String objectId = UUID.randomUUID().toString().replace("-", "");
|
|
|
FileContent fileContent = new FileContent(contentId, objectId);
|
|
|
@@ -112,7 +101,7 @@ public class PutObjectService {
|
|
|
list.forEach(parentName -> {
|
|
|
FileMeta fileMeta = fileMetaMapper.findByObjectName(parentName);
|
|
|
if (fileMeta == null) {
|
|
|
- fileMetas.add(new FileMeta(parentName, 2));
|
|
|
+ fileMetas.add(new FileMeta(parentName));
|
|
|
}
|
|
|
});
|
|
|
|
|
|
@@ -131,33 +120,13 @@ public class PutObjectService {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- public void postObject(String objectName, long len, String contentType, InputStream inputStream) throws Exception {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- private List<DataBlock> store(String contentId, long len, File file) throws IOException {
|
|
|
- FileInputStream fis = new FileInputStream(file);
|
|
|
- FileChannel inputChannel = fis.getChannel();
|
|
|
-
|
|
|
+ private List<DataBlock> store(String contentId, long size, InputStream inputStream) throws IOException {
|
|
|
List<DataBlock> list = new ArrayList<>();
|
|
|
- String absolutePath = fileUrlService.genFilePath(len, contentId, "dat");
|
|
|
- FileOutputStream fos = new FileOutputStream(absolutePath);
|
|
|
- WritableByteChannel targetChannel = fos.getChannel();
|
|
|
- inputChannel.transferTo(0, inputChannel.size(), targetChannel);
|
|
|
- Files.delete(file.toPath());
|
|
|
-
|
|
|
- String blockId = UUID.randomUUID().toString();
|
|
|
- list.add(new DataBlock(contentId, 0, blockId, absolutePath));
|
|
|
- return list;
|
|
|
- }
|
|
|
-
|
|
|
- private List<DataBlock> store(String objectId, long len, InputStream inputStream) throws IOException {
|
|
|
- List<DataBlock> list = new ArrayList<>();
|
|
|
- String absolutePath = fileUrlService.genFilePath(len, objectId, "dat");
|
|
|
+ String absolutePath = fileUrlService.genFilePath(size, contentId, "dat");
|
|
|
fileStoreService.saveFile(absolutePath, inputStream);
|
|
|
|
|
|
String blockId = UUID.randomUUID().toString();
|
|
|
- list.add(new DataBlock(objectId, 0, blockId, absolutePath));
|
|
|
+ list.add(new DataBlock(contentId, 0, blockId, absolutePath));
|
|
|
return list;
|
|
|
}
|
|
|
|