feat : 자료실 파일 받기·글 수정 API 추가
SpecialSource Backend CI/CD / build-and-deploy (push) Successful in 1m4s

- GET /api/media-posts/files/..?dl=1 : attachment로 응답 (원래 파일명 유지, 이어받기 지원)
- 상세 응답 파일에 size(바이트) 추가
- PUT /api/media-posts/{id} : 제목·분류 수정 (본인 글만, 제목 필수, 분류 비우면 미분류)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
headporterandClaude Opus 5.5 committed 2026-11-27 18:02:10 +09:00
1 parent 4521cab2a5
commit 4d8d1cfff6
3 files changed
+80 -7

No files matched your search

@@ -72,10 +72,12 @@ public class MediaPostController {
public record CreateRequest(String url, String title, String category) {}
public record UpdateRequest(String title, String category) {}
// result: VIDEO / IMAGE / FAIL = 끝남, PROGRESS = 받는 중 (progress %, speed)
public record CallbackRequest(String folder, String result, Integer progress, String speed) {}
public record FileItem(String name, String type, String url) {}
public record FileItem(String name, String type, long size, String url) {} // size: 바이트
public record MediaPostResponse(Long id, String title, String sourceUrl, String category, MediaPost.Status status,
Integer progress, String speed, String author, LocalDateTime createdAt,
@@ -92,8 +94,7 @@ public class MediaPostController {
}
String title = request.title() == null || request.title().isBlank() ? url : request.title().trim();
if (title.length() > 500) title = title.substring(0, 500);
String category = request.category() == null || request.category().isBlank() ? null : request.category().trim();
if (category != null && category.length() > 50) category = category.substring(0, 50);
String category = normalizeCategory(request.category());
LocalDateTime now = LocalDateTime.now();
MediaPost post = repository.save(MediaPost.builder()
@@ -173,6 +174,31 @@ public class MediaPostController {
.orElse(ResponseEntity.notFound().build());
}
// 수정: 제목·분류만 (링크를 바꾸려면 다시 받아야 하므로 삭제 후 새로 등록). 본인 글만, 이유는 delete와 같이 400
@PutMapping("/{id}")
public ResponseEntity<?> update(@PathVariable Long id, @RequestBody UpdateRequest request, Principal principal) {
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", "본인이 등록한 글만 수정할 수 있습니다"));
}
if (request.title() == null || request.title().isBlank()) {
return ResponseEntity.badRequest().body(Map.of("error", "제목을 입력하세요"));
}
String title = request.title().trim();
post.setTitle(title.length() > 500 ? title.substring(0, 500) : title);
post.setCategory(normalizeCategory(request.category()));
repository.save(post);
return ResponseEntity.ok(toResponse(post, true));
}
// 분류: 앞뒤 공백 제거, 비우면 미분류(null), 최대 50자
private static String normalizeCategory(String category) {
if (category == null || category.isBlank()) return null;
String c = category.trim();
return c.length() > 50 ? c.substring(0, 50) : c;
}
// 삭제: 글 + 폴더(파일) 함께. 본인 글만
// (403은 프론트가 "로그인 만료"로 처리해 로그아웃되므로 400 + 이유로 응답)
// ponytail: 받는 중에 지우면 봇이 폴더를 다시 만들어 파일이 남을 수 있음. 문제 되면 DOWNLOADING 중 삭제 막기
@@ -193,9 +219,11 @@ public class MediaPostController {
return ResponseEntity.noContent().build();
}
// 파일 전송 (<video>/<img> src). 로그인 헤더 대신 ?t= 토큰 검사. Range 요청(영상 탐색) 자동 지원
// 파일 전송 (<video>/<img> src). 로그인 헤더 대신 ?t= 토큰 검사. Range 요청(영상 탐색·이어받기) 자동 지원
// dl=1 이면 브라우저가 재생하지 않고 파일로 저장 (attachment)
@GetMapping("/files/{folder}/{index}")
public ResponseEntity<Resource> file(@PathVariable String folder, @PathVariable int index, @RequestParam String t) {
public ResponseEntity<Resource> file(@PathVariable String folder, @PathVariable int index, @RequestParam String t,
@RequestParam(defaultValue = "false") boolean dl) {
if (!jwtTokenProvider.validateMediaToken(t, folder)) {
return ResponseEntity.status(403).build();
}
@@ -207,7 +235,8 @@ public class MediaPostController {
String name = file.getFileName().toString();
return ResponseEntity.ok()
.contentType(MediaTypeFactory.getMediaType(name).orElse(MediaType.APPLICATION_OCTET_STREAM))
.header(HttpHeaders.CONTENT_DISPOSITION, ContentDisposition.inline().filename(name, StandardCharsets.UTF_8).build().toString())
.header(HttpHeaders.CONTENT_DISPOSITION, (dl ? ContentDisposition.attachment() : ContentDisposition.inline())
.filename(name, StandardCharsets.UTF_8).build().toString())
.body(new FileSystemResource(file));
}
@@ -256,7 +285,13 @@ public class MediaPostController {
for (int i = 0; i < files.size(); i++) {
String name = files.get(i).getFileName().toString();
String type = MediaTypeFactory.getMediaType(name).map(MediaType::getType).orElse("file"); // video / image / ...
items.add(new FileItem(name, type, "/api/media-posts/files/" + post.getFolder() + "/" + i + "?t=" + token));
long size;
try {
size = Files.size(files.get(i));
} catch (IOException e) {
size = -1; // 그 사이 지워졌으면 크기만 모름
}
items.add(new FileItem(name, type, size, "/api/media-posts/files/" + post.getFolder() + "/" + i + "?t=" + token));
}
}
return new MediaPostResponse(post.getId(), post.getTitle(), post.getSourceUrl(), post.getCategory(),