diff --git a/src/main/java/company/specialsource/controller/MediaPostController.java b/src/main/java/company/specialsource/controller/MediaPostController.java index f916511..20d8c9d 100644 --- a/src/main/java/company/specialsource/controller/MediaPostController.java +++ b/src/main/java/company/specialsource/controller/MediaPostController.java @@ -17,6 +17,7 @@ import org.springframework.http.MediaType; import org.springframework.http.MediaTypeFactory; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import org.springframework.web.client.RestClient; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestClientResponseException; @@ -130,6 +131,52 @@ public class MediaPostController { return ResponseEntity.ok(toResponse(post, false)); } + // 업로드: 내 PC 파일을 바로 글로 등록 (봇 거치지 않음 → 바로 DONE). 원본 링크가 없어서 sourceUrl = "" + // 파일을 다 저장한 뒤에 글을 만들고, 저장 중 실패하면 폴더를 지움 + @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity upload(@RequestParam("files") List files, + @RequestParam(value = "title", required = false) String title, + @RequestParam(value = "category", required = false) String category, + Principal principal) throws IOException { + files = files.stream().filter(f -> !f.isEmpty()).toList(); + if (files.isEmpty()) return ResponseEntity.badRequest().body(Map.of("error", "파일을 선택하세요")); + + LocalDateTime now = LocalDateTime.now(); + String folder = now.format(FOLDER_DATE) + "_" + UUID.randomUUID().toString().replace("-", "").substring(0, 12); + Path dir = mediaDir.resolve(folder); + Files.createDirectories(dir); + try { + for (int i = 0; i < files.size(); i++) { + // 경로 조각·윈도우 금지 문자 제거. 숨김(.)·빈 이름이면 번호 이름, 같은 이름이 있으면 앞에 번호 + String name = Paths.get(String.valueOf(files.get(i).getOriginalFilename())).getFileName().toString() + .replaceAll("[\\\\/:*?\"<>|]", "_").trim(); + if (name.isEmpty() || name.startsWith(".") || name.equals("null")) name = "file" + (i + 1) + name; + if (Files.exists(dir.resolve(name))) name = (i + 1) + "_" + name; + files.get(i).transferTo(dir.resolve(name)); + } + } catch (IOException e) { + try (Stream walk = Files.walk(dir)) { + for (Path p : walk.sorted(Comparator.reverseOrder()).toList()) Files.deleteIfExists(p); + } + return ResponseEntity.status(500).body(Map.of("error", "파일 저장에 실패했습니다")); + } + + // 제목을 비우면 첫 파일 이름 (확장자 빼고) + String t = title == null || title.isBlank() + ? listFiles(folder).stream().findFirst().map(p -> p.getFileName().toString().replaceFirst("\\.[^.]+$", "")).orElse("업로드") + : title.trim(); + MediaPost post = repository.save(MediaPost.builder() + .title(t.length() > 500 ? t.substring(0, 500) : t) + .sourceUrl("") + .category(normalizeCategory(category)) + .folder(folder) + .status(MediaPost.Status.DONE) + .author(principal.getName()) + .createdAt(now) + .build()); + return ResponseEntity.ok(toResponse(post, false)); + } + // 목록 + 검색. 조건은 준 것만 적용 // category: 없으면 전체, 빈 문자열("")이면 미분류 // q: 제목에 포함 (대소문자 무시), from/to: 등록일 범위 (yyyy-MM-dd, to 당일 포함) @@ -269,11 +316,11 @@ public class MediaPostController { post.setStatus(ok ? MediaPost.Status.DONE : MediaPost.Status.FAILED); String description = request.description() == null || request.description().isBlank() ? null : request.description().trim(); if (ok && description != null) post.setDescription(description); - // 제목을 안 적었으면(= 링크): 설명글 첫 줄(최대 100자), 없으면 첫 파일 이름으로 - // 봇 파일명 "제목_영상id_Video1.mp4" 에서 뒤쪽 제거 + // 제목을 안 적었으면(= 링크): 인스타는 설명글 첫 줄(최대 100자, 인스타엔 제목이 따로 없음), + // 그 밖(유튜브)이나 설명이 없으면 첫 파일 이름. 봇 파일명 "제목_영상id_Video1.mp4" 에서 뒤쪽 제거 // ponytail: 영상 id에 '_'가 있으면 일부가 제목에 남음. 거슬리면 봇이 콜백에 제목을 실어 보내게 if (ok && post.getTitle().equals(post.getSourceUrl())) { - if (description != null) { + if (description != null && post.getSourceUrl().contains("instagram.com")) { String first = description.lines().map(String::strip).filter(l -> !l.isEmpty()).findFirst().orElse(description); post.setTitle(first.length() > 100 ? first.substring(0, 100) + "…" : first); } else {