|
1 | 1 | package net.cakeyfox.marisastuff |
2 | 2 |
|
| 3 | +import io.ktor.http.ContentType |
| 4 | +import io.ktor.http.HttpStatusCode |
| 5 | +import io.ktor.http.fromFilePath |
3 | 6 | import io.ktor.serialization.kotlinx.json.* |
4 | 7 | import io.ktor.server.application.* |
5 | 8 | import io.ktor.server.engine.* |
6 | 9 | import io.ktor.server.http.content.* |
7 | 10 | import io.ktor.server.netty.* |
| 11 | +import io.ktor.server.plugins.compression.Compression |
| 12 | +import io.ktor.server.plugins.compression.deflate |
| 13 | +import io.ktor.server.plugins.compression.gzip |
| 14 | +import io.ktor.server.plugins.compression.minimumSize |
8 | 15 | import io.ktor.server.plugins.contentnegotiation.* |
| 16 | +import io.ktor.server.response.respond |
| 17 | +import io.ktor.server.response.respondBytes |
| 18 | +import io.ktor.server.response.respondFile |
9 | 19 | import io.ktor.server.routing.* |
10 | 20 | import net.cakeyfox.marisastuff.config.ServerConfig |
| 21 | +import net.coobird.thumbnailator.Thumbnails |
| 22 | +import java.io.ByteArrayOutputStream |
11 | 23 | import java.io.File |
12 | 24 |
|
13 | 25 | class MarisaStuffServerInstance( |
14 | 26 | config: ServerConfig |
15 | 27 | ) { |
16 | 28 | companion object { |
17 | | - private const val STUFF_FOLDER = "/opt/marisa-stuff" |
| 29 | + private const val STUFF_FOLDER = "C:\\Users\\WinG4merBR\\Downloads" |
18 | 30 | } |
19 | 31 |
|
20 | 32 | private val server = embeddedServer(Netty, port = config.port) { |
21 | 33 | install(ContentNegotiation) { |
22 | 34 | json() |
23 | 35 | } |
24 | 36 |
|
| 37 | + install(Compression) { |
| 38 | + gzip { priority = 1.0 } |
| 39 | + deflate { priority = 10.0; minimumSize(1024) } |
| 40 | + } |
| 41 | + |
25 | 42 | routing { |
26 | | - staticFiles("/", File(STUFF_FOLDER)) |
| 43 | + get("/{path...}") { |
| 44 | + val path = call.parameters.getAll("path")?.joinToString("/") ?: return@get |
| 45 | + val file = File(STUFF_FOLDER, path) |
| 46 | + val size = call.request.queryParameters["size"]?.toIntOrNull() |
| 47 | + |
| 48 | + |
| 49 | + if (!file.exists() || file.isDirectory) { |
| 50 | + call.respond(HttpStatusCode.NotFound) |
| 51 | + return@get |
| 52 | + } |
| 53 | + |
| 54 | + val extension = file.extension.lowercase() |
| 55 | + val isImage = extension in listOf("jpg", "jpeg", "png", "webp") |
| 56 | + |
| 57 | + if (isImage && size != null) { |
| 58 | + if (size > 2048) return@get call.respond(HttpStatusCode.BadRequest) |
| 59 | + |
| 60 | + val outputStream = ByteArrayOutputStream() |
| 61 | + |
| 62 | + try { |
| 63 | + Thumbnails.of(file) |
| 64 | + .size(size, size) |
| 65 | + .outputFormat(if (extension == "webp") "png" else extension) |
| 66 | + .toOutputStream(outputStream) |
| 67 | + |
| 68 | + val contentType = ContentType.fromFilePath(file.path).first() |
| 69 | + call.respondBytes(outputStream.toByteArray(), contentType) |
| 70 | + } catch (e: Exception) { |
| 71 | + call.respond(HttpStatusCode.InternalServerError, "Erro ao processar imagem") |
| 72 | + } |
| 73 | + } else { |
| 74 | + call.respondFile(file) |
| 75 | + } |
| 76 | + } |
27 | 77 | } |
28 | 78 | } |
29 | 79 |
|
|
0 commit comments