Procházet zdrojové kódy

fix-[noteData,NoteNationData]: 新增文件可以为00,删除文件时删除全部图片

seeseele před 9 měsíci
rodič
revize
d6a291e6ca

+ 4 - 0
src/main/java/com/ruoyi/project/VRdemo/controller/NoteDataController.java

@@ -120,8 +120,12 @@ public class NoteDataController extends BaseController {
         for (String s : paths) {
             String fileName = s.substring(s.lastIndexOf("/") + 1, s.lastIndexOf(".")); //获取文件名
             String last = fileName.substring(fileName.length() - 2); //获取文件名后面的编号
+            if (last.equals("01")) {
+                zero = s;
+            }
             if (last.equals("00")) {
                 zero = s;
+                break;
             }
         }
         noteData.setPath(zero);

+ 4 - 0
src/main/java/com/ruoyi/project/VRdemo/controller/NoteNationDataController.java

@@ -108,8 +108,12 @@ public class NoteNationDataController extends BaseController {
         for (String s : paths) {
             String fileName = s.substring(s.lastIndexOf("/") + 1, s.lastIndexOf(".")); //获取文件名
             String last = fileName.substring(fileName.length() - 2); //获取文件名后面的编号
+            if (last.equals("01")) {
+                zero = s;
+            }
             if (last.equals("00")) {
                 zero = s;
+                break;
             }
         }
         noteNationData.setPath(zero);

+ 82 - 8
src/main/java/com/ruoyi/project/VRdemo/service/impl/NoteDataServiceImpl.java

@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 /**
@@ -129,6 +130,7 @@ public class NoteDataServiceImpl implements INoteDataService {
         }
 
         String oldPath = file.getPath();
+        Long count = file.getCount();
 
         System.out.println("oldPath  "+oldPath);
 
@@ -149,7 +151,7 @@ public class NoteDataServiceImpl implements INoteDataService {
 
             String deletaPath = filePath + StringUtils.substringAfter(oldPath, Constants.RESOURCE_PREFIX);
             System.out.println("5." + deletaPath);
-            if (deleteFileRecursive(deletaPath)) {
+            if (deleteFileRecursive(deletaPath, count)) {
                 // 执行特定文件夹删除操作
                 String fileExtension = getFileExtensionFromPath(deletaPath);
                 if (fileExtension.equalsIgnoreCase("pdf")) {
@@ -219,14 +221,86 @@ public class NoteDataServiceImpl implements INoteDataService {
      * @param ids 文件
      * @return
      */
-    public static boolean deleteFileRecursive(String ids) {
-        File file = new File(ids);
-        System.out.println("11." + file.getPath());
-        if (file.isFile() && file.exists()) {
-            return file.delete();
+    public static boolean deleteFileRecursive(String ids, Long count) {
+        for (String s : deleteFileList(ids, count)){
+            File file = new File(s);
+            System.out.println("11. " + s);
+            if (file.isFile() && file.exists()) {
+                if (!file.delete()){
+                    return false;
+                }
+            }
         }
-        System.out.println(false);
-        return false;
+
+        return true;
+    }
+
+    /**
+     * 获取要删除的文件列表
+     */
+    public static List<String> deleteFileList(String str, Long count){
+        //获取/前的str
+        String path = "";
+        // 查找最后一个 '/' 的索引
+        int lastSlashIndex = str.lastIndexOf('/');
+        // 确保找到 '/' 并且 '/' 不是第一个字符
+        if (lastSlashIndex > 0 && lastSlashIndex < str.length() - 1) {
+            // 获取 '/' 前面的字符
+            path = str.substring(0, lastSlashIndex);
+        }
+        // 获取文件名
+        String fileName = str.substring(lastSlashIndex+1);
+
+        String[] strings = splitFileName(fileName);
+        List<String> list = new ArrayList<>();
+        int i = 0;
+        // 如果文件不从0开始
+        if (!strings[1].equals("00")){
+            i = 1;
+            count++;
+        }
+        for (; i < count; i++){
+            String fileNumber = "";
+            System.out.println(strings[1]);
+            if (i < 100){
+                fileNumber = String.format("%02d", i);
+            }else {
+                fileNumber = String.valueOf(i);
+            }
+
+            String filename = path + "/" + strings[0] + "_" + fileNumber + "." + strings[2];
+            System.out.println("filename"+filename);
+            list.add(filename);
+        }
+        return list;
+    }
+
+    /**
+     * 分解格式为 xxx_00.jpg 的字符串
+     *
+     * @param fileName 要处理的字符串
+     * @return 分解后的字符串数组,其中包含_前面的部分,_和.之间的部分,以及.后面的部分
+     */
+    public static String[] splitFileName(String fileName) {
+        // 找到 '_' 和 '.' 的索引
+        int underscoreIndex = fileName.indexOf('_');
+        int dotIndex = fileName.indexOf('.');
+
+        // 检查索引有效性
+        if (underscoreIndex == -1 || dotIndex == -1 || underscoreIndex >= dotIndex) {
+            throw new IllegalArgumentException("字符串格式不正确");
+        }
+
+        // 获取 _ 前面的部分
+        String partBeforeUnderscore = fileName.substring(0, underscoreIndex);
+
+        // 获取 _ 和 . 之间的部分
+        String partBetweenUnderscoreAndDot = fileName.substring(underscoreIndex + 1, dotIndex);
+
+        // 获取 . 后面的部分
+        String partAfterDot = fileName.substring(dotIndex + 1);
+
+        return new String[] { partBeforeUnderscore, partBetweenUnderscoreAndDot, partAfterDot };
     }
 
     // 递归删除文件夹及其内容

+ 83 - 8
src/main/java/com/ruoyi/project/VRdemo/service/impl/NoteNationDataServiceImpl.java

@@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.io.File;
+import java.util.ArrayList;
 import java.util.List;
 
 @Service
@@ -121,6 +122,8 @@ public class NoteNationDataServiceImpl implements INoteNationDataService {
         }
 
         String oldPath = file.getPath();
+        Long count = file.getCount();
+
         if (oldPath != null) {
             String filePath = RuoYiConfig.getUploadPath();
 
@@ -133,7 +136,7 @@ public class NoteNationDataServiceImpl implements INoteNationDataService {
 
             String deletaPath = filePath + StringUtils.substringAfter(oldPath, Constants.RESOURCE_PREFIX);
             System.out.println("5." + deletaPath);
-            if (deleteFileRecursive(deletaPath)) {
+            if (deleteFileRecursive(deletaPath, count)) {
                 // 执行特定文件夹删除操作
                 String fileExtension = getFileExtensionFromPath(deletaPath);
                 if (fileExtension.equalsIgnoreCase("pdf")) {
@@ -203,14 +206,86 @@ public class NoteNationDataServiceImpl implements INoteNationDataService {
      * @param ids 文件
      * @return
      */
-    public static boolean deleteFileRecursive(String ids) {
-        File file = new File(ids);
-        System.out.println("11." + file.getPath());
-        if (file.isFile() && file.exists()) {
-            return file.delete();
+    public static boolean deleteFileRecursive(String ids, Long count) {
+       for (String s : deleteFileList(ids, count)){
+           File file = new File(s);
+           System.out.println("11. " + s);
+           if (file.isFile() && file.exists()) {
+               if (!file.delete()){
+                   return false;
+               }
+           }
+       }
+
+        return true;
+    }
+
+    /**
+     * 获取要删除的文件列表
+     */
+    public static List<String> deleteFileList(String str, Long count){
+        //获取/前的str
+        String path = "";
+        // 查找最后一个 '/' 的索引
+        int lastSlashIndex = str.lastIndexOf('/');
+        // 确保找到 '/' 并且 '/' 不是第一个字符
+        if (lastSlashIndex > 0 && lastSlashIndex < str.length() - 1) {
+            // 获取 '/' 前面的字符
+            path = str.substring(0, lastSlashIndex);
         }
-        System.out.println(false);
-        return false;
+        // 获取文件名
+        String fileName = str.substring(lastSlashIndex+1);
+
+        String[] strings = splitFileName(fileName);
+        List<String> list = new ArrayList<>();
+        int i = 0;
+        // 如果文件不从0开始
+        if (!strings[1].equals("00")){
+            i = 1;
+            count++;
+        }
+        for (; i < count; i++){
+            String fileNumber = "";
+            System.out.println(strings[1]);
+            if (i < 100){
+                fileNumber = String.format("%02d", i);
+            }else {
+                fileNumber = String.valueOf(i);
+            }
+
+            String filename = path + "/" + strings[0] + "_" + fileNumber + "." + strings[2];
+            System.out.println("filename"+filename);
+            list.add(filename);
+        }
+        return list;
+    }
+
+    /**
+     * 分解格式为 xxx_00.jpg 的字符串
+     *
+     * @param fileName 要处理的字符串
+     * @return 分解后的字符串数组,其中包含_前面的部分,_和.之间的部分,以及.后面的部分
+     */
+    public static String[] splitFileName(String fileName) {
+        // 找到 '_' 和 '.' 的索引
+        int underscoreIndex = fileName.indexOf('_');
+        int dotIndex = fileName.indexOf('.');
+
+        // 检查索引有效性
+        if (underscoreIndex == -1 || dotIndex == -1 || underscoreIndex >= dotIndex) {
+            throw new IllegalArgumentException("字符串格式不正确");
+        }
+
+        // 获取 _ 前面的部分
+        String partBeforeUnderscore = fileName.substring(0, underscoreIndex);
+
+        // 获取 _ 和 . 之间的部分
+        String partBetweenUnderscoreAndDot = fileName.substring(underscoreIndex + 1, dotIndex);
+
+        // 获取 . 后面的部分
+        String partAfterDot = fileName.substring(dotIndex + 1);
+
+        return new String[] { partBeforeUnderscore, partBetweenUnderscoreAndDot, partAfterDot };
     }
 
     // 递归删除文件夹及其内容