feat: 完善后台布局编辑器,新增多页面与图片上传
- 服务端:布局 JSON 持久化与删除接口,页面接口支持 ETag 条件请求与任意布局路由 - 服务端:新增 POST /api/remote-ui/assets 图片上传,文件名带内容哈希,便于客户端缓存 - 编辑器:多页面切换/新建/删除、画布比例自适应、撤销重做、复制粘贴、层级与对齐线 - 编辑器:图片属性面板支持上传图片并自动回填 URL 与像素尺寸 - 客户端:文档磁盘缓存与 If-None-Match 条件请求,304 时复用缓存 - 部署:镜像升级到 1.0.0-editor5,挂载布局与图片目录
This commit is contained in:
1 parent
07b038b4e7
commit
a77293dadd
7 files changed
+868
-128
No files matched your search
+73
-20
@@ -63,10 +63,11 @@ private val client = OkHttpClient.Builder()
|
||||
.readTimeout(10, TimeUnit.SECONDS)
|
||||
.build()
|
||||
|
||||
/** 一次页面请求的结果:文档字节 + 文档引用的图片地址。 */
|
||||
/** 一次页面请求的结果:文档字节 + 文档引用的图片地址 + 服务端 ETag。 */
|
||||
private class RemoteUiPayload(
|
||||
val bytes: ByteArray,
|
||||
val assets: List<String>,
|
||||
val etag: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
@@ -91,6 +92,39 @@ private object RemoteDocumentStore {
|
||||
fun put(page: String, payload: RemoteUiPayload) {
|
||||
documents[page] = payload
|
||||
}
|
||||
|
||||
/** 文档与 ETag 落盘,进程重启后仍可发条件请求拿到 304。 */
|
||||
fun readDisk(context: Context, page: String): RemoteUiPayload? = runCatching {
|
||||
val name = sha256(page)
|
||||
val bytes = File(diskDir(context), "$name.bin").takeIf { it.isFile }?.readBytes()
|
||||
?: return@runCatching null
|
||||
val meta = File(diskDir(context), "$name.meta").takeIf { it.isFile }?.readLines().orEmpty()
|
||||
RemoteUiPayload(
|
||||
bytes = bytes,
|
||||
assets = meta.drop(1).filter { it.isNotBlank() },
|
||||
etag = meta.firstOrNull()?.takeIf { it.isNotBlank() },
|
||||
)
|
||||
}.getOrNull()
|
||||
|
||||
fun writeDisk(context: Context, page: String, payload: RemoteUiPayload) {
|
||||
runCatching {
|
||||
val dir = diskDir(context)
|
||||
val name = sha256(page)
|
||||
val temp = File(dir, "$name.bin.tmp")
|
||||
temp.writeBytes(payload.bytes)
|
||||
if (!temp.renameTo(File(dir, "$name.bin"))) temp.delete()
|
||||
File(dir, "$name.meta").writeText(
|
||||
(listOf(payload.etag ?: "") + payload.assets).joinToString("\n")
|
||||
)
|
||||
}.onFailure { Log.w("RemoteDocumentStore", "写入文档缓存失败 $page: ${it.message}") }
|
||||
}
|
||||
|
||||
private fun diskDir(context: Context): File =
|
||||
File(context.cacheDir, "remote-docs").apply { mkdirs() }
|
||||
|
||||
private fun sha256(value: String): String =
|
||||
MessageDigest.getInstance("SHA-256").digest(value.toByteArray())
|
||||
.joinToString("") { "%02x".format(it.toInt() and 0xFF) }
|
||||
}
|
||||
|
||||
private sealed interface RemoteUiState {
|
||||
@@ -208,25 +242,35 @@ private class RemoteImageLoader(context: Context) {
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun fetchRemoteUi(page: String, baseUrl: String): RemoteUiPayload =
|
||||
withContext(Dispatchers.IO) {
|
||||
val request = Request.Builder().url("$baseUrl/api/remote-ui/$page").build()
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) {
|
||||
throw IOException("HTTP ${response.code}")
|
||||
}
|
||||
val bytes = response.body?.bytes() ?: throw IOException("空响应")
|
||||
val assets = response.header("X-Remote-Assets")
|
||||
?.split(",")
|
||||
?.map { it.trim() }
|
||||
?.filter { it.isNotEmpty() }
|
||||
.orEmpty()
|
||||
RemoteUiPayload(bytes, assets)
|
||||
/**
|
||||
* 请求页面文档;本地已有 ETag 时发条件请求,304 返回 null 表示沿用缓存。
|
||||
*/
|
||||
private suspend fun fetchRemoteUi(
|
||||
page: String,
|
||||
baseUrl: String,
|
||||
cached: RemoteUiPayload?,
|
||||
): RemoteUiPayload? = withContext(Dispatchers.IO) {
|
||||
val request = Request.Builder()
|
||||
.url("$baseUrl/api/remote-ui/$page")
|
||||
.apply { cached?.etag?.let { header("If-None-Match", it) } }
|
||||
.build()
|
||||
client.newCall(request).execute().use { response ->
|
||||
if (response.code == 304) return@withContext null
|
||||
if (!response.isSuccessful) {
|
||||
throw IOException("HTTP ${response.code}")
|
||||
}
|
||||
val bytes = response.body?.bytes() ?: throw IOException("空响应")
|
||||
val assets = response.header("X-Remote-Assets")
|
||||
?.split(",")
|
||||
?.map { it.trim() }
|
||||
?.filter { it.isNotEmpty() }
|
||||
.orEmpty()
|
||||
RemoteUiPayload(bytes, assets, response.header("ETag"))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取出页面文档字节:命中缓存就不重复下载。
|
||||
* 取出页面文档字节:内存 → 磁盘缓存 → 条件请求,服务端 304 时直接复用缓存。
|
||||
*
|
||||
* 解析([RemoteDocument] 构造)放在 [Dispatchers.Default],避免大文档阻塞主线程;
|
||||
* 图片必须在返回前完成预取,否则播放器渲染时会取不到图片。
|
||||
@@ -235,10 +279,19 @@ private suspend fun loadDocument(
|
||||
page: String,
|
||||
baseUrl: String,
|
||||
imageLoader: RemoteImageLoader,
|
||||
context: Context,
|
||||
): RemoteDocument {
|
||||
val payload = RemoteDocumentStore.get(page) ?: fetchRemoteUi(page, baseUrl).also {
|
||||
RemoteDocumentStore.put(page, it)
|
||||
}
|
||||
val cached = RemoteDocumentStore.get(page)
|
||||
?: RemoteDocumentStore.readDisk(context, page)?.also { RemoteDocumentStore.put(page, it) }
|
||||
val payload = try {
|
||||
fetchRemoteUi(page, baseUrl, cached)?.also {
|
||||
RemoteDocumentStore.put(page, it)
|
||||
RemoteDocumentStore.writeDisk(context, page, it)
|
||||
} ?: cached
|
||||
} catch (e: IOException) {
|
||||
// 网络失败时退回本地缓存,保证离线也能看到上一次的页面。
|
||||
cached ?: throw e
|
||||
} ?: throw IOException("空响应")
|
||||
// 文档以 URL 形式声明图片,解析阶段就会校验该开关(默认关闭)。
|
||||
Limits.ENABLE_IMAGE_URLS = true
|
||||
val document = withContext(Dispatchers.Default) { RemoteDocument(payload.bytes) }
|
||||
@@ -405,7 +458,7 @@ fun RemoteUIScreen(
|
||||
state = RemoteUiState.Loading
|
||||
}
|
||||
state = try {
|
||||
RemoteUiState.Success(loadDocument(page, baseUrl, imageLoader))
|
||||
RemoteUiState.Success(loadDocument(page, baseUrl, imageLoader, appContext))
|
||||
} catch (e: Exception) {
|
||||
Log.e("RemoteUIScreen", "加载页面 $page 失败", e)
|
||||
RemoteUiState.Error(e.message ?: "加载失败")
|
||||
|
||||
Reference in new issue
Block a user