-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add thumbnail fetching functionality to SongFinder
This commit introduces the capability of fetching thumbnails for each song in the SongFinder application. This feature is realized through the addition of a new "FindThumbnailService" and supportive configurations, including a dedicated HTTP client "thumbnail-fetcher". This new functionality allows more detailed results to be presented to the users.
- Loading branch information
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/config/KtorClientConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package mikufan.cx.songfinder.backend.config | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.java.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.jackson.* | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
class KtorClientConfig { | ||
|
||
@Bean("thumbnail-fetcher") | ||
fun thumbnailFetcher(mapper: ObjectMapper) = HttpClient(Java) { | ||
install(ContentNegotiation) { | ||
// use spring jackson mapper | ||
register(ContentType.Application.Json, JacksonConverter(mapper)) | ||
} | ||
install(UserAgent) { | ||
agent = "Song Finder by VocaDB @ https://github.com/CXwudi/song-finder-vocadb" | ||
} | ||
// BrowserUserAgent() | ||
|
||
followRedirects = true | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
songfinder-app/src/main/kotlin/mikufan/cx/songfinder/backend/service/FindThumbnailService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mikufan.cx.songfinder.backend.service | ||
|
||
import mikufan.cx.songfinder.backend.component.thumbnailfinder.ThumbnailFinder | ||
import mikufan.cx.songfinder.backend.model.PVInfo | ||
import mikufan.cx.songfinder.backend.model.ThumbnailInfo | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class FindThumbnailService( | ||
thumbnailFinders: List<ThumbnailFinder>, | ||
) { | ||
|
||
private val finderMap = thumbnailFinders.associateBy { it.matchedPvService } | ||
|
||
suspend fun tryGetThumbnail(pv: PVInfo): Result<ThumbnailInfo> { | ||
val finder = finderMap[pv.pvService] | ||
return if (finder == null) { | ||
Result.failure(IllegalArgumentException("No thumbnail finder for pv service ${pv.pvService}")) | ||
} else { | ||
try { | ||
Result.success(finder.findThumbnail(pv)) | ||
} catch (e: Exception) { | ||
Result.failure(e) | ||
} | ||
} | ||
} | ||
|
||
fun evictCache() { | ||
TODO("return back here once spring cache is added") | ||
} | ||
} |