-
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.
Allow using "original" to get the original image file, add support fo…
…r jpeg files (downscaling not supported yet)
- Loading branch information
1 parent
989c1f8
commit 333cf86
Showing
3 changed files
with
123 additions
and
33 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
backend/src/main/kotlin/net/perfectdreams/etherealgambi/backend/utils/JPEGImageInfo.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,61 @@ | ||
package net.perfectdreams.etherealgambi.backend.utils | ||
|
||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import net.perfectdreams.etherealgambi.backend.EtherealGambi | ||
import net.perfectdreams.etherealgambi.data.DefaultImageVariant | ||
import net.perfectdreams.etherealgambi.data.DefaultImageVariantPreset | ||
import net.perfectdreams.etherealgambi.data.ImageInfoData | ||
import net.perfectdreams.etherealgambi.data.ImageType | ||
import net.perfectdreams.etherealgambi.data.ImageVariant | ||
import net.perfectdreams.etherealgambi.data.ImageVariantInfoData | ||
import net.perfectdreams.etherealgambi.data.ScaleDownToWidthImageVariant | ||
import java.io.File | ||
|
||
class JPEGImageInfo( | ||
m: EtherealGambi, | ||
data: ImageInfoData | ||
) : ImageInfo(m, data) { | ||
override fun getValidVariantPresets() = listOf(DefaultImageVariantPreset) | ||
|
||
override suspend fun createImageVariant( | ||
variant: ImageVariant | ||
): File { | ||
return mutexes.getOrPut(variant) { Mutex() }.withLock { | ||
val storedVariant = data.variants.firstOrNull { it.variantAttributes == variant } | ||
|
||
if (storedVariant != null) { | ||
val storedVariantFile = File(m.generatedFiles, storedVariant.path) | ||
if (storedVariant.optimizationVersion == EtherealGambi.OPTIMIZATION_VERSION && storedVariantFile.exists()) | ||
return@withLock storedVariantFile | ||
|
||
data.variants.removeIf { it.variantAttributes == variant } | ||
} | ||
|
||
val originalImageFile = File(m.files, path) | ||
val generatedImageFile = File(m.generatedFiles, "$folder/${fileName.name}${variant.variantWithPrefix()}.${variant.imageType.extension}") | ||
|
||
when (variant) { | ||
is DefaultImageVariant -> { | ||
generatedImageFile.parentFile.mkdirs() | ||
|
||
generatedImageFile.writeBytes(originalImageFile.readBytes()) | ||
|
||
// TODO: Use jpegoptim to optimize the GIF! | ||
|
||
data.variants.add( | ||
ImageVariantInfoData( | ||
EtherealGambi.OPTIMIZATION_VERSION, | ||
path, | ||
variant, | ||
generatedImageFile.length() | ||
) | ||
) | ||
|
||
return@withLock generatedImageFile | ||
} | ||
is ScaleDownToWidthImageVariant -> error("JPEG does not support scale down variations yet!") | ||
} | ||
} | ||
} | ||
} |