Import feature
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package be.simplenotes.domain
|
||||
|
||||
import be.simplenotes.domain.security.HtmlSanitizer
|
||||
import be.simplenotes.persistence.repositories.NoteRepository
|
||||
import be.simplenotes.types.ExportedNote
|
||||
import be.simplenotes.types.LoggedInUser
|
||||
import jakarta.inject.Singleton
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
interface ImportService {
|
||||
fun importJson(user: LoggedInUser, content: String)
|
||||
}
|
||||
|
||||
@Singleton
|
||||
internal class ImportServiceImpl(
|
||||
private val noteRepository: NoteRepository,
|
||||
private val json: Json,
|
||||
private val htmlSanitizer: HtmlSanitizer,
|
||||
) : ImportService {
|
||||
override fun importJson(user: LoggedInUser, content: String) {
|
||||
val notes = json.decodeFromString(ListSerializer(ExportedNote.serializer()), content)
|
||||
noteRepository.import(
|
||||
user.userId,
|
||||
notes.map {
|
||||
it.copy(html = htmlSanitizer.sanitize(user, it.html))
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ internal class MarkdownServiceImpl(
|
||||
private fun splitMetaFromDocument(input: String) = either {
|
||||
val split = input.split(yamlBoundPattern, 3)
|
||||
ensure(split.size >= 3) { MarkdownParsingError.MissingMeta }
|
||||
(split[1].trim() to split[2].trim())
|
||||
split[1].trim() to split[2].trim()
|
||||
}
|
||||
|
||||
private val yaml = Yaml()
|
||||
|
||||
Reference in New Issue
Block a user