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

[ImgBot] Optimize images #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,13 @@ jobs:
- name: Build with Gradle
run: "./gradlew build -Dgithub.build.id=$GITHUB_RUN_ID -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"

- name: Publish to PerfectDreams' repository
if: github.ref == 'refs/heads/master'
env:
USERNAME: ${{ secrets.PERFECTDREAMS_REPO_USERNAME }}
PASSWORD: ${{ secrets.PERFECTDREAMS_REPO_PASSWORD }}
run: "./gradlew publish -DUSERNAME=$USERNAME -DPASSWORD=$PASSWORD"

- name: Publish to Docker
if: github.ref == 'refs/heads/master'
run: "./gradlew :image-generation-server:jib -Dgithub.build.id=$GITHUB_RUN_ID -Dbuild.number=$GITHUB_RUN_NUMBER -Dgit.branch=$GITHUB_REF -Dcommit.hash=$GITHUB_SHA -Dcompiled.at=${{ steps.current-time.outputs.time }}"
27 changes: 26 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
plugins {
`maven-publish`
}

group = "net.perfectdreams.gabrielaimageserver"
version = Versions.GABRIELA_IMAGE_SERVER

allprojects {
repositories {
mavenCentral()
jcenter()
}
}

subprojects {
apply<MavenPublishPlugin>()
version = "0.0.10-SNAPSHOT"

publishing {
repositories {
maven {
name = "PerfectDreams"
url = uri("https://repo.perfectdreams.net/")

credentials {
username = System.getProperty("USERNAME") ?: System.getenv("USERNAME")
password = System.getProperty("PASSWORD") ?: System.getenv("PASSWORD")
}
}
}
}
}
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
}
8 changes: 8 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Versions {
const val GABRIELA_IMAGE_SERVER = "2.0.0-SNAPSHOT"
const val KOTLIN = "1.5.31"
const val JIB = "3.1.4"
const val KOTLINX_SERIALIZATION = "1.3.0"
const val PROMETHEUS = "0.12.0"
const val KTOR = "1.6.3"
}
41 changes: 41 additions & 0 deletions client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
plugins {
kotlin("multiplatform") version Versions.KOTLIN
kotlin("plugin.serialization") version Versions.KOTLIN
`maven-publish`
}

group = "net.perfectdreams.gabrielaimageserver"
version = Versions.GABRIELA_IMAGE_SERVER

repositories {
mavenCentral()
}

kotlin {
jvm {
compilations.all {
kotlinOptions.jvmTarget = "11"
}

withJava()
}

sourceSets {
commonMain {
dependencies {
api(project(":common"))

api("io.ktor:ktor-client-core:${Versions.KTOR}")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:${Versions.KOTLINX_SERIALIZATION}")
}
}
}
}

