Browse Source

修改core-fss中OssStore关于mime的获取方法,避免常见文件类型content-type都被设置成application/octet-stream的情况

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 8 months ago
parent
commit
be456847e3

+ 3 - 6
core-fss/src/main/java/com/qmth/boot/core/fss/store/impl/OssStore.java

@@ -11,11 +11,11 @@ import com.qmth.boot.tools.uuid.FastUUID;
 import okhttp3.*;
 import org.apache.commons.lang3.StringUtils;
 
-import javax.activation.MimetypesFileTypeMap;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.URLConnection;
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
 
@@ -25,15 +25,12 @@ public class OssStore implements FileStore {
 
     private OssConfig config;
 
-    private MimetypesFileTypeMap mimeTypes;
-
     private File tempDir;
 
     public OssStore(OssConfig config, String tempPath) {
         this.client = new OkHttpClient.Builder().connectionPool(new ConnectionPool())
                 .connectTimeout(Duration.ofSeconds(10)).readTimeout(Duration.ofSeconds(30)).build();
         this.config = config;
-        this.mimeTypes = new MimetypesFileTypeMap();
         this.tempDir = new File(tempPath);
     }
 
@@ -76,9 +73,9 @@ public class OssStore implements FileStore {
         try {
             path = formatPath(path);
             OssApiParam param = new OssApiParam().setContentMd5(toBase64(md5))
-                    .setContentType(mimeTypes.getContentType(path));
+                    .setContentType(URLConnection.guessContentTypeFromName(path));
             tempFile = writeToTemp(ins);
-            RequestBody body = RequestBody.create(MediaType.parse(param.getContentType()), tempFile);
+            RequestBody body = RequestBody.create(null, tempFile);
             Request request = new Request.Builder().url(config.getEndpoint() + "/" + path)
                     .headers(buildHeader(param, OssSigner.buildHeader(config, "put", path, param))).put(body).build();
             Response response = client.newCall(request).execute();

+ 13 - 5
core-fss/src/test/java/com/qmth/boot/test/core/fss/OssStoreTest.java

@@ -5,6 +5,7 @@ import com.qmth.boot.core.fss.utils.OssConfig;
 import com.qmth.boot.tools.models.ByteArray;
 import org.apache.commons.lang3.RandomStringUtils;
 import org.junit.Assert;
+import org.junit.Test;
 
 import java.io.ByteArrayInputStream;
 import java.time.Duration;
@@ -19,17 +20,24 @@ public class OssStoreTest {
 
     private String config_local = "oss://key:secret@test.oss-api.qmth.com.cn";
 
-    //@Test
+    @Test
     public void test() throws Exception {
         OssStore store = new OssStore(new OssConfig(config_cloud, server_cloud), System.getProperty("java.io.tmpdir"));
         String content = RandomStringUtils.random(128, true, true);
         ByteArray data = ByteArray.fromString(content);
-        store.write("/test/1.txt", new ByteArrayInputStream(data.value()), ByteArray.md5(data.value()).toHexString());
-        Assert.assertTrue(store.exist("test/1.txt"));
-        Assert.assertEquals(content, ByteArray.fromInputStream(store.read("test/1.txt")).toString());
-        String url = store.getServerUrl("/test/1.txt", Duration.ofMinutes(5));
+        store.write("/test/randome", new ByteArrayInputStream(data.value()), ByteArray.md5(data.value()).toHexString());
+        Assert.assertTrue(store.exist("test/randome"));
+        Assert.assertEquals(content, ByteArray.fromInputStream(store.read("test/randome")).toString());
+        String url = store.getServerUrl("/test/randome", Duration.ofMinutes(5));
         //System.out.println(url);
         Assert.assertEquals(content, ByteArray.fromUrl(url).toString());
+
+        //        data = ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.pdf"));
+        //        store.write("/test/test.pdf", new ByteArrayInputStream(data.value()),
+        //                ByteArray.md5(data.value()).toHexString());
+        //        data = ByteArray.fromFile(new File("/Users/luoshi/Downloads/test.json"));
+        //        store.write("/test/test.json", new ByteArrayInputStream(data.value()),
+        //                ByteArray.md5(data.value()).toHexString());
         store.close();
     }