Add zip export

This commit is contained in:
Hubert Van De Walle 2020-09-28 19:13:54 +02:00
parent a7c8e63b11
commit 0dfb2a7e03
4 changed files with 70 additions and 16 deletions

View File

@ -7,10 +7,7 @@ import be.simplenotes.domain.security.JwtPayload
import be.simplenotes.domain.usecases.UserService import be.simplenotes.domain.usecases.UserService
import be.simplenotes.domain.usecases.users.delete.DeleteError import be.simplenotes.domain.usecases.users.delete.DeleteError
import be.simplenotes.domain.usecases.users.delete.DeleteForm import be.simplenotes.domain.usecases.users.delete.DeleteForm
import org.http4k.core.Method import org.http4k.core.*
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status
import org.http4k.core.body.form import org.http4k.core.body.form
import org.http4k.core.cookie.invalidateCookie import org.http4k.core.cookie.invalidateCookie
@ -49,18 +46,29 @@ class SettingsController(
) )
} }
fun export(request: Request, jwtPayload: JwtPayload): Response { private fun attachment(filename: String, contentType: String) = { response: Response ->
val isDownload = request.form("download") != null val name = filename.replace("[^a-zA-Z0-9-_.]".toRegex(), "_")
val json = userService.export(jwtPayload.userId) response
val res = Response(Status.OK).body(json).header("Content-Type", "application/json") .header("Content-Disposition", "attachment; filename=\"$name\"")
return if (isDownload) res.header( .header("Content-Type", contentType)
"Content-Disposition",
"attachment; filename=\"simplenotes-export-${sanitizeFilename(jwtPayload.username)}.json\""
)
else res
} }
private fun sanitizeFilename(inputName: String): String = inputName.replace("[^a-zA-Z0-9-_.]".toRegex(), "_") fun export(request: Request, jwtPayload: JwtPayload): Response {
val isDownload = request.form("download") != null
return if (isDownload) {
val filename = "simplenotes-export-${jwtPayload.username}"
if (request.form("format") == "zip") {
val zip = userService.exportAsZip(jwtPayload.userId)
Response(Status.OK)
.with(attachment("$filename.zip", "application/zip"))
.body(zip)
} else
Response(Status.OK)
.with(attachment("$filename.json", "application/json"))
.body(userService.exportAsJson(jwtPayload.userId))
} else Response(Status.OK).body(userService.exportAsJson(jwtPayload.userId)).header("Content-Type", "application/json")
}
private fun Request.deleteForm(jwtPayload: JwtPayload) = private fun Request.deleteForm(jwtPayload: JwtPayload) =
DeleteForm(jwtPayload.username, form("password"), form("checked") != null) DeleteForm(jwtPayload.username, form("password"), form("checked") != null)

View File

@ -61,6 +61,11 @@
<groupId>org.jetbrains.kotlinx</groupId> <groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-serialization-runtime</artifactId> <artifactId>kotlinx-serialization-runtime</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,5 +1,8 @@
package be.simplenotes.domain.usecases.export package be.simplenotes.domain.usecases.export
import java.io.InputStream
interface ExportUseCase { interface ExportUseCase {
fun export(userId: Int): String fun exportAsJson(userId: Int): String
fun exportAsZip(userId: Int): InputStream
} }

View File

@ -11,10 +11,15 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import kotlinx.serialization.modules.SerializersModule import kotlinx.serialization.modules.SerializersModule
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.InputStream
import java.time.LocalDateTime import java.time.LocalDateTime
internal class ExportUseCaseImpl(private val noteRepository: NoteRepository) : ExportUseCase { internal class ExportUseCaseImpl(private val noteRepository: NoteRepository) : ExportUseCase {
override fun export(userId: Int): String { override fun exportAsJson(userId: Int): String {
val module = SerializersModule { val module = SerializersModule {
contextual(LocalDateTime::class, LocalDateTimeSerializer) contextual(LocalDateTime::class, LocalDateTimeSerializer)
} }
@ -27,7 +32,40 @@ internal class ExportUseCaseImpl(private val noteRepository: NoteRepository) : E
val notes = noteRepository.export(userId) val notes = noteRepository.export(userId)
return json.encodeToString(ListSerializer(ExportedNote.serializer()), notes) return json.encodeToString(ListSerializer(ExportedNote.serializer()), notes)
} }
private fun sanitizeFilename(inputName: String): String = inputName.replace("[^a-zA-Z0-9-_.]".toRegex(), "_")
override fun exportAsZip(userId: Int): InputStream {
val notes = noteRepository.export(userId)
val zipOutput = ZipOutput()
zipOutput.use { zip ->
notes.forEach {
val name = sanitizeFilename(it.title)
zip.write("notes/$name.md", it.markdown)
} }
}
return ByteArrayInputStream(zipOutput.outputStream.toByteArray())
}
}
class ZipOutput : AutoCloseable {
val outputStream = ByteArrayOutputStream()
private val zipOutputStream = ZipArchiveOutputStream(outputStream)
fun write(path: String, content: String) {
val entry = ZipArchiveEntry(path)
zipOutputStream.putArchiveEntry(entry)
zipOutputStream.write(content.toByteArray())
zipOutputStream.closeArchiveEntry()
}
override fun close() {
zipOutputStream.finish()
zipOutputStream.close()
}
}
internal object LocalDateTimeSerializer : KSerializer<LocalDateTime> { internal object LocalDateTimeSerializer : KSerializer<LocalDateTime> {
override val descriptor: SerialDescriptor override val descriptor: SerialDescriptor