mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-08-08 15:41:11 +09:00
This commit is contained in:
@@ -3,53 +3,58 @@ package company.specialsource.controller;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.multipart.MultipartHttpServletRequest;
|
||||
import java.io.InputStream;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
|
||||
@RestController
|
||||
public class FileSyncController {
|
||||
|
||||
// 🟢 @RequestParam을 추가하여 마이크로드로이드가 보내는 'filename' 값을 전달받습니다.
|
||||
@PostMapping("/api/sync/upload")
|
||||
public ResponseEntity<?> uploadFile(HttpServletRequest request) {
|
||||
String contentType = request.getContentType();
|
||||
public ResponseEntity<?> uploadFile(
|
||||
HttpServletRequest request,
|
||||
@RequestParam(value = "filename", required = false) String originalFileName) {
|
||||
|
||||
try {
|
||||
// 1. 기존 웹 프론트엔드(Vue)에서 보낸 폼 데이터(Multipart) 처리
|
||||
if (contentType != null && contentType.startsWith("multipart/form-data")) {
|
||||
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
||||
MultipartFile multipartFile = multipartRequest.getFile("file");
|
||||
|
||||
if (multipartFile != null && !multipartFile.isEmpty()) {
|
||||
// 💾 기존에 사용하시던 파일 저장 로직을 여기에 그대로 수행하면 됩니다.
|
||||
// 예: multipartFile.transferTo(new File("기존 저장 경로"));
|
||||
System.out.println("웹 프론트엔드로부터 멀티파트 파일 수신 완료: " + multipartFile.getOriginalFilename());
|
||||
}
|
||||
}
|
||||
// 2. MacroDroid 매크로에서 보낸 순수 바이너리 스트림(Octet-Stream 등) 처리
|
||||
else {
|
||||
// 저장될 임시 파일명 정의 (타임스탬프 조합)
|
||||
String fileName = "macro_sync_" + System.currentTimeMillis() + ".jpg";
|
||||
|
||||
// ⚠️ 유저님의 나스 환경에 맞는 실제 업로드 절대 경로를 입력해 주세요.
|
||||
File targetFile = new File("/volume1/docker/upload_dir/" + fileName);
|
||||
|
||||
try (InputStream is = request.getInputStream();
|
||||
FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
while ((bytesRead = is.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
System.out.println("MacroDroid로부터 바이너리 파일 수신 및 저장 완료: " + fileName);
|
||||
// 1. 저장할 파일명 결정 로직
|
||||
String fileName;
|
||||
if (originalFileName != null && !originalFileName.isEmpty()) {
|
||||
// 마이크로드로이드가 이름을 보내준 경우 그 이름(확장자 포함)을 그대로 사용
|
||||
fileName = originalFileName;
|
||||
} else {
|
||||
// 이름을 찾지 못한 경우의 예비용 이름 (확장자를 알 수 없으므로 .dat로 임시 지정)
|
||||
fileName = "unknown_file_" + System.currentTimeMillis() + ".dat";
|
||||
}
|
||||
|
||||
return ResponseEntity.ok("Upload Success");
|
||||
// 2. 도커 내부의 상대 경로로 폴더 지정
|
||||
String uploadDirPath = "./upload/";
|
||||
File uploadDir = new File(uploadDirPath);
|
||||
|
||||
// 3. 폴더가 존재하지 않으면 자동으로 생성
|
||||
if (!uploadDir.exists()) {
|
||||
uploadDir.mkdirs();
|
||||
}
|
||||
|
||||
// 4. 결정된 파일명으로 최종 저장 경로 생성
|
||||
File targetFile = new File(uploadDir, fileName);
|
||||
|
||||
// 5. 파일 데이터 수신 및 쓰기
|
||||
try (InputStream is = request.getInputStream();
|
||||
FileOutputStream fos = new FileOutputStream(targetFile)) {
|
||||
|
||||
byte[] buffer = new byte[4096];
|
||||
int bytesRead;
|
||||
while ((bytesRead = is.read(buffer)) != -1) {
|
||||
fos.write(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println("✅ 파일 업로드 성공: " + targetFile.getAbsolutePath());
|
||||
return ResponseEntity.ok("Upload Success: " + fileName);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
|
||||
Reference in New Issue
Block a user