|
|
@@ -1,33 +1,50 @@
|
|
|
package cn.reghao.dfs.store.sync;
|
|
|
|
|
|
+import cn.reghao.dfs.store.config.DfsProperties;
|
|
|
+import cn.reghao.dfs.store.db.mapper.FilePathMapper;
|
|
|
+import cn.reghao.dfs.store.model.dto.SyncFile;
|
|
|
import cn.reghao.dfs.store.model.po.FilePath;
|
|
|
-import cn.reghao.dfs.store.model.po.FileUrl;
|
|
|
import cn.reghao.dfs.store.service.FileStoreService;
|
|
|
import cn.reghao.dfs.store.service.FileUrlService;
|
|
|
+import cn.reghao.dfs.store.util.store.StoreDir;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
/**
|
|
|
+ * 节点间的文件同步
|
|
|
+ *
|
|
|
* @author reghao
|
|
|
* @date 2022-08-16 20:15:13
|
|
|
*/
|
|
|
@Service
|
|
|
public class FileSync {
|
|
|
- private FileUrlService fileUrlService;
|
|
|
- private FileStoreService fileStoreService;
|
|
|
-
|
|
|
- public FileSync(FileUrlService fileUrlService, FileStoreService fileStoreService) {
|
|
|
+ private final int group;
|
|
|
+ private final int node;
|
|
|
+ private final FileUrlService fileUrlService;
|
|
|
+ private final FileStoreService fileStoreService;
|
|
|
+ private final FilePathMapper filePathMapper;
|
|
|
+
|
|
|
+ public FileSync(DfsProperties dfsProperties, FileUrlService fileUrlService, FileStoreService fileStoreService,
|
|
|
+ FilePathMapper filePathMapper) {
|
|
|
+ this.group = dfsProperties.getGroup();
|
|
|
+ this.node = dfsProperties.getNode();
|
|
|
this.fileUrlService = fileUrlService;
|
|
|
this.fileStoreService = fileStoreService;
|
|
|
+ this.filePathMapper = filePathMapper;
|
|
|
}
|
|
|
|
|
|
public void send(String fileId, String absolutePath) {
|
|
|
-
|
|
|
}
|
|
|
|
|
|
- public void receive(String fileId, InputStream inputStream) {
|
|
|
+ public void receive(SyncFile syncFile, InputStream inputStream) throws IOException {
|
|
|
+ StoreDir storeDir = fileUrlService.getStoreDir(syncFile.getSha256sum(), syncFile.getFileSize());
|
|
|
+ String dir = storeDir.getAbsoluteDirPath();
|
|
|
+ String absolutePath = String.format("%s/%s", dir, syncFile.getFilename());
|
|
|
+ fileStoreService.saveFile(absolutePath, inputStream);
|
|
|
|
|
|
- FilePath filePath = new FilePath();
|
|
|
+ FilePath filePath = new FilePath(syncFile.getFileId(), storeDir.getBlockId(), absolutePath, group, node);
|
|
|
+ filePathMapper.save(filePath);
|
|
|
}
|
|
|
}
|