mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-08-08 15:41:11 +09:00
This commit is contained in:
@@ -13,36 +13,30 @@ import java.io.InputStream;
|
|||||||
@RestController
|
@RestController
|
||||||
public class FileSyncController {
|
public class FileSyncController {
|
||||||
|
|
||||||
// 🟢 @RequestParam을 추가하여 마이크로드로이드가 보내는 'filename' 값을 전달받습니다.
|
|
||||||
@PostMapping("/api/sync/upload")
|
@PostMapping("/api/sync/upload")
|
||||||
public ResponseEntity<?> uploadFile(
|
public ResponseEntity<?> uploadFile(
|
||||||
HttpServletRequest request,
|
HttpServletRequest request,
|
||||||
@RequestParam(value = "filename", required = false) String originalFileName) {
|
@RequestParam(value = "filename", required = false) String originalFileName) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. 저장할 파일명 결정 로직
|
// 1. 🟢 파일명 검증: 원래 명칭이 비어있거나 누락되었다면 요청을 거부합니다.
|
||||||
String fileName;
|
if (originalFileName == null || originalFileName.trim().isEmpty()) {
|
||||||
if (originalFileName != null && !originalFileName.isEmpty()) {
|
return ResponseEntity.badRequest().body("Error: 원래 파일명이 전달되지 않았습니다.");
|
||||||
// 마이크로드로이드가 이름을 보내준 경우 그 이름(확장자 포함)을 그대로 사용
|
|
||||||
fileName = originalFileName;
|
|
||||||
} else {
|
|
||||||
// 이름을 찾지 못한 경우의 예비용 이름 (확장자를 알 수 없으므로 .dat로 임시 지정)
|
|
||||||
fileName = "unknown_file_" + System.currentTimeMillis() + ".dat";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 도커 내부의 상대 경로로 폴더 지정
|
// 2. 유저님이 지정하신 나스의 실제 절대 경로 고정
|
||||||
String uploadDirPath = "./upload/";
|
String uploadDirPath = "/volume1/docker/upload_dir/";
|
||||||
File uploadDir = new File(uploadDirPath);
|
File uploadDir = new File(uploadDirPath);
|
||||||
|
|
||||||
// 3. 폴더가 존재하지 않으면 자동으로 생성
|
// 디렉토리가 없으면 안전하게 자동 생성
|
||||||
if (!uploadDir.exists()) {
|
if (!uploadDir.exists()) {
|
||||||
uploadDir.mkdirs();
|
uploadDir.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. 결정된 파일명으로 최종 저장 경로 생성
|
// 3. 🟢 파일명 가공 없이 원래 명칭 그대로 저장 파일 객체 정의
|
||||||
File targetFile = new File(uploadDir, fileName);
|
File targetFile = new File(uploadDir, originalFileName);
|
||||||
|
|
||||||
// 5. 파일 데이터 수신 및 쓰기
|
// 4. 바이너리 스트림을 읽어 파일 작성 진행
|
||||||
try (InputStream is = request.getInputStream();
|
try (InputStream is = request.getInputStream();
|
||||||
FileOutputStream fos = new FileOutputStream(targetFile)) {
|
FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||||
|
|
||||||
@@ -53,8 +47,8 @@ public class FileSyncController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
System.out.println("✅ 파일 업로드 성공: " + targetFile.getAbsolutePath());
|
System.out.println("✅ 원래 명칭 그대로 업로드 완료: " + targetFile.getAbsolutePath());
|
||||||
return ResponseEntity.ok("Upload Success: " + fileName);
|
return ResponseEntity.ok("Upload Success: " + originalFileName);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
Reference in New Issue
Block a user