From b7a62c11a9de23ac9f27ea0c0daad25f7541da31 Mon Sep 17 00:00:00 2001
From: zj <1772600164@qq.com>
Date: Mon, 01 Sep 2025 16:36:20 +0800
Subject: [PATCH] 1

---
 trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java |   89 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 83 insertions(+), 6 deletions(-)

diff --git a/trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java b/trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java
index dd3863e..7b17759 100644
--- a/trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java
+++ b/trading-order-service/src/main/java/com/yami/trading/service/impl/AwsS3OSSFileServiceImpl.java
@@ -157,23 +157,34 @@
     @Override
     public String uploadFile(String moduleName, MultipartFile file) {
         // 兼容c端组件上传原理
-        String fileType = file.getOriginalFilename();
+        String originalFilename = file.getOriginalFilename();
+        String fileType = originalFilename != null ? originalFilename : "";
         if (StrUtil.isEmpty(fileType) || fileType.contains("blob")) {
             fileType = "blob.png";
         }
 
+        // 提取文件扩展名
+        String extension = "";
+        int dotIndex = fileType.lastIndexOf('.');
+        if (dotIndex > 0) {
+            extension = fileType.substring(dotIndex);
+        }
+
         // 生成唯一的文件名
         String id = UUID.randomUUID().toString();
-        String path = LocalDate.now() + "/" + id + fileType;
+        String filename = id + extension;
+        String path = filename; // 直接使用文件名作为路径
 
         // 确保目标文件夹存在
-        File targetDir = new File(PropertiesUtil.getProperty("loca.images.dir")  + "/" + LocalDate.now());
+        File targetDir = new File(PropertiesUtil.getProperty("loca.images.dir"));
         if (!targetDir.exists()) {
             targetDir.mkdirs();
+            // 设置目录权限为755 (rwxr-xr-x)
+            setFilePermissions(targetDir, "755");
         }
 
         // 构建本地文件路径
-        File localFile = new File(targetDir, id + fileType);
+        File localFile = new File(targetDir, filename);
 
         // 打印上传路径
         log.info("LocalFileUploadService uploadFile localFilePath: {}", localFile.getAbsolutePath());
@@ -182,11 +193,14 @@
             // 将文件保存到本地
             file.transferTo(localFile);
 
+            // 设置文件权限为644 (rw-r--r--)
+            setFilePermissions(localFile, "644");
+
             // 如果需要自定义元数据,可以在此处理
             Map<String, String> metadata = new HashMap<>();
-            metadata.put("x-amz-meta-myVal", "test");  // 如果需要元数据,可以放在文件名或其他地方
+            metadata.put("x-amz-meta-myVal", "test");
 
-            // 返回相对路径
+            // 返回文件名
             return path;
 
         } catch (IOException e) {
@@ -194,6 +208,69 @@
             throw new YamiShopBindException("文件上传失败");
         }
     }
+
+    /**
+     * 设置文件或目录权限(使用数字格式)
+     * @param file 文件或目录对象
+     * @param permissions 权限数字字符串,如 "644", "755"
+     */
+    private void setFilePermissions(File file, String permissions) {
+        try {
+            // 使用数字格式的chmod命令(更可靠)
+            Process process = Runtime.getRuntime().exec(new String[]{"chmod", permissions, file.getAbsolutePath()});
+            int exitCode = process.waitFor();
+
+            // 读取命令输出和错误信息
+            String output = readProcessOutput(process.getInputStream());
+            String error = readProcessOutput(process.getErrorStream());
+
+            if (exitCode != 0) {
+                log.warn("设置文件权限失败,文件: {}, 退出码: {}, 错误: {}",
+                        file.getAbsolutePath(), exitCode, error);
+            } else {
+                log.debug("成功设置文件权限 {}: {}", permissions, file.getAbsolutePath());
+                if (!output.isEmpty()) {
+                    log.debug("命令输出: {}", output);
+                }
+            }
+        } catch (IOException | InterruptedException e) {
+            log.warn("设置文件权限时发生异常,文件: {}", file.getAbsolutePath(), e);
+            if (e instanceof InterruptedException) {
+                Thread.currentThread().interrupt();
+            }
+        }
+    }
+
+    /**
+     * 读取进程输出流
+     */
+    private String readProcessOutput(InputStream inputStream) throws IOException {
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
+            StringBuilder output = new StringBuilder();
+            String line;
+            while ((line = reader.readLine()) != null) {
+                if (output.length() > 0) {
+                    output.append("; ");
+                }
+                output.append(line);
+            }
+            return output.toString();
+        }
+    }
+
+    /**
+     * 验证文件权限(调试用)
+     */
+    private void verifyFilePermissions(File file) {
+        try {
+            Process process = Runtime.getRuntime().exec(new String[]{"ls", "-l", file.getAbsolutePath()});
+            process.waitFor();
+            String output = readProcessOutput(process.getInputStream());
+            log.info("文件权限验证: {}", output);
+        } catch (Exception e) {
+            log.warn("无法验证文件权限: {}", e.getMessage());
+        }
+    }
 //    /**
 //     * 上传本地文件
 //     *

--
Gitblit v1.9.3