Fix plugin logo not loading in standalone build

This commit is contained in:
grimsi
2025-04-02 18:29:24 +02:00
parent d3d922146a
commit 7fce903c0e
4 changed files with 12 additions and 14 deletions
@@ -1,13 +1,12 @@
package de.grimsi.gameyfin.core package de.grimsi.gameyfin.core
import org.apache.tika.Tika import org.apache.tika.Tika
import org.springframework.core.io.InputStreamResource import org.springframework.core.io.ByteArrayResource
import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity import org.springframework.http.ResponseEntity
import org.springframework.web.context.request.RequestContextHolder import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes import org.springframework.web.context.request.ServletRequestAttributes
import java.io.InputStream
class Utils { class Utils {
@@ -32,21 +31,21 @@ class Utils {
} }
} }
fun inputStreamToResponseEntity(stream: InputStream?): ResponseEntity<InputStreamResource> { fun inputStreamToResponseEntity(bytes: ByteArray?): ResponseEntity<ByteArrayResource> {
if (stream == null) return ResponseEntity.notFound().build() if (bytes == null) return ResponseEntity.notFound().build()
val inputStreamResource = InputStreamResource(stream) val byteArrayResource = ByteArrayResource(bytes)
val headers = HttpHeaders() val headers = HttpHeaders()
val contentLength = stream.available().toLong() val contentLength = bytes.size.toLong()
val contentType = tika.detect(stream) val contentType = tika.detect(bytes)
headers.contentLength = contentLength headers.contentLength = contentLength
headers.contentType = MediaType.parseMediaType(contentType) headers.contentType = MediaType.parseMediaType(contentType)
return ResponseEntity.ok() return ResponseEntity.ok()
.headers(headers) .headers(headers)
.body(inputStreamResource) .body(byteArrayResource)
} }
} }
} }
@@ -6,7 +6,6 @@ import org.pf4j.ExtensionPoint
import org.pf4j.PluginWrapper import org.pf4j.PluginWrapper
import org.springframework.data.repository.findByIdOrNull import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service import org.springframework.stereotype.Service
import java.io.InputStream
@Service @Service
class PluginManagementService( class PluginManagementService(
@@ -71,7 +70,7 @@ class PluginManagementService(
} }
} }
fun getLogo(pluginId: String): InputStream? { fun getLogo(pluginId: String): ByteArray? {
val plugin = pluginManager.getPlugin(pluginId).plugin as GameyfinPlugin val plugin = pluginManager.getPlugin(pluginId).plugin as GameyfinPlugin
return plugin.getLogo() return plugin.getLogo()
} }
@@ -8,6 +8,7 @@ import de.grimsi.gameyfin.games.entities.Image
import de.grimsi.gameyfin.games.entities.ImageType import de.grimsi.gameyfin.games.entities.ImageType
import de.grimsi.gameyfin.users.UserService import de.grimsi.gameyfin.users.UserService
import jakarta.annotation.security.RolesAllowed import jakarta.annotation.security.RolesAllowed
import org.springframework.core.io.ByteArrayResource
import org.springframework.core.io.InputStreamResource import org.springframework.core.io.InputStreamResource
import org.springframework.http.HttpHeaders import org.springframework.http.HttpHeaders
import org.springframework.http.MediaType import org.springframework.http.MediaType
@@ -37,7 +38,7 @@ class ImageEndpoint(
} }
@GetMapping("/plugins/{id}/logo") @GetMapping("/plugins/{id}/logo")
fun getPluginLogo(@PathVariable("id") pluginId: String): ResponseEntity<InputStreamResource>? { fun getPluginLogo(@PathVariable("id") pluginId: String): ResponseEntity<ByteArrayResource>? {
val logo = pluginManagementService.getLogo(pluginId) val logo = pluginManagementService.getLogo(pluginId)
return Utils.inputStreamToResponseEntity(logo) return Utils.inputStreamToResponseEntity(logo)
} }
@@ -2,7 +2,6 @@ package de.grimsi.gameyfin.pluginapi.core
import org.pf4j.Plugin import org.pf4j.Plugin
import org.pf4j.PluginWrapper import org.pf4j.PluginWrapper
import java.io.InputStream
abstract class GameyfinPlugin(wrapper: PluginWrapper) : Plugin(wrapper) { abstract class GameyfinPlugin(wrapper: PluginWrapper) : Plugin(wrapper) {
@@ -40,12 +39,12 @@ abstract class GameyfinPlugin(wrapper: PluginWrapper) : Plugin(wrapper) {
return false return false
} }
fun getLogo(): InputStream? { fun getLogo(): ByteArray? {
for (format in SUPPORTED_LOGO_FORMATS) { for (format in SUPPORTED_LOGO_FORMATS) {
val resourcePath = "$LOGO_FILE_NAME.$format" val resourcePath = "$LOGO_FILE_NAME.$format"
val inputStream = wrapper.pluginClassLoader.getResourceAsStream(resourcePath) val inputStream = wrapper.pluginClassLoader.getResourceAsStream(resourcePath)
if (inputStream != null) { if (inputStream != null) {
return inputStream return inputStream.readAllBytes()
} }
} }