diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 4a6f066..5ebbbdb 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -41,8 +41,6 @@ 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 deleted file mode 100644 index 2d0dc38..0000000 --- a/src/main/java/company/specialsource/controller/JukeboxController.java +++ /dev/null @@ -1,75 +0,0 @@ -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 2eb39d8..32254e4 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -48,9 +48,6 @@ 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 창구 diff --git a/src/test/java/company/specialsource/controller/JukeboxControllerTest.java b/src/test/java/company/specialsource/controller/JukeboxControllerTest.java deleted file mode 100644 index 3936fc7..0000000 --- a/src/test/java/company/specialsource/controller/JukeboxControllerTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package company.specialsource.controller; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.io.TempDir; - -import java.nio.file.Files; -import java.nio.file.Path; - -import static org.assertj.core.api.Assertions.assertThat; - -/** 주크박스 파일 받기: 음악 폴더 밖이나 RAR이 아닌 파일은 절대 내주지 않음 */ -class JukeboxControllerTest { - - @Test - void 음악_폴더_밖은_거절(@TempDir Path root) throws Exception { - Path music = Files.createDirectories(root.resolve("music")); - Files.createDirectories(music.resolve("sub")); - Files.writeString(music.resolve("sub/ok.rar"), "rar"); - Files.writeString(music.resolve("note.txt"), "txt"); - Files.writeString(root.resolve("secret.rar"), "secret"); // 음악 폴더 바깥 - JukeboxController c = new JukeboxController(music.toString()); - - assertThat(c.file("sub/ok.rar").getStatusCode().value()).isEqualTo(200); - assertThat(c.file("../secret.rar").getStatusCode().value()).isEqualTo(404); - assertThat(c.file("sub/../../secret.rar").getStatusCode().value()).isEqualTo(404); - assertThat(c.file(root.resolve("secret.rar").toString()).getStatusCode().value()).isEqualTo(404); // 절대 경로 - assertThat(c.file("note.txt").getStatusCode().value()).isEqualTo(404); // RAR 아님 - assertThat(c.file("sub").getStatusCode().value()).isEqualTo(404); // 폴더 - assertThat(c.file("없는파일.rar").getStatusCode().value()).isEqualTo(404); - } -}