diff --git a/src/main/java/company/specialsource/controller/FileSyncController.java b/src/main/java/company/specialsource/controller/FileSyncController.java index 72cdf67..c3425f0 100644 --- a/src/main/java/company/specialsource/controller/FileSyncController.java +++ b/src/main/java/company/specialsource/controller/FileSyncController.java @@ -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();