Skip to content

Commit

Permalink
code impr
Browse files Browse the repository at this point in the history
  • Loading branch information
storytellerF committed Feb 1, 2025
1 parent 994ed30 commit d4b86a1
Show file tree
Hide file tree
Showing 25 changed files with 399 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import co.elastic.clients.elasticsearch._types.query_dsl.MatchQuery
import co.elastic.clients.elasticsearch._types.query_dsl.Query
import co.elastic.clients.elasticsearch._types.query_dsl.RangeQuery
import co.elastic.clients.elasticsearch._types.query_dsl.TermQuery
import co.elastic.clients.elasticsearch.core.BulkRequest
import co.elastic.clients.elasticsearch.core.SearchRequest
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation
import co.elastic.clients.elasticsearch.core.bulk.IndexOperation
Expand Down Expand Up @@ -85,7 +84,6 @@ class ElasticTopicSearchService(private val connection: ElasticConnection) : Top
it.index(TOPIC_INDEX_NAME).id(id.toString())
}, TopicDocument::class.java).await().source()
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import java.io.File
import java.net.URLConnection
import java.nio.file.Files
import java.nio.file.StandardCopyOption
import javax.imageio.ImageIO
import javax.imageio.stream.FileImageInputStream

class FileSystemMediaService(private val url: String, base: String) : MediaService {
private val root = File(base).canonicalFile
Expand Down Expand Up @@ -91,7 +89,7 @@ class FileSystemMediaService(private val url: String, base: String) : MediaServi
val contentType = kotlin.runCatching {
tika.detect(file)
}.getOrNull() ?: URLConnection.guessContentTypeFromName(file.path)
?: org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM.mimeType
?: org.apache.http.entity.ContentType.APPLICATION_OCTET_STREAM.mimeType
return MediaItem(
it,
contentType,
Expand Down
50 changes: 26 additions & 24 deletions backend/src/main/kotlin/com/storyteller_f/media/MediaService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import com.ashampoo.kim.jvm.KimJvm
import com.storyteller_f.Backend
import com.storyteller_f.shared.model.AMEDIA_BUCKET
import com.storyteller_f.shared.model.MediaInfo
import com.storyteller_f.shared.type.PrimaryKey
import org.apache.tika.Tika
import java.io.File

data class UploadPack(val name: String, val path: File, val contentType: String? = null, val meta: Map<String, String> = emptyMap())
data class UploadPack(
val name: String,
val path: File,
val contentType: String? = null,
val meta: Map<String, String> = emptyMap()
)

interface MediaService {
fun upload(bucketName: String, list: List<UploadPack>): Result<List<MediaInfo?>>
Expand All @@ -21,32 +25,30 @@ interface MediaService {
fun list(bucketName: String, prefix: String): Result<List<MediaInfo>>
}


fun uploadOneFil(
file: File,
fun uploadFiles(
tika: Tika,
backend: Backend,
fileName: String,
lng: PrimaryKey,
contentType: String?
): Result<MediaInfo?> {
val type = checkContentType(file, tika, contentType)
val meta = mutableMapOf<String, String>()
if (type.second?.startsWith("image") == true) {
KimJvm.readMetadata(file)?.convertToPhotoMetadata()?.let {
val width = it.widthPx
val height = it.heightPx
if (width != null && height != null) {
meta.putAll(arrayOf("width" to width.toString(), "height" to height.toString()))
}
files: List<Triple<File, String, String?>>
): Result<List<MediaInfo?>> {
val packs = files.map { (file, saveFileName, contentType) ->
val type = checkContentType(file, tika, contentType)
val meta = if (type.second?.startsWith("image") == true) {
KimJvm.readMetadata(file)?.convertToPhotoMetadata()?.let {
val width = it.widthPx
val height = it.heightPx
if (width != null && height != null) {
mapOf("width" to width.toString(), "height" to height.toString())
} else {
null
}
} ?: emptyMap()
} else {
emptyMap()
}
UploadPack(saveFileName, file, contentType, meta)
}
return backend.mediaService.upload(
AMEDIA_BUCKET,
listOf(UploadPack("$lng/$fileName", file, type.first, meta))
).map {
it.firstOrNull()
}

return backend.mediaService.upload(AMEDIA_BUCKET, packs)
}

private fun checkContentType(
Expand Down
3 changes: 1 addition & 2 deletions backend/src/main/kotlin/com/storyteller_f/tables/Users.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.storyteller_f.tables

import com.storyteller_f.*
import com.storyteller_f.shared.model.AMEDIA_BUCKET
import com.storyteller_f.shared.model.MediaInfo
import com.storyteller_f.shared.model.UserInfo
import com.storyteller_f.shared.type.ObjectType
import com.storyteller_f.shared.type.PrimaryKey
Expand Down Expand Up @@ -296,4 +295,4 @@ suspend fun DatabaseFactory.getUsersByIds(ids: List<PrimaryKey>, backend: Backen
}
}.mapResult {
fillUserMedia(it, backend)
}
}
99 changes: 79 additions & 20 deletions cli/src/main/kotlin/com/storyteller_f/cli/AddPreset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import com.storyteller_f.DatabaseFactory
import com.storyteller_f.ROOM_ID_LENGTH
import com.storyteller_f.crypto_jvm.addProviderForJvm
import com.storyteller_f.index.TopicDocument
import com.storyteller_f.media.uploadOneFil
import com.storyteller_f.media.uploadFiles
import com.storyteller_f.shared.*
import com.storyteller_f.shared.obj.PresetCommunity
import com.storyteller_f.shared.obj.PresetTopic
import com.storyteller_f.shared.obj.PresetValue
import com.storyteller_f.shared.type.*
import com.storyteller_f.shared.utils.extractMarkdownMediaLink
import com.storyteller_f.shared.utils.now
import com.storyteller_f.tables.*
import io.github.aakira.napier.Napier
Expand Down Expand Up @@ -87,7 +88,11 @@ class AddPreset : Subcommand("add", "add entry") {
} else {
val path = File(parentDir, icon)
val p = "$id/room-icon.${path.extension}"
uploadOneFil(path, tika, backend, "room-icon.${path.extension}", id, null).getOrThrow()
uploadFiles(
tika,
backend,
listOf(Triple(path, "$id/${"room-icon.${path.extension}"}", null))
).getOrThrow()
Triple(it, p, id)
}
}
Expand Down Expand Up @@ -143,15 +148,20 @@ class AddPreset : Subcommand("add", "add entry") {
it.community != null
}.forEach { (t, u) ->
if (t) {
addTopicsIntoCommunity(u, userList, parentDir)
addTopicsIntoCommunity(u, userList, parentDir, tika)
} else {
addTopicsIntoRoom(u, userList, parentDir)
addTopicsIntoRoom(u, userList, parentDir, tika)
}
}
}.getOrThrow()
}

private suspend fun addTopicsIntoRoom(u: List<PresetTopic>, userList: Map<String, User>, parentDir: File) {
private suspend fun addTopicsIntoRoom(
u: List<PresetTopic>,
userList: Map<String, User>,
parentDir: File,
tika: Tika
) {
val roomList = u.mapNotNull {
it.room
}.distinct().map {
Expand All @@ -163,8 +173,8 @@ class AddPreset : Subcommand("add", "add entry") {
checkRoomIsPrivate(value.id).getOrNull() == true
}

insertEncryptedTopic(roomIsPrivate, parentDir, ids, u)
insertUnEncryptedTopic(u, roomIsPrivate, parentDir, ids, roomList, userList)
insertEncryptedTopicToRoom(roomIsPrivate, parentDir, ids, u, tika, roomList)
insertUnEncryptedTopicToRoom(u, roomIsPrivate, parentDir, ids, roomList, userList, tika)
}

private suspend fun insertRoomTopicBaseLevel(
Expand Down Expand Up @@ -210,13 +220,14 @@ class AddPreset : Subcommand("add", "add entry") {
return ids
}

private suspend fun insertUnEncryptedTopic(
private suspend fun insertUnEncryptedTopicToRoom(
presetTopicList: List<PresetTopic>,
roomIsPrivate: Map<String, Boolean>,
parentDir: File,
ids: LongArray,
roomList: Map<String, Room>,
userList1: Map<String, User>
userList: Map<String, User>,
tika: Tika
) {
val topicsPublic = presetTopicList.mapIndexedNotNull { i, addTopic ->
if (roomIsPrivate[addTopic.room] != true) {
Expand All @@ -239,17 +250,29 @@ class AddPreset : Subcommand("add", "add entry") {
else -> ids[second - first.parent!!]
},
(if (level == 0) ObjectType.ROOM else ObjectType.TOPIC).name,
userList1[first.author]!!.id
userList[first.author]!!.id
)
}
).getOrThrow()
presetTopicList.forEachIndexed { index, topic ->
if (topic.room != null) {
val content = getTopicContent(topic, parentDir)
uploadFiles(tika, backend, extractMarkdownMediaLink(content).map {
Triple(File(parentDir, "images/topics/$it"), "${userList[topic.author]!!.id}/$it", null)
}).getOrThrow()
} else {
null
}
}
}

private suspend fun insertEncryptedTopic(
private suspend fun insertEncryptedTopicToRoom(
roomIsPrivate: Map<String, Boolean>,
parentDir: File,
ids: LongArray,
u: List<PresetTopic>
u: List<PresetTopic>,
tika: Tika,
roomList: Map<String, Room>
) {
val topicsPrivate = u.mapIndexedNotNull { i, addTopic ->
if (roomIsPrivate[addTopic.room] == true) {
Expand All @@ -258,7 +281,7 @@ class AddPreset : Subcommand("add", "add entry") {
null
}
}
val rooms = u.mapNotNull {
val roomMembers = u.mapNotNull {
it.room
}.distinct().map { roomAid ->
val members = MemberJoins
Expand All @@ -272,13 +295,13 @@ class AddPreset : Subcommand("add", "add entry") {
it[Users.publicKey] to it[Users.id]
}
roomAid to members
}.associateBy { it.first }
}.associate { it }
val encrypted = topicsPrivate.map { (addTopic, index) ->
val (first, aesBytes) = encrypt(getTopicContent(addTopic, parentDir))
Tuple4(index, first, aesBytes, addTopic)
}
val encryptedKeys = encrypted.flatMap { (index, _, aesBytes, topic) ->
rooms[topic.room]!!.second.map {
roomMembers[topic.room]!!.map {
val pubKey = it.first
val data4 = encryptAesKey(pubKey, aesBytes)
Triple(index, data4, it.second)
Expand All @@ -293,9 +316,27 @@ class AddPreset : Subcommand("add", "add entry") {
this[EncryptedTopicKeys.encryptedAes] = ExposedBlob(t4)
this[EncryptedTopicKeys.uid] = id
}
u.forEachIndexed { index, topic ->
if (topic.room != null) {
val room = roomList[topic.room]
if (room != null) {
val content = getTopicContent(topic, parentDir)
uploadFiles(tika, backend, extractMarkdownMediaLink(content).map {
Triple(File(parentDir, "images/topics/$it"), "${room.id}/$it", null)
}).getOrThrow()
}
} else {
null
}
}
}

private suspend fun addTopicsIntoCommunity(u: List<PresetTopic>, userList: Map<String, User>, parentDir: File) {
private suspend fun addTopicsIntoCommunity(
u: List<PresetTopic>,
userList: Map<String, User>,
parentDir: File,
tika: Tika
) {
val communityList = u.mapNotNull {
it.community
}.distinct().map {
Expand All @@ -310,7 +351,17 @@ class AddPreset : Subcommand("add", "add entry") {
}.associate {
it.first to it.second
}
val ids = insertCommunityTopicTopLevel(u, userList, communityList)
val ids = insertCommunityTopics(u, userList, communityList)
u.forEachIndexed { index, topic ->
if (topic.community != null) {
val content = getTopicContent(topic, parentDir)
uploadFiles(tika, backend, extractMarkdownMediaLink(content).map {
Triple(File(parentDir, "images/topics/$it"), "${userList[topic.author]!!.id}/$it", null)
}).getOrThrow()
} else {
null
}
}
backend.topicSearchService.saveDocument(
u.mapIndexedNotNull { index, topic ->
if (topic.community != null) {
Expand Down Expand Up @@ -339,7 +390,7 @@ class AddPreset : Subcommand("add", "add entry") {
).getOrThrow()
}

private suspend fun insertCommunityTopicTopLevel(
private suspend fun insertCommunityTopics(
u: List<PresetTopic>,
userList: Map<String, User>,
communityList: Map<String, PrimaryKey>
Expand Down Expand Up @@ -409,7 +460,11 @@ class AddPreset : Subcommand("add", "add entry") {
} else {
val path = File(parentDir, icon)
val p = "$id/avatar.${path.extension}"
uploadOneFil(path, tika, backend, "avatar.${path.extension}", id, null).getOrThrow()
uploadFiles(
tika,
backend,
listOf(Triple(path, "$id/${"avatar.${path.extension}"}", null))
).getOrThrow()
Tuple5(it, p, derPublicKey, ad, id)
}
}
Expand Down Expand Up @@ -443,7 +498,11 @@ class AddPreset : Subcommand("add", "add entry") {
} else {
val path = File(parentDir, icon)
val p = "$id/community-icon.${path.extension}"
uploadOneFil(path, tika, backend, "community-icon.${path.extension}", id, null).getOrThrow()
uploadFiles(
tika,
backend,
listOf(Triple(path, "$id/${"community-icon.${path.extension}"}", null))
).getOrThrow()
Triple(it, p, id)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.storyteller_f.a.app.compontents

import android.graphics.Bitmap
import androidx.compose.ui.geometry.Size
import androidx.core.graphics.drawable.toBitmap
import ru.noties.jlatexmath.JLatexMathDrawable
import java.io.OutputStream

actual fun buildTexPainter(tex: String, backgroundColor: Int, color: Int, textSize: Float, outputStream: OutputStream): Boolean {
actual fun buildTexPainter(
tex: String,
backgroundColor: Int,
color: Int,
textSize: Float,
outputStream: OutputStream
): Boolean {
val drawable = JLatexMathDrawable.builder(tex)
.textSize(textSize)
.padding(8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import androidx.media3.common.*
import androidx.media3.exoplayer.ExoPlaybackException
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import com.dokar.sonner.Toaster
import com.dokar.sonner.ToasterState
import com.dokar.sonner.rememberToasterState
import com.storyteller_f.a.app.LocalToaster
import io.github.aakira.napier.Napier
import io.github.aakira.napier.log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ fun LoginCheck(content: @Composable () -> Unit) {
Text("Retry")
}
}

} else {
CircularProgressIndicator(modifier = Modifier.size(40.dp))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private fun startSign(
}
}

suspend fun signUpOrSignIn(
suspend fun signUpOrSignIn(
privateKey: String,
client: HttpClient,
isSignUp: Boolean,
Expand Down
Loading

0 comments on commit d4b86a1

Please sign in to comment.