From da8a8f94d2d752b37696344d936483ec983a1e85 Mon Sep 17 00:00:00 2001 From: aarbit Date: Fri, 7 Jun 2024 22:52:02 -0500 Subject: [PATCH] Makes CORS config global. Revamps and adds exceptions. --- .../halfbinary/prettyplayerapi/config/WebConfig.kt | 14 ++++++++++++++ .../prettyplayerapi/controller/CacheController.kt | 6 +++--- .../prettyplayerapi/controller/ImageController.kt | 9 +++------ .../prettyplayerapi/controller/MusicController.kt | 3 +-- .../exception/AlbumFolderNotFoundException.kt | 4 ++++ .../exception/AlbumHashNotFoundException.kt | 2 +- .../EnvironmentVariableNotFoundException.kt | 2 +- 7 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/config/WebConfig.kt create mode 100644 src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumFolderNotFoundException.kt diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/config/WebConfig.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/config/WebConfig.kt new file mode 100644 index 0000000..b3e8fa9 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/config/WebConfig.kt @@ -0,0 +1,14 @@ +package net.halfbinary.prettyplayerapi.config + +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.CorsRegistry +import org.springframework.web.servlet.config.annotation.EnableWebMvc +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +@Configuration +@EnableWebMvc +class WebConfig: WebMvcConfigurer { + override fun addCorsMappings(registry: CorsRegistry) { + registry.addMapping("/**") + } +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt index 0334546..47213a0 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/CacheController.kt @@ -1,14 +1,14 @@ package net.halfbinary.prettyplayerapi.controller import net.halfbinary.prettyplayerapi.service.AlbumCacheService -import org.springframework.web.bind.annotation.CrossOrigin import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController -@CrossOrigin +@RequestMapping("cache") class CacheController(private val albumCacheService: AlbumCacheService) { - @GetMapping("cache") + @GetMapping suspend fun refreshAlbumCache() { albumCacheService.refreshAlbumCache() } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/ImageController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/ImageController.kt index 1fe1b87..34e0b36 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/ImageController.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/ImageController.kt @@ -2,15 +2,12 @@ package net.halfbinary.prettyplayerapi.controller import net.halfbinary.prettyplayerapi.service.ImageService import org.springframework.http.MediaType -import org.springframework.web.bind.annotation.CrossOrigin -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* @RestController -@CrossOrigin +@RequestMapping("image") class ImageController(private val imageService: ImageService) { - @GetMapping(value = ["image/{id}"], produces = [MediaType.IMAGE_JPEG_VALUE]) + @GetMapping(value = ["/{id}"], produces = [MediaType.IMAGE_JPEG_VALUE]) fun getImage(@PathVariable("id") id: String): ByteArray { return imageService.getImage(id).readBytes() } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/MusicController.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/MusicController.kt index 763e31c..558fb2d 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/MusicController.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/controller/MusicController.kt @@ -7,10 +7,9 @@ import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBo @RestController @RequestMapping("music") -@CrossOrigin class MusicController(private val musicService: MusicService) { - @GetMapping(value = ["/album/{albumHash}/{trackNumber}"], produces = ["audio/mpeg3"]) + @GetMapping(value = ["/album/{albumHash}/track/{trackNumber}"], produces = ["audio/mpeg3"]) fun getAlbum(@PathVariable albumHash: String, @PathVariable trackNumber: Int): ResponseEntity { val track = musicService.getMusic(albumHash, trackNumber) val responseBody = StreamingResponseBody { stream -> stream.write(track.readBytes()) } diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumFolderNotFoundException.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumFolderNotFoundException.kt new file mode 100644 index 0000000..ab38ce7 --- /dev/null +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumFolderNotFoundException.kt @@ -0,0 +1,4 @@ +package net.halfbinary.prettyplayerapi.exception + +class AlbumFolderNotFoundException(folder: String) : RuntimeException("Album folder $folder does not exist") { +} \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumHashNotFoundException.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumHashNotFoundException.kt index b82eff4..5889621 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumHashNotFoundException.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/AlbumHashNotFoundException.kt @@ -1,4 +1,4 @@ package net.halfbinary.prettyplayerapi.exception -class AlbumHashNotFoundException(message: String) : RuntimeException(message) { +class AlbumHashNotFoundException(hash: String) : RuntimeException("Album hash $hash not found") { } \ No newline at end of file diff --git a/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/EnvironmentVariableNotFoundException.kt b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/EnvironmentVariableNotFoundException.kt index f50dcb1..8280f6d 100644 --- a/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/EnvironmentVariableNotFoundException.kt +++ b/src/main/kotlin/net/halfbinary/prettyplayerapi/exception/EnvironmentVariableNotFoundException.kt @@ -1,4 +1,4 @@ package net.halfbinary.prettyplayerapi.exception -class EnvironmentVariableNotFoundException(message: String) : RuntimeException(message) { +class EnvironmentVariableNotFoundException(envVar: String) : RuntimeException("Environment variable $envVar does not exist, please specify") { } \ No newline at end of file