reghao 3 лет назад
Родитель
Сommit
5a3d3b3dab

+ 6 - 2
src/main/java/cn/reghao/dfs/store/controller/CacheController.java

@@ -1,5 +1,6 @@
 package cn.reghao.dfs.store.controller;
 
+import cn.reghao.dfs.store.model.vo.CacheResult;
 import cn.reghao.dfs.store.service.CacheService;
 import cn.reghao.jutil.jdk.result.WebBody;
 import io.swagger.annotations.Api;
@@ -27,7 +28,10 @@ public class CacheController {
     @ApiOperation(value = "缓存远程图片")
     @PostMapping(value = "/image", produces = MediaType.APPLICATION_JSON_VALUE)
     public String uploadVideoFile(@RequestParam("url") String url) {
-        String localUrl = cacheService.cacheImage(url);
-        return WebBody.success(localUrl);
+        CacheResult cacheResult = cacheService.cacheImage(url);
+        if (cacheResult == null) {
+            return WebBody.failWithMsg("");
+        }
+        return WebBody.success(cacheResult);
     }
 }

+ 21 - 0
src/main/java/cn/reghao/dfs/store/model/vo/CacheResult.java

@@ -0,0 +1,21 @@
+package cn.reghao.dfs.store.model.vo;
+
+/**
+ * @author reghao
+ * @date 2022-07-29 15:12:40
+ */
+public class CacheResult {
+    private Boolean notFound;
+    private String uploadId;
+    private String url;
+
+    public CacheResult(String uploadId, String url) {
+        this.notFound = false;
+        this.uploadId = uploadId;
+        this.url = url;
+    }
+
+    public CacheResult(boolean notFound) {
+        this.notFound = notFound;
+    }
+}

+ 13 - 12
src/main/java/cn/reghao/dfs/store/service/CacheService.java

@@ -1,6 +1,7 @@
 package cn.reghao.dfs.store.service;
 
 import cn.reghao.dfs.store.model.dto.UploadedFile;
+import cn.reghao.dfs.store.model.vo.CacheResult;
 import cn.reghao.dfs.store.model.vo.UploadFileRet;
 import cn.reghao.jutil.jdk.http.util.UrlFormatter;
 import cn.reghao.jutil.jdk.http.util.UserAgents;
@@ -32,7 +33,7 @@ public class CacheService {
         this.fileTypeService = fileTypeService;
     }
 
-    public String cacheImage(String url) {
+    public CacheResult cacheImage(String url) {
         HttpRequest.Builder builder = HttpRequest.newBuilder()
                 .uri(URI.create(url))
                 .timeout(Duration.ofSeconds(30))
@@ -41,21 +42,21 @@ public class CacheService {
 
         try {
             HttpResponse<InputStream> in = client.send(builder.build(), HttpResponse.BodyHandlers.ofInputStream());
-            InputStream inputStream = in.body();
-            String filename = UrlFormatter.getFilename(url);
-            long size = inputStream.available();
-            String contentType = fileTypeService.getFileType(filename).getContentType();
-            if (contentType.startsWith("application")) {
-                return null;
+            int statusCode = in.statusCode();
+            if (statusCode == 200) {
+                InputStream inputStream = in.body();
+                String filename = UrlFormatter.getFilename(url);
+                long size = inputStream.available();
+                String contentType = fileTypeService.getFileType(filename).getContentType();
+                UploadedFile uploadedFile = new UploadedFile(filename, size, contentType, inputStream);
+                UploadFileRet uploadFileRet = fileUploadService.put(uploadedFile);
+                return new CacheResult(uploadFileRet.getUploadId(), uploadFileRet.getUrl());
+            } else if (statusCode == 404) {
+                return new CacheResult(true);
             }
-
-            UploadedFile uploadedFile = new UploadedFile(filename, size, contentType, inputStream);
-            UploadFileRet uploadFileRet = fileUploadService.put(uploadedFile);
-            return uploadFileRet.getUrl();
         } catch (Exception e) {
             log.info("{} 下载失败 -> {}", url, e.getMessage());
         }
-
         return null;
     }
 }

+ 6 - 0
src/main/java/cn/reghao/dfs/store/service/FileTypeService.java

@@ -26,6 +26,12 @@ public class FileTypeService {
         if (suffix.startsWith("jpg")) {
             fileType = 1;
             contentType = "image/jpg";
+        } else if (suffix.startsWith("gif")) {
+            fileType = 1;
+            contentType = "image/gif";
+        } else if (suffix.startsWith("png")) {
+            fileType = 1;
+            contentType = "image/png";
         } else if (suffix.startsWith("mp3")) {
             fileType = 2;
             contentType = "audio/mp3";