|
|
@@ -8,12 +8,15 @@ import cn.reghao.dfs.store.model.po.FileMeta;
|
|
|
import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import cn.reghao.jutil.web.ServletUtil;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.*;
|
|
|
import java.net.URLEncoder;
|
|
|
+import java.nio.channels.FileChannel;
|
|
|
+import java.nio.channels.WritableByteChannel;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
@@ -23,6 +26,7 @@ import java.util.UUID;
|
|
|
* @author reghao
|
|
|
* @date 2022-11-23 09:40:18
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class ObjectBasicService {
|
|
|
private final IdGenerator objectIdGenerator;
|
|
|
@@ -49,22 +53,60 @@ public class ObjectBasicService {
|
|
|
this.fileTypeService = fileTypeService;
|
|
|
}
|
|
|
|
|
|
+ public void putObject(String objectName, File file, String contentType, String sha256sum) throws Exception {
|
|
|
+ String objectId = objectIdGenerator.stringId();
|
|
|
+ long len = file.length();
|
|
|
+
|
|
|
+ List<DataBlock> list = store(objectId, len, file);
|
|
|
+ FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
+ if (fileMeta == null) {
|
|
|
+ String[] names = objectName.split("/");
|
|
|
+ String filename = names[names.length-1];
|
|
|
+ int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
|
+
|
|
|
+ fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
|
+ dataBlockMapper.saveAll(list);
|
|
|
+ fileMetaMapper.save(fileMeta);
|
|
|
+ } else {
|
|
|
+ log.info("{} sha256sum {} exist", objectName, sha256sum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void putObject(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);
|
|
|
+
|
|
|
FileMeta fileMeta = fileMetaMapper.findBySha256sum(sha256sum);
|
|
|
if (fileMeta == null) {
|
|
|
String[] names = objectName.split("/");
|
|
|
String filename = names[names.length-1];
|
|
|
int fileTypeId = fileTypeService.getFileType1(contentType);
|
|
|
|
|
|
- fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb");
|
|
|
+ fileMeta = new FileMeta(objectName, objectId, filename, len, fileTypeId, contentType, sha256sum, "tnb", 2);
|
|
|
dataBlockMapper.saveAll(list);
|
|
|
fileMetaMapper.save(fileMeta);
|
|
|
+ } else {
|
|
|
+ log.info("sha256sum {} exist", sha256sum);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private List<DataBlock> store(String objectId, long len, File file) throws IOException {
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ FileChannel inputChannel = fis.getChannel();
|
|
|
+
|
|
|
+ List<DataBlock> list = new ArrayList<>();
|
|
|
+ String absolutePath = fileUrlService.genFilePath(len, objectId, "dat");
|
|
|
+ FileOutputStream fos = new FileOutputStream(absolutePath);
|
|
|
+ WritableByteChannel targetChannel = fos.getChannel();
|
|
|
+ inputChannel.transferTo(0, inputChannel.size(), targetChannel);
|
|
|
+
|
|
|
+ String blockId = UUID.randomUUID().toString();
|
|
|
+ list.add(new DataBlock(objectId, 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");
|