From f8bf46db241d2fe1e334e27161145e5043a5619c Mon Sep 17 00:00:00 2001 From: "sungjin.choi" Date: Mon, 30 Nov 2026 17:07:02 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EC=A3=BC=ED=81=AC=EB=B0=95=EC=8A=A4?= =?UTF-8?q?=20API=20(NAS=201001=20=EC=95=A8=EB=B2=94=20RAR=20=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=C2=B7=ED=8C=8C=EC=9D=BC,=20=EC=9D=BD=EA=B8=B0=20?= =?UTF-8?q?=EC=A0=84=EC=9A=A9=20=EB=A7=88=EC=9A=B4=ED=8A=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 --- .gitea/workflows/deploy.yml | 2 + .../controller/JukeboxController.java | 75 +++++++++++++++++++ src/main/resources/application.yaml | 3 + 3 files changed, 80 insertions(+) create mode 100644 src/main/java/company/specialsource/controller/JukeboxController.java diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 5ebbbdb..4a6f066 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -41,6 +41,8 @@ jobs: -v /volume1/docker/app/specialsource-backend/logs:/app/logs \ -v /volume2/data/media:/media \ -e MEDIA_DIR=/media \ + -v /home/sungjin.choi/Music/1001-albums-you-must-hear-before-you-die-collections:/music:ro \ + -e JUKEBOX_DIR=/music \ -e DOWNLOADER_URL=http://192.168.0.215:18090 \ -e DOWNLOADER_API_KEY=${{ secrets.DOWNLOADER_API_KEY }} \ -e MEDIA_CALLBACK_URL=http://192.168.0.215:18080/api/media-posts/callback \ diff --git a/src/main/java/company/specialsource/controller/JukeboxController.java b/src/main/java/company/specialsource/controller/JukeboxController.java new file mode 100644 index 0000000..2d0dc38 --- /dev/null +++ b/src/main/java/company/specialsource/controller/JukeboxController.java @@ -0,0 +1,75 @@ +package company.specialsource.controller; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.regex.Pattern; +import java.util.stream.Stream; + +/** + * 주크박스: 음악 폴더(NAS의 1001 앨범 RAR들, 읽기 전용 마운트)의 RAR 목록과 파일을 그대로 내려줌. + * 압축은 브라우저가 풂(node-unrar-js). 로그인한 회원만 (SecurityConfig의 anyRequest().authenticated()). + */ +@RestController +@RequestMapping("/api/jukebox") +public class JukeboxController { + + // 분할 압축의 2번째 조각부터(xxx.part2.rar ...)는 목록에서 뺌 → 브라우저에서 이어 붙일 수 없음 + private static final Pattern LATER_PART = Pattern.compile("(?i).*\\.part0*([2-9]|\\d{2,})\\.rar$"); + private static final Duration CACHE_TTL = Duration.ofMinutes(10); + + private final Path dir; + + private List cached = List.of(); + private Instant scannedAt = Instant.EPOCH; + + public JukeboxController(@Value("${jukebox.dir}") String dir) { + this.dir = Paths.get(dir).toAbsolutePath().normalize(); + } + + /** path = 음악 폴더 기준 상대 경로 (파일 받을 때 그대로 돌려줌) */ + public record Album(String path, String name, long size) {} + + // 앨범(RAR) 목록, 이름순. 10분 동안 메모리에 보관 + @GetMapping("/albums") + public synchronized List albums() throws IOException { + if (Instant.now().isBefore(scannedAt.plus(CACHE_TTL))) return cached; + if (!Files.isDirectory(dir)) return List.of(); + try (Stream files = Files.walk(dir)) { + cached = files + .filter(p -> Files.isRegularFile(p) && p.getFileName().toString().toLowerCase().endsWith(".rar")) + .filter(p -> !LATER_PART.matcher(p.getFileName().toString()).matches()) + .map(p -> { + String name = p.getFileName().toString().replaceAll("(?i)(\\.part0*1)?\\.rar$", ""); + return new Album(dir.relativize(p).toString().replace('\\', '/'), name, p.toFile().length()); + }) + .sorted((a, b) -> a.path().compareToIgnoreCase(b.path())) + .toList(); + } + scannedAt = Instant.now(); + return cached; + } + + // RAR 파일 그대로 (Range 요청 지원은 스프링이 처리). 음악 폴더 밖·RAR 아닌 파일은 거절 + @GetMapping("/file") + public ResponseEntity file(@RequestParam String path) { + Path file = dir.resolve(path).normalize(); + if (!file.startsWith(dir) || !file.getFileName().toString().toLowerCase().endsWith(".rar") || !Files.isRegularFile(file)) { + return ResponseEntity.notFound().build(); + } + return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).body(new FileSystemResource(file)); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 32254e4..2eb39d8 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -48,6 +48,9 @@ scraping: cleanup-initial-delay-ms: 60000 # 앱 기동 후 첫 정리 작업까지 대기 시간 cleanup-interval-ms: 86400000 # 정리 작업 주기 (24시간) +jukebox: + dir: ${JUKEBOX_DIR:./music} # 주크박스 음악 폴더 (1001 앨범 RAR, NAS 폴더를 읽기 전용 마운트) + media: dir: ${MEDIA_DIR:./media} # 다운로드 게시판 파일 폴더 (봇 컨테이너와 같은 NAS 폴더를 마운트) downloader-url: ${DOWNLOADER_URL:http://localhost:8090} # 디스코드 다운로드 봇 HTTP 창구