Просмотр исходного кода

使用 linux file 命令获取文件的 media type

reghao 2 лет назад
Родитель
Сommit
e1e68af668

+ 0 - 6
dfs-store/pom.xml

@@ -110,12 +110,6 @@
             <version>31.1-jre</version>
         </dependency>
 
-        <dependency>
-            <groupId>org.apache.tika</groupId>
-            <artifactId>tika-core</artifactId>
-            <version>2.7.0</version>
-        </dependency>
-
         <dependency>
             <groupId>io.springfox</groupId>
             <artifactId>springfox-swagger2</artifactId>

+ 11 - 7
dfs-store/src/main/java/cn/reghao/dfs/store/service/PutObjectService.java

@@ -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;
     }
 

+ 1 - 1
dfs-store/src/test/java/FileTest.java

@@ -31,7 +31,7 @@ public class FileTest {
     }
 
     public static void main(String[] args) {
-        String filePath = "/home/reghao/Downloads/public.sql";
+        String filePath = "/opt/oss/disk/00b989fc-991b-4d4e-959e-9b6e19299b72/2/101//14abaa52e32243a196561c19a0ef70f1.dat";
 
         Tika tika = new Tika();
         log.info(tika.detect(filePath));