feat(android): albums from the phone — several files shared at once are one link
Albums.encode and Grant.encode (live grants too) write what TS writes; the album vector is resealed byte for byte from Kotlin. MediaUploader.uploadAlbum seals and uploads one. The demo turns a multi-file share into an album and its player lists an album's items and opens them in place. The live cross-check uploads an album from Kotlin that TS opens. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package no.zyon.shade.media
|
||||
|
||||
import no.zyon.shade.serialization.stringifyJson
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
@@ -23,6 +24,20 @@ private const val MAX_ALBUM_BYTES = 16L * 1024 * 1024
|
||||
class Album(val title: String, val items: List<Grant>)
|
||||
|
||||
object Albums {
|
||||
/** An album's plaintext, byte for byte what TS `encodeAlbum` writes. */
|
||||
fun encode(album: Album): ByteArray {
|
||||
if (album.title.length > MAX_TITLE) throw MediaFormatException("album title longer than $MAX_TITLE characters")
|
||||
if (album.items.size > MAX_ALBUM_ITEMS) throw MediaFormatException("album holds more than $MAX_ALBUM_ITEMS items")
|
||||
val o = LinkedHashMap<String, Any>()
|
||||
o["kind"] = ALBUM_KIND
|
||||
o["title"] = album.title
|
||||
o["items"] = album.items.map { it.toJsonMap() }
|
||||
return stringifyJson(o).toByteArray(Charsets.UTF_8)
|
||||
}
|
||||
|
||||
/** The meta an album is sealed with: its type, and the title as its name. */
|
||||
fun meta(title: String) = MediaMeta(mime = ALBUM_MIME, name = title)
|
||||
|
||||
/** Parse an album's plaintext, strictly: every item must be a valid grant. */
|
||||
fun parse(bytes: ByteArray): Album {
|
||||
val text = try {
|
||||
|
||||
@@ -16,7 +16,17 @@ import java.util.Base64
|
||||
const val MEDIA_GRANT_KIND = "shade.media-grant/v1"
|
||||
const val LIVE_GRANT_KIND = "shade.live-grant/v1"
|
||||
|
||||
sealed class Grant
|
||||
sealed class Grant {
|
||||
/** The wire fields in TS `encodeGrant`'s order. */
|
||||
internal abstract fun toJsonMap(): Map<String, Any>
|
||||
|
||||
/** Wire JSON, byte for byte what TS `encodeGrant` writes (field order fixed, meta canonical). */
|
||||
fun encode(): String = stringifyJson(toJsonMap())
|
||||
|
||||
/** `pageUrl#shade=<base64url(json)>`: the link that opens it, the key in the fragment (never sent to a server). */
|
||||
fun capabilityUrl(pageUrl: String): String =
|
||||
"${pageUrl.substringBefore('#')}#shade=${b64u(encode().toByteArray(Charsets.UTF_8))}"
|
||||
}
|
||||
|
||||
class MediaGrant(
|
||||
val mediaId: ByteArray,
|
||||
@@ -36,8 +46,7 @@ class MediaGrant(
|
||||
|
||||
fun open(): MediaReader = MediaReader.open(source(), keys())
|
||||
|
||||
/** Wire JSON, byte for byte what TS `encodeGrant` writes (field order fixed, meta canonical). */
|
||||
fun encode(): String {
|
||||
override fun toJsonMap(): Map<String, Any> {
|
||||
val o = LinkedHashMap<String, Any>()
|
||||
o["kind"] = MEDIA_GRANT_KIND
|
||||
o["mediaId"] = b64u(mediaId)
|
||||
@@ -46,13 +55,9 @@ class MediaGrant(
|
||||
url?.let { o["url"] = it }
|
||||
readToken?.let { o["token"] = b64u(it) }
|
||||
meta?.let { o["meta"] = MediaMeta.validate(it).toJsonMap() }
|
||||
return stringifyJson(o)
|
||||
return o
|
||||
}
|
||||
|
||||
/** `pageUrl#shade=<base64url(json)>`: the link that opens this file, the key in the fragment (never sent to a server). */
|
||||
fun capabilityUrl(pageUrl: String): String =
|
||||
"${pageUrl.substringBefore('#')}#shade=${b64u(encode().toByteArray(Charsets.UTF_8))}"
|
||||
|
||||
companion object {
|
||||
/** The grant for a file just sealed (and uploaded to `url`). */
|
||||
fun of(r: SealResult, url: String? = null, readToken: ByteArray? = null, meta: MediaMeta? = null) =
|
||||
@@ -72,7 +77,23 @@ class LiveGrant(
|
||||
val meta: MediaMeta? = null,
|
||||
/** Link mode: reads this epoch and every later one, unaided. */
|
||||
val channelSecret: ByteArray? = null,
|
||||
) : Grant()
|
||||
) : Grant() {
|
||||
override fun toJsonMap(): Map<String, Any> {
|
||||
val o = LinkedHashMap<String, Any>()
|
||||
o["kind"] = LIVE_GRANT_KIND
|
||||
o["channelId"] = b64u(channelId)
|
||||
o["signingKey"] = b64u(signingKey)
|
||||
o["epoch"] = epoch
|
||||
o["seq"] = seq
|
||||
o["chainKey"] = b64u(chainKey)
|
||||
o["stickyKey"] = b64u(stickyKey)
|
||||
channelSecret?.let { o["channelSecret"] = b64u(it) }
|
||||
url?.let { o["url"] = it }
|
||||
readToken?.let { o["token"] = b64u(it) }
|
||||
meta?.let { o["meta"] = MediaMeta.validate(it).toJsonMap() }
|
||||
return o
|
||||
}
|
||||
}
|
||||
|
||||
internal fun b64u(b: ByteArray): String = Base64.getUrlEncoder().withoutPadding().encodeToString(b)
|
||||
|
||||
|
||||
@@ -92,6 +92,10 @@ class MediaUploader(
|
||||
return putSealed(result, ByteArraySource(sealed), readToken, meta)
|
||||
}
|
||||
|
||||
/** Seal and upload an album; the grant opens every item in it (a season, the photos of a trip). */
|
||||
fun uploadAlbum(album: Album, readToken: ByteArray? = newReadToken()): MediaGrant =
|
||||
uploadBytes(Albums.encode(album), Albums.meta(album.title), readToken)
|
||||
|
||||
/**
|
||||
* Seal `input` into `sealedFile` and upload it — a film without holding it
|
||||
* in memory. The sealed copy is the caller's: keep it (with the returned
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package no.zyon.shade
|
||||
|
||||
import no.zyon.shade.media.Album
|
||||
import no.zyon.shade.media.Albums
|
||||
import no.zyon.shade.media.FileRangeSource
|
||||
import no.zyon.shade.media.Grants
|
||||
import no.zyon.shade.media.MediaGrant
|
||||
@@ -109,11 +111,20 @@ class MediaLiveTest {
|
||||
assertTrue(uploader.delete(photo.mediaId))
|
||||
assertFalse(uploader.delete(photo.mediaId))
|
||||
|
||||
// The photos of a trip, shared at once: one album link for all of them.
|
||||
val album = uploader.uploadAlbum(Album("Tur — «Lofoten»", listOf(film, fresh)))
|
||||
album.open().use { r ->
|
||||
val back = Albums.read(r)
|
||||
assertEquals("Tur — «Lofoten»", back.title)
|
||||
assertEquals(listOf(film.encode(), fresh.encode()), back.items.map { it.encode() })
|
||||
}
|
||||
|
||||
File(spec.getString("kotlinOut")).writeText(
|
||||
JSONObject()
|
||||
.put("film", JSONObject().put("link", film.capabilityUrl("https://watch.example/")).put("sha256", sha256(plain)).put("size", plain.size))
|
||||
.put("again", again.capabilityUrl("https://watch.example/"))
|
||||
.put("photo", JSONObject().put("url", photo.url))
|
||||
.put("album", album.capabilityUrl("https://watch.example/"))
|
||||
.toString(),
|
||||
)
|
||||
sealedFile.delete()
|
||||
|
||||
@@ -137,6 +137,7 @@ class MediaVectorTest {
|
||||
fun liveFramesOpenFromTheLinkGrantAcrossARotation() {
|
||||
val g = Grants.parse(byPrefix("live grant").single().getString("json")) as LiveGrant
|
||||
assertNotNull(g.channelSecret)
|
||||
assertEquals(byPrefix("live grant").single().getString("json"), g.encode())
|
||||
val sub = LiveSubscriber(g)
|
||||
for (v in byPrefix("live frame")) {
|
||||
val f = sub.open(hex(v.getString("wire")))
|
||||
@@ -193,6 +194,11 @@ class MediaVectorTest {
|
||||
assertEquals(ALBUM_MIME, reader.meta.mime)
|
||||
val album = Albums.read(reader)
|
||||
assertEquals(v.getString("title"), album.title)
|
||||
// And written back: the same plaintext, sealed to the same bytes.
|
||||
val plain = Albums.encode(album)
|
||||
assertEquals(v.getString("plaintext"), String(plain, Charsets.UTF_8))
|
||||
val (resealed, _) = MediaSealer.withPinnedKeys(mediaId, mediaKey, Albums.meta(album.title), 4096).sealAll(plain)
|
||||
assertEquals(v.getString("sealed"), toHex(resealed))
|
||||
val kinds = v.getJSONArray("itemKinds")
|
||||
val names = v.getJSONArray("itemNames")
|
||||
assertEquals(kinds.length(), album.items.size)
|
||||
|
||||
Reference in New Issue
Block a user