|
|
@@ -8,9 +8,9 @@ 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.redis.ds.RedisStringObj;
|
|
|
+import cn.reghao.jutil.jdk.shell.Shell;
|
|
|
import cn.reghao.jutil.tool.id.IdGenerator;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.tika.Tika;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@@ -39,7 +39,6 @@ public class PutObjectService {
|
|
|
private final FileTypeService fileTypeService;
|
|
|
private final LocalCache localCache;
|
|
|
private final RedisStringObj redisStringObj;
|
|
|
- private Tika tika = new Tika();
|
|
|
|
|
|
public PutObjectService(FileMetaMapper fileMetaMapper, FileContentMapper fileContentMapper,
|
|
|
DataBlockMapper dataBlockMapper,
|
|
|
@@ -63,7 +62,7 @@ public class PutObjectService {
|
|
|
String[] names = objectName.split("/");
|
|
|
String filename = names[names.length-1];
|
|
|
|
|
|
- String contentType = tika.detect(file);
|
|
|
+ String contentType = getMediaType(file.getAbsolutePath());
|
|
|
FileMeta fileMeta2 = fileMetaMapper.findByObjectName(objectName);
|
|
|
if (fileMeta2 != null) {
|
|
|
String sha256sum1 = fileMeta2.getSha256sum();
|
|
|
@@ -83,7 +82,7 @@ public class PutObjectService {
|
|
|
String contentId = UUID.randomUUID().toString().replace("-", "");
|
|
|
FileContent fileContent = new FileContent(contentId, objectId);
|
|
|
long len = file.length();
|
|
|
- List<DataBlock> blocks = store(objectId, len, file);
|
|
|
+ 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);
|
|
|
|
|
|
@@ -102,6 +101,11 @@ public class PutObjectService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private String getMediaType(String src) {
|
|
|
+ String cmd = String.format("/bin/file -b --mime-type \"%s\"", src);
|
|
|
+ return Shell.execWithResult(cmd);
|
|
|
+ }
|
|
|
+
|
|
|
private void addParent(String objectName) {
|
|
|
List<String> list = getParent(objectName);
|
|
|
List<FileMeta> fileMetas = new ArrayList<>();
|
|
|
@@ -130,19 +134,19 @@ public class PutObjectService {
|
|
|
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 contentId, 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");
|
|
|
+ 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(objectId, 0, blockId, absolutePath));
|
|
|
+ list.add(new DataBlock(contentId, 0, blockId, absolutePath));
|
|
|
return list;
|
|
|
}
|
|
|
|