瀏覽代碼

代码优化

fangzhen 4 月之前
父節點
當前提交
e1f3a1a821

二進制
.jpg


+ 90 - 58
ruoyi-admin/src/main/java/com/ruoyi/web/controller/common/CommonController.java

@@ -10,6 +10,7 @@ import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.file.FileUploadUtils;
 import com.ruoyi.common.utils.file.FileUtils;
 import com.ruoyi.common.utils.file.ImageUtils;
+import com.ruoyi.common.utils.file.OssUtils;
 import com.ruoyi.framework.config.ServerConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -44,6 +45,8 @@ public class CommonController {
 
     private static final String FILE_DELIMETER = ",";
 
+    private static final String fileUrl = "http://file.iciyuanshidai.com";
+
     /**
      * 通用下载请求
      *
@@ -70,60 +73,6 @@ public class CommonController {
         }
     }
 
-    /**
-     * 通用上传请求(单个)
-     */
-    @PostMapping("/upload")
-    public AjaxResult uploadFile(MultipartFile file) throws Exception {
-        try {
-            // 上传文件路径
-            String filePath = RuoYiConfig.getUploadPath();
-            String thumbnailPath = "";
-            // 上传并返回新文件名称
-            String fileName = FileUploadUtils.upload(filePath, file);
-            String url = serverConfig.getUrl() + fileName;
-            String fileType = FileUtils.getFileType(file);
-            if ("video".equalsIgnoreCase(fileType)) {
-                thumbnailPath = fileName.replace("/profile", RuoYiConfig.getProfile());
-                int dotIndex = thumbnailPath.lastIndexOf(".");
-                if (dotIndex != -1) {
-                    thumbnailPath = thumbnailPath.substring(0, dotIndex) + ".jpg";
-                } else {
-                    thumbnailPath = thumbnailPath + ".jpg";  // 如果没有后缀,直接加 .jpg
-                }
-                FileUtils.screenShots(fileName.replace("/profile", RuoYiConfig.getProfile()), thumbnailPath, "00:00:01");
-            }
-
-            if ("image".equalsIgnoreCase(fileType)) {
-                //获取用户名
-                String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
-                // 原始文件
-                if (fileName.contains("/profile/upload")) {
-                    fileName = fileName.replace("/profile/upload", "");
-                }
-
-                File sourceFile = new File(filePath + fileName);
-                //图片添加水印
-                String watermarkFileName = "watermark_" + fileName;
-                File destFile = new File(filePath + fileName);
-                // 加水印
-                ImageUtils.addWatermark(sourceFile, destFile, "@" + nickName, "次元时代-ACG爱好者社区");
-                fileName = watermarkFileName;
-            }
-
-            AjaxResult ajax = AjaxResult.success();
-            ajax.put("url", url);
-            ajax.put("fileName", fileName);
-            ajax.put("newFileName", FileUtils.getName(fileName));
-            ajax.put("originalFilename", file.getOriginalFilename());
-            ajax.put("fileType", fileType);
-            ajax.put("thumbnailPath", thumbnailPath);
-            return ajax;
-        } catch (Exception e) {
-            return AjaxResult.error(e.getMessage());
-        }
-    }
-
 //    /**
 //     * 通用上传请求(单个)
 //     */
@@ -134,12 +83,11 @@ public class CommonController {
 //            String filePath = RuoYiConfig.getUploadPath();
 //            String thumbnailPath = "";
 //            // 上传并返回新文件名称
-//            String fileName = OssUtils.uploadMultipartFile(file);
-//            System.out.println(fileName);
-//            String url = OssUtils.getOssFilePath(fileName);
+//            String fileName = FileUploadUtils.upload(filePath, file);
+//            String url = serverConfig.getUrl() + fileName;
 //            String fileType = FileUtils.getFileType(file);
 //            if ("video".equalsIgnoreCase(fileType)) {
-//                thumbnailPath = fileName;
+//                thumbnailPath = fileName.replace("/profile", RuoYiConfig.getProfile());
 //                int dotIndex = thumbnailPath.lastIndexOf(".");
 //                if (dotIndex != -1) {
 //                    thumbnailPath = thumbnailPath.substring(0, dotIndex) + ".jpg";
@@ -148,6 +96,24 @@ public class CommonController {
 //                }
 //                FileUtils.screenShots(fileName.replace("/profile", RuoYiConfig.getProfile()), thumbnailPath, "00:00:01");
 //            }
+//
+//            if ("image".equalsIgnoreCase(fileType)) {
+//                //获取用户名
+//                String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
+//                // 原始文件
+//                if (fileName.contains("/profile/upload")) {
+//                    fileName = fileName.replace("/profile/upload", "");
+//                }
+//
+//                File sourceFile = new File(filePath + fileName);
+//                //图片添加水印
+//                String watermarkFileName = "watermark_" + fileName;
+//                File destFile = new File(filePath + fileName);
+//                // 加水印
+//                ImageUtils.addWatermark(sourceFile, destFile, "@" + nickName, "次元时代-ACG爱好者社区");
+//                fileName = watermarkFileName;
+//            }
+//
 //            AjaxResult ajax = AjaxResult.success();
 //            ajax.put("url", url);
 //            ajax.put("fileName", fileName);
@@ -161,6 +127,72 @@ public class CommonController {
 //        }
 //    }
 
+    /**
+     * 通用上传请求(单个) OSS
+     */
+    @PostMapping("/upload")
+    public AjaxResult uploadFile(MultipartFile file) throws Exception {
+        try {
+            // 上传文件路径
+            String fileName = OssUtils.uploadMultipartFile(file);
+            System.out.println(fileName);
+            String fileNameResult = fileName.replace("https://cysd.oss-cn-shanghai.aliyuncs.com", fileUrl);
+            String thumbnailPath = "";
+            String fileType = FileUtils.getFileType(file);
+            if ("video".equalsIgnoreCase(fileType)) {
+                String tempThumbnailFile = System.getProperty("java.io.tmpdir") + File.separator + "thumbnail.jpg";
+
+                boolean screenshotSuccess = FileUtils.screenShots(fileNameResult, tempThumbnailFile, "00:00:01");
+                if (!screenshotSuccess) {
+                    throw new RuntimeException("生成视频缩略图失败");
+                }
+
+                // 上传缩略图到OSS
+                thumbnailPath = OssUtils.uploadFile(tempThumbnailFile);
+
+                // 删除临时缩略图文件
+                new File(tempThumbnailFile).delete();
+            }
+
+            if ("image".equalsIgnoreCase(fileType)) {
+                //获取用户名
+                String nickName = SecurityUtils.getLoginUser().getUser().getNickName();
+                // 下载源文件到本地临时目录
+                String tempSourceFile = System.getProperty("java.io.tmpdir") + File.separator + "source_" + FileUtils.getName(fileName);
+                OssUtils.downloadFile(fileName, tempSourceFile);
+
+                // 本地临时水印文件路径
+                String tempWatermarkFile = System.getProperty("java.io.tmpdir") + File.separator + "watermark_" + FileUtils.getName(fileName);
+                // 添加水印
+                ImageUtils.addWatermark(
+                        new File(tempSourceFile),
+                        new File(tempWatermarkFile),
+                        "@" + nickName,
+                        "次元时代-ACG爱好者社区"
+                );
+
+                // 上传加水印的文件到OSS
+                fileName = OssUtils.uploadFile(tempWatermarkFile);
+
+                // 删除临时文件
+                new File(tempSourceFile).delete();
+                new File(tempWatermarkFile).delete();
+            }
+
+            String url = fileNameResult;
+            AjaxResult ajax = AjaxResult.success();
+            ajax.put("url", url);
+            ajax.put("fileName", fileNameResult);
+            ajax.put("newFileName", FileUtils.getName(fileNameResult));
+            ajax.put("originalFilename", file.getOriginalFilename());
+            ajax.put("fileType", fileType);
+            ajax.put("thumbnailPath", thumbnailPath.replace("https://cysd.oss-cn-shanghai.aliyuncs.com", fileUrl));
+            return ajax;
+        } catch (Exception e) {
+            return AjaxResult.error(e.getMessage());
+        }
+    }
+
     /**
      * 通用上传请求(多个)
      */

+ 7 - 4
ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FileUtils.java

@@ -299,9 +299,7 @@ public class FileUtils {
         commands.add("image2");
         commands.add(file);
 
-        Process process = new ProcessBuilder(commands)
-                .start();
-
+        Process process = new ProcessBuilder(commands).start();
         // 读取进程标准输出
         new Thread(() -> {
             try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
@@ -324,7 +322,12 @@ public class FileUtils {
             }
         }).start();
 
-        return process.waitFor() == 0;
+        if (process.waitFor() != 0) {
+            return false;
+        }
+
+        OssUtils.uploadFile(file);
+        return true;
     }
 
     /**

+ 1 - 1
ruoyi-generator/src/main/java/com/ruoyi/generator/vo/CommunityArticleVo.java

@@ -130,7 +130,7 @@ public class CommunityArticleVo extends BaseEntity implements Serializable {
     @ApiModelProperty("文章标签")
     private List<CommunityTag> tags;
 
-    @ApiModelProperty("文章标签")
+    @ApiModelProperty("文章合集")
     private CommunityCollectionVo collectionVo;
 
     /**