45 lines
1.3 KiB
Kotlin
45 lines
1.3 KiB
Kotlin
package com.android.trisolarisserver.component
|
|
|
|
import org.springframework.beans.factory.annotation.Value
|
|
import org.springframework.stereotype.Component
|
|
import org.springframework.web.multipart.MultipartFile
|
|
import java.nio.file.Files
|
|
import java.nio.file.Path
|
|
import java.nio.file.Paths
|
|
import java.util.UUID
|
|
|
|
data class StoredSignature(
|
|
val storagePath: String,
|
|
val sizeBytes: Long
|
|
)
|
|
|
|
@Component
|
|
class GuestSignatureStorage(
|
|
@Value("\${storage.documents.root}") private val root: String
|
|
) {
|
|
init {
|
|
val rootPath = Paths.get(root)
|
|
Files.createDirectories(rootPath)
|
|
if (!Files.isWritable(rootPath)) {
|
|
throw IllegalStateException("Guest signature root not writable: $root")
|
|
}
|
|
}
|
|
|
|
fun store(propertyId: UUID, guestId: UUID, file: MultipartFile): StoredSignature {
|
|
val dir = Paths.get(root, "guests", propertyId.toString(), guestId.toString())
|
|
Files.createDirectories(dir)
|
|
val path = dir.resolve("signature.svg")
|
|
file.inputStream.use { input ->
|
|
Files.newOutputStream(path).use { output -> input.copyTo(output) }
|
|
}
|
|
return StoredSignature(
|
|
storagePath = path.toString(),
|
|
sizeBytes = Files.size(path)
|
|
)
|
|
}
|
|
|
|
fun resolvePath(storagePath: String): Path {
|
|
return Paths.get(storagePath)
|
|
}
|
|
}
|