하석형 하석형 04-09
250409 하석형 이미지/파일 상대 경로, 유튜브 URL 재가공
@79dc8b5fc6ff7a3ce098974d3c3fdcc04e52c4ff
src/main/java/com/takensoft/cms/bbs/service/Impl/BbsCnServiceImpl.java
--- src/main/java/com/takensoft/cms/bbs/service/Impl/BbsCnServiceImpl.java
+++ src/main/java/com/takensoft/cms/bbs/service/Impl/BbsCnServiceImpl.java
@@ -23,6 +23,8 @@
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.HashMap;
@@ -270,15 +272,17 @@
             if (path.startsWith("file:///")) {
                 // "file:///" 부분을 제거하고 경로를 반환
                 previewPath = path.replace("file:///", "") + fileUploadPath;
-            } else {
-                previewPath = locationPath + fileUploadPath;
             }
+//            else {
+//                previewPath = locationPath + fileUploadPath;
+//            }
         }
         if (filePath == null) return null;
         // 서버경로를 절대경로에서 상대경로로 변환
         String basePath = previewPath; // 이 부분은 환경설정 파일에서 가져오는 것이 좋습니다.
         String relativePath = filePath.substring(basePath.length());
-        return frontUrl + ":" + serverPort + fileUploadPath + relativePath;
+        return fileUploadPath + relativePath; // 윈도우 상대 경로
+//        return frontUrl + ":" + serverPort + fileUploadPath + relativePath;
     }
 
     /**
@@ -462,13 +466,44 @@
      */
      private String vdoUrlCheck(String vdoUrl) {
          // youtube 링크가 있을 경우
-         if (vdoUrl.contains("youtube.com") || vdoUrl.contains("youtu.be")) {
-             String[] url = vdoUrl.split("/");
-             String videoId = url[url.length - 1];
-             String videoUrl = "https://www.youtube.com/embed/" + videoId;
+//         if (vdoUrl.contains("youtube.com") || vdoUrl.contains("youtu.be")) {
+//             String[] url = vdoUrl.split("/");
+//             String videoId = url[url.length - 1];
+//             String videoUrl = "https://www.youtube.com/embed/" + videoId;
+//             return videoUrl;
+//         } else {
+//             return vdoUrl;
+//         }
+         try {
+             URI uri = new URI(vdoUrl);
+             String host = uri.getHost();
+             String path = uri.getPath();
+
+             if (host == null) return vdoUrl;
+
+             String videoUrl = "https://www.youtube.com/embed/";
+
+             if (host.contains("youtu.be")) {
+                 videoUrl += path.substring(1);
+             }
+
+             if (host.contains("youtube.com") || host.contains("m.youtube.com")) {
+                 String query = uri.getQuery();
+                 if (query != null) {
+                     for (String param : query.split("&")) {
+                         if (param.startsWith("v=")) {
+                             videoUrl += param.substring(2);
+                         }
+                     }
+                 }
+
+                 if (path.contains("/embed/")) {
+                     videoUrl += path.substring(path.lastIndexOf("/embed/") + 7);
+                 }
+             }
              return videoUrl;
-         } else {
-             return vdoUrl;
+         } catch (URISyntaxException e) {
+             throw new RuntimeException(e);
          }
      }
 
src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
--- src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
+++ src/main/java/com/takensoft/common/file/service/Impl/FileServiceImpl.java
@@ -288,7 +288,17 @@
             String maskNm = UUID.randomUUID() + ext; // 랜덤으로 파일명 생성
             // 파일 저장 경로
             String uploadPath = null;
-            uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath;
+            // 1. 윈도우 환경
+            String[] pathArray = locationPath.split("/, ");
+            for (String path : pathArray) {
+                if (path.startsWith("file:///")) {
+                    // "file:///" 부분을 제거하고 경로를 반환
+                    uploadPath = path.replace("file:///", "") + editPath;
+                    break; // 루프를 종료하여 첫 번째 일치하는 경로만 사용
+                }
+            }
+            // 2. 리눅스 환경
+//            uploadPath = "/home/cloud-user/front-end/uploadFiles" + editPath;
             String savePath = uploadPath + maskNm;
             // 파일 저장
             try {
@@ -300,7 +310,8 @@
             } catch (IOException ioe) {
                 throw new CustomFileUploadFailException("파일 업로드에 실패했습니다.", ioe);
             }
-            String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm;
+            String imgPath = editPath + maskNm; // 윈도우 상대 경로 경로
+//            String imgPath = frontUrl + "/uploadFiles" + editPath + maskNm; // 리눅스 경로
             return imgPath;
         } catch (DataAccessException dae) {
             throw dae;
Add a comment
List