feat : 자료실 글 삭제 API 추가 (DELETE /api/media-posts/{id})
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 53s

- 글과 NAS 폴더(파일)를 함께 삭제, 본인 글만 가능
- 남의 글은 400 + 이유 (403이면 프론트가 로그인 만료로 처리해 로그아웃되므로)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
headporterandClaude Opus 5.5 committed 2026-11-27 17:09:30 +09:00
1 parent 73bcd872aa
commit f98cf0912d
2 files changed
+40

No files matched your search

@@ -27,6 +27,7 @@ import java.security.MessageDigest;
import java.security.Principal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -130,6 +131,26 @@ public class MediaPostController {
.orElse(ResponseEntity.notFound().build());
}
// 삭제: 글 + 폴더(파일) 함께. 본인 글만
// (403은 프론트가 "로그인 만료"로 처리해 로그아웃되므로 400 + 이유로 응답)
// ponytail: 받는 중에 지우면 봇이 폴더를 다시 만들어 파일이 남을 수 있음. 문제 되면 DOWNLOADING 중 삭제 막기
@DeleteMapping("/{id}")
public ResponseEntity<?> delete(@PathVariable Long id, Principal principal) throws IOException {
MediaPost post = repository.findById(id).orElse(null);
if (post == null) return ResponseEntity.notFound().build();
if (!post.getAuthor().equals(principal.getName())) {
return ResponseEntity.badRequest().body(Map.of("error", "본인이 등록한 글만 삭제할 수 있습니다"));
}
Path dir = mediaDir.resolve(post.getFolder()).normalize();
if (dir.getParent().equals(mediaDir) && Files.isDirectory(dir)) {
try (Stream<Path> walk = Files.walk(dir)) {
for (Path p : walk.sorted(Comparator.reverseOrder()).toList()) Files.delete(p); // 안쪽 파일부터
}
}
repository.delete(post);
return ResponseEntity.noContent().build();
}
// 파일 전송 (<video>/<img> src). 로그인 헤더 대신 ?t= 토큰 검사. Range 요청(영상 탐색) 자동 지원
@GetMapping("/files/{folder}/{index}")
public ResponseEntity<Resource> file(@PathVariable String folder, @PathVariable int index, @RequestParam String t) {