publishing {
publications {
register("PerfectDreams", MavenPublication::class.java) {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package net.perfectdreams.gabrielaimageserver.client

import io.ktor.client.*
import net.perfectdreams.gabrielaimageserver.client.services.ImagesService
import net.perfectdreams.gabrielaimageserver.client.services.VideosService

/**
* Client for [GabrielaImageServer](https://github.com/LorittaBot/GabrielaImageGen)
*
* While the requests themselves are very simple, this is useful to wrap errors and exceptions.
*
* @param baseUrl the URL of the image server, example: https://gabriela.loritta.website
* @param http the http client that will be used for requests
*/
class GabrielaImageServerClient(baseUrl: String, val http: HttpClient) {
val baseUrl = baseUrl.removeSuffix("/") // Remove trailing slash

val images = ImagesService(this)
val videos = VideosService(this)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package net.perfectdreams.gabrielaimageserver.client.services

import net.perfectdreams.gabrielaimageserver.client.GabrielaImageServerClient
import net.perfectdreams.gabrielaimageserver.data.CortesFlowRequest
import net.perfectdreams.gabrielaimageserver.data.ManiaTitleCardRequest
import net.perfectdreams.gabrielaimageserver.data.PetPetRequest
import net.perfectdreams.gabrielaimageserver.data.SAMLogoRequest
import net.perfectdreams.gabrielaimageserver.data.ShipRequest
import net.perfectdreams.gabrielaimageserver.data.SingleImageRequest
import net.perfectdreams.gabrielaimageserver.data.TerminatorAnimeRequest
import net.perfectdreams.gabrielaimageserver.data.TobyTextBoxRequest
import net.perfectdreams.gabrielaimageserver.data.TwoImagesRequest

class ImagesService(client: GabrielaImageServerClient) : Service(client) {
suspend fun ship(request: ShipRequest) = execute("/images/ship", request)
suspend fun petPet(request: PetPetRequest) = execute("/images/pet-pet", request)
suspend fun maniaTitleCard(request: ManiaTitleCardRequest) = execute("/images/mania-title-card", request)
suspend fun tobyTextBox(request: TobyTextBoxRequest) = execute("/images/toby-text-box", request)

suspend fun invertColors(request: SingleImageRequest) = execute("/images/invert-colors", request)
suspend fun toBeContinued(request: SingleImageRequest) = execute("/images/to-be-continued", request)
suspend fun cepoDeMadeira(request: SingleImageRequest) = execute("/images/cepo-de-madeira", request)
suspend fun knucklesThrow(request: SingleImageRequest) = execute("/images/knuckles-throw", request)
suspend fun nichijouYuukoPaper(request: SingleImageRequest) = execute("/images/nichijou-yuuko-paper", request)
suspend fun getOverHere(request: SingleImageRequest) = execute("/images/get-over-here", request)
suspend fun drake(request: TwoImagesRequest) = execute("/images/drake", request)
suspend fun bolsoDrake(request: TwoImagesRequest) = execute("/images/bolso-drake", request)
suspend fun loriDrake(request: TwoImagesRequest) = execute("/images/lori-drake", request)
suspend fun trump(request: TwoImagesRequest) = execute("/images/trump", request)
suspend fun samLogo(request: SAMLogoRequest) = execute("/images/sam", request)
suspend fun terminatorAnime(request: TerminatorAnimeRequest) = execute("/images/terminator-anime", request)
suspend fun cortesFlow(path: String, request: CortesFlowRequest) = execute("/images/cortes-flow/$path", request)

// ===[ SCALED IMAGES (with one single image) GENERATORS ]===
suspend fun pepeDream(request: SingleImageRequest) = execute("/images/pepe-dream", request)
suspend fun loriScared(request: SingleImageRequest) = execute("/images/lori-scared", request)
suspend fun studiopolisTv(request: SingleImageRequest) = execute("/images/studiopolis-tv", request)

// ===[ SKEWED IMAGES (with one single image) GENERATORS ]===
suspend fun art(request: SingleImageRequest) = execute("/images/art", request)
suspend fun bobBurningPaper(request: SingleImageRequest) = execute("/images/bob-burning-paper", request)
suspend fun bolsoFrame(request: SingleImageRequest) = execute("/images/bolso-frame", request)
suspend fun bolsonaro(request: SingleImageRequest) = execute("/images/bolsonaro", request)
suspend fun bolsonaro2(request: SingleImageRequest) = execute("/images/bolsonaro2", request)
suspend fun briggsCover(request: SingleImageRequest) = execute("/images/briggs-cover", request)
suspend fun canellaDvd(request: SingleImageRequest) = execute("/images/canella-dvd", request)
suspend fun chicoAta(request: SingleImageRequest) = execute("/images/chico-ata", request)
suspend fun ednaldoBandeira(request: SingleImageRequest) = execute("/images/ednaldo-bandeira", request)
suspend fun ednaldoTv(request: SingleImageRequest) = execute("/images/ednaldo-tv", request)
suspend fun gessyAta(request: SingleImageRequest) = execute("/images/gessy-ata", request)
suspend fun loriAta(request: SingleImageRequest) = execute("/images/lori-ata", request)
suspend fun loriSign(request: SingleImageRequest) = execute("/images/lori-sign", request)
suspend fun monicaAta(request: SingleImageRequest) = execute("/images/monica-ata", request)
suspend fun passingPaper(request: SingleImageRequest) = execute("/images/passing-paper", request)
suspend fun romeroBritto(request: SingleImageRequest) = execute("/images/romero-britto", request)
suspend fun wolverineFrame(request: SingleImageRequest) = execute("/images/wolverine-frame", request)
suspend fun ripTv(request: SingleImageRequest) = execute("/images/rip-tv", request)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.perfectdreams.gabrielaimageserver.client.services

import io.ktor.client.call.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import net.perfectdreams.gabrielaimageserver.client.GabrielaImageServerClient
import net.perfectdreams.gabrielaimageserver.data.ContentLengthTooLargeExceptionResponse
import net.perfectdreams.gabrielaimageserver.data.ErrorResponse
import net.perfectdreams.gabrielaimageserver.data.ImageNotFoundExceptionResponse
import net.perfectdreams.gabrielaimageserver.data.ImageTooLargeExceptionResponse
import net.perfectdreams.gabrielaimageserver.data.InternalServerErrorExceptionResponse
import net.perfectdreams.gabrielaimageserver.data.StreamExceedsLimitExceptionResponse
import net.perfectdreams.gabrielaimageserver.data.UntrustedURLExceptionResponse
import net.perfectdreams.gabrielaimageserver.exceptions.ContentLengthTooLargeException
import net.perfectdreams.gabrielaimageserver.exceptions.ImageNotFoundException
import net.perfectdreams.gabrielaimageserver.exceptions.ImageTooLargeException
import net.perfectdreams.gabrielaimageserver.exceptions.InternalServerErrorException
import net.perfectdreams.gabrielaimageserver.exceptions.StreamExceedsLimitException
import net.perfectdreams.gabrielaimageserver.exceptions.UntrustedURLException

open class Service(private val client: GabrielaImageServerClient) {
private val apiVersion = "v2"

suspend inline fun <reified T> execute(endpoint: String, body: T) = execute(endpoint, Json.encodeToJsonElement<T>(body).jsonObject)

suspend fun execute(endpoint: String, body: JsonObject): ByteArray {
val response = client.http.post<HttpResponse>("${client.baseUrl}/api/$apiVersion$endpoint") {
this.body = body.toString()
}

// If the status code is not between 400..499, then it means that it was (probably) a invalid input or something
if (response.status.value !in 200..299) {
// If it wasn't successful, let's try parsing the response!
val errorResponse = Json.decodeFromString<ErrorResponse>(response.readText())

when (errorResponse) {
is ContentLengthTooLargeExceptionResponse -> throw ContentLengthTooLargeException()
is ImageNotFoundExceptionResponse -> throw ImageNotFoundException()
is ImageTooLargeExceptionResponse -> throw ImageTooLargeException()
is InternalServerErrorExceptionResponse -> throw InternalServerErrorException()
is StreamExceedsLimitExceptionResponse -> throw StreamExceedsLimitException()
is UntrustedURLExceptionResponse -> throw UntrustedURLException(errorResponse.url)
}
}

return response.receive()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.perfectdreams.gabrielaimageserver.client.services

import net.perfectdreams.gabrielaimageserver.client.GabrielaImageServerClient
import net.perfectdreams.gabrielaimageserver.data.CocieloChavesRequest
import net.perfectdreams.gabrielaimageserver.data.FansExplainingRequest
import net.perfectdreams.gabrielaimageserver.data.SingleImageRequest

class VideosService(client: GabrielaImageServerClient) : Service(client) {
suspend fun attackOnHeart(request: SingleImageRequest) = execute("/videos/attack-on-heart", request)
suspend fun carlyAaah(request: SingleImageRequest) = execute("/videos/carly-aaah", request)
suspend fun fansExplaining(request: FansExplainingRequest) = execute("/videos/fans-explaining", request)
suspend fun cocieloChaves(request: CocieloChavesRequest) = execute("/videos/cocielo-chaves", request)
}
31 changes: 15 additions & 16 deletions gabriela-image-api/build.gradle.kts → common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("multiplatform") version "1.4.20-M2"
kotlin("multiplatform") version "1.5.31"
kotlin("plugin.serialization") version "1.5.31"
`maven-publish`
}

group = "net.perfectdreams.imagegen"
Expand All @@ -18,26 +18,17 @@ kotlin {
}
withJava()
}
js {
browser {
binaries.executable()
testTask {
useKarma {
useChromeHeadless()
}
}
}
}

sourceSets {
jvm().compilations["main"].defaultSourceSet {
commonMain {
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.3.0")
}
}

js().compilations["main"].defaultSourceSet {
jvm().compilations["main"].defaultSourceSet {
dependencies {
implementation(npm("buffer", "5.6.1"))

}
}

Expand All @@ -51,4 +42,12 @@ kotlin {
}
}
}
}

publishing {
publications {
register("PerfectDreams", MavenPublication::class.java) {
from(components["java"])
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.perfectdreams.gabrielaimageserver.data

import kotlinx.serialization.Serializable

@Serializable
data class CocieloChavesRequest(
val image1: SourceImageData,
val image2: SourceImageData,
val image3: SourceImageData,
val image4: SourceImageData,
val image5: SourceImageData,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package net.perfectdreams.gabrielaimageserver.data

import kotlinx.serialization.Serializable

@Serializable
data class CortesFlowRequest(
val text: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package net.perfectdreams.gabrielaimageserver.data

import kotlinx.serialization.Serializable

@Serializable
sealed class ErrorResponse {
abstract val reason: String?
}

@Serializable
data class ContentLengthTooLargeExceptionResponse(override val reason: String?) : ErrorResponse()

@Serializable
data class ImageTooLargeExceptionResponse(override val reason: String?) : ErrorResponse()

@Serializable
data class StreamExceedsLimitExceptionResponse(override val reason: String?) : ErrorResponse()

@Serializable
data class UntrustedURLExceptionResponse(override val reason: String?, val url: String) : ErrorResponse()

@Serializable
data class ImageNotFoundExceptionResponse(override val reason: String?) : ErrorResponse()

@Serializable
data class InternalServerErrorExceptionResponse(override val reason: String?) : ErrorResponse()
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.perfectdreams.gabrielaimageserver.data

import kotlinx.serialization.Serializable

@Serializable
data class FansExplainingRequest(
val section1Line1: String,
val section1Line2: String,

val section2Line1: String,
val section2Line2: String,

val section3Line1: String,
val section3Line2: String,

val section4Line1: String,
val section4Line2: String,

val section5Line1: String,
val section5Line2: String
)
Loading