Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #306

Merged
merged 3 commits into from
Mar 24, 2024
Merged

Dev #306

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/update-gradle-wrapper.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ jobs:
- name: Update Gradle Wrapper
uses: gradle-update/update-gradle-wrapper-action@v1
with:
reviewers: Ziedelth
reviewers: Ziedelth
base-branch: dev
target-branch: dev
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionSha256Sum=9631d53cf3e74bfa726893aee1f8994fee4e060c401335946dba2156f440f24c
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionSha256Sum=544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/fr/shikkanime/services/AnimeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ class AnimeService : AbstractService<Anime, AnimeRepository>() {
}.toList()
}

fun addImage(uuid: UUID, image: String) {
ImageService.add(uuid, ImageService.Type.IMAGE, image, 480, 720)
fun addImage(uuid: UUID, image: String, bypass: Boolean = false) {
ImageService.add(uuid, ImageService.Type.IMAGE, image, 480, 720, bypass)
}

fun addBanner(uuid: UUID, image: String?) {
fun addBanner(uuid: UUID, image: String?, bypass: Boolean = false) {
if (image.isNullOrBlank()) return
ImageService.add(uuid, ImageService.Type.BANNER, image, 640, 360)
ImageService.add(uuid, ImageService.Type.BANNER, image, 640, 360, bypass)
}

override fun save(entity: Anime): Anime {
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/fr/shikkanime/services/EpisodeService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class EpisodeService : AbstractService<Episode, EpisodeRepository>() {
blacklisted: List<UUID>
) = episodeRepository.findAllByDateRange(countryCode, start, end, blacklisted)

fun addImage(uuid: UUID, image: String) {
ImageService.add(uuid, ImageService.Type.IMAGE, image, 640, 360)
fun addImage(uuid: UUID, image: String, bypass: Boolean = false) {
ImageService.add(uuid, ImageService.Type.IMAGE, image, 640, 360, bypass)
}

fun getSimulcast(anime: Anime, entity: Episode): Simulcast {
Expand Down
28 changes: 18 additions & 10 deletions src/main/kotlin/fr/shikkanime/services/ImageService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,22 @@ object ImageService {
)
}

fun add(uuid: UUID, type: Type, url: String, width: Int, height: Int) {
if (get(uuid, type) != null || url.isBlank()) {
fun add(uuid: UUID, type: Type, url: String, width: Int, height: Int, bypass: Boolean = false) {
if (!bypass && (get(uuid, type) != null || url.isBlank())) {
return
}

val image = Image(uuid.toString(), type, url)
cache.add(image)
val image = if (!bypass) {
val image = Image(uuid.toString(), type, url)
cache.add(image)
image
} else {
get(uuid, type) ?: run {
val image = Image(uuid.toString(), type, url)
cache.add(image)
image
}
}

threadPool.submit { encodeImage(url, uuid, type, width, height, image) }
}
Expand Down Expand Up @@ -232,25 +241,24 @@ object ImageService {
get() = toHumanReadable(cache.sumOf { it.size })

fun invalidate() {
cache.clear()
change.set(true)
addAll()
addAll(true)
}

fun addAll() {
fun addAll(bypass: Boolean = false) {
val animeService = Constant.injector.getInstance(AnimeService::class.java)
val episodeService = Constant.injector.getInstance(EpisodeService::class.java)

animeService.findAllUUIDAndImage().forEach {
val uuid = it[0] as UUID
animeService.addImage(uuid, it[1] as String)
animeService.addBanner(uuid, it[2] as String?)
animeService.addImage(uuid, it[1] as String, bypass)
animeService.addBanner(uuid, it[2] as String?, bypass)
}

episodeService.findAllUUIDAndImage().forEach {
episodeService.addImage(
it[0] as UUID,
(it[1] as String?)?.ifBlank { null } ?: Constant.DEFAULT_IMAGE_PREVIEW,
bypass
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/admin/dashboard.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
<div class="card p-3">
<div class="d-flex mb-4">
<h4 class="card-title">Images</h4>
<a href="/admin/images-invalidate" class="btn btn-danger ms-auto me-0">Invalidate</a>
<a id="images-invalidate" href="/admin/images-invalidate" class="btn btn-danger ms-auto me-0">Invalidate</a>
<a href="/admin/images-save" class="btn btn-success ms-2 me-0">Save</a>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.module
import fr.shikkanime.services.AnimeService
import fr.shikkanime.services.EpisodeService
import fr.shikkanime.services.ImageService
import fr.shikkanime.services.SimulcastService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.ObjectParser
Expand Down Expand Up @@ -70,6 +71,9 @@ internal class AnimeControllerTest {
animeService.deleteAll()
simulcastService.deleteAll()
animeService.preIndex()
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fr.shikkanime.dtos.AnimeDto
import fr.shikkanime.entities.Anime
import fr.shikkanime.module
import fr.shikkanime.services.AnimeService
import fr.shikkanime.services.ImageService
import fr.shikkanime.services.SimulcastService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.ObjectParser
Expand All @@ -31,7 +32,7 @@ class AttachmentControllerTest {
fun setUp() {
Constant.injector.injectMembers(this)

val listFiles = File(ClassLoader.getSystemClassLoader().getResource("animes")?.file).listFiles()
val listFiles = ClassLoader.getSystemClassLoader().getResource("animes")?.file?.let { File(it).listFiles() }

listFiles
?.sortedBy { it.name.lowercase() }
Expand All @@ -52,6 +53,9 @@ class AttachmentControllerTest {
animeService.deleteAll()
simulcastService.deleteAll()
animeService.preIndex()
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import fr.shikkanime.entities.enums.Platform
import fr.shikkanime.module
import fr.shikkanime.services.AnimeService
import fr.shikkanime.services.EpisodeService
import fr.shikkanime.services.ImageService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.ObjectParser
import io.ktor.client.request.*
Expand Down Expand Up @@ -88,6 +89,9 @@ class EpisodeControllerTest {
fun tearDown() {
episodeService.deleteAll()
animeService.deleteAll()
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}

@Test
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/fr/shikkanime/services/EpisodeServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class EpisodeServiceTest {
episodeService.deleteAll()
animeService.deleteAll()
simulcastService.deleteAll()
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}

@Test
Expand Down
59 changes: 59 additions & 0 deletions src/test/kotlin/fr/shikkanime/services/ImageServiceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,63 @@ class ImageServiceTest {
val image = ImageService.toEpisodeImage(dto)
assertNotNull(image)
}

@Test
fun `add bypass images`() {
val uuid1 = UUID.randomUUID()
val uuid2 = UUID.randomUUID()
ImageService.add(
uuid1,
ImageService.Type.IMAGE,
"https://www.crunchyroll.com/imgsrv/display/thumbnail/1920x1080/catalog/crunchyroll/efd4a6f29ce3b03a6f9d14d818b804bf.jpe",
1920,
1080,
true
)
ImageService.add(
uuid2,
ImageService.Type.IMAGE,
"https://www.crunchyroll.com/imgsrv/display/thumbnail/1920x1080/catalog/crunchyroll/1e9c79ce0d5cebd2670bddd2a27cd9dd.jpe",
1920,
1080,
true
)
Thread.sleep(5000)
ImageService.add(
uuid1,
ImageService.Type.IMAGE,
"https://www.crunchyroll.com/imgsrv/display/thumbnail/1920x1080/catalog/crunchyroll/efd4a6f29ce3b03a6f9d14d818b804bf.jpe",
1920,
1080,
true
)
ImageService.add(
uuid2,
ImageService.Type.IMAGE,
"https://www.crunchyroll.com/imgsrv/display/thumbnail/1920x1080/catalog/crunchyroll/1e9c79ce0d5cebd2670bddd2a27cd9dd.jpe",
1920,
1080,
true
)
Thread.sleep(5000)
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}

@Test
fun `invalid images`() {
Thread.sleep(5000)
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()

Thread.sleep(5000)
ImageService.invalidate()

Thread.sleep(5000)
ImageService.cache.clear()
ImageService.change.set(true)
ImageService.saveCache()
}
}
Loading