feat(android): MediaUploader — a phone puts films on a relay, in resumable pieces
MediaOwner signs owner requests and MediaUploader speaks the piece protocol of media put (window, retries, resync after a lost answer). MediaSealer .sealStream seals a film to a file; MediaGrant.encode/capabilityUrl make the link to share. Owner signatures and encoded grants are pinned by new vectors; scripts/media-live-kotlin.ts runs the uploader against a real TS relay with a lost answer and a refused piece injected. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
package no.zyon.shade
|
||||
|
||||
import no.zyon.shade.media.FileRangeSource
|
||||
import no.zyon.shade.media.Grants
|
||||
import no.zyon.shade.media.MediaGrant
|
||||
import no.zyon.shade.media.MediaMeta
|
||||
import no.zyon.shade.media.MediaOwner
|
||||
import no.zyon.shade.media.MediaSealer
|
||||
import no.zyon.shade.media.MediaUploader
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import java.security.SecureRandom
|
||||
|
||||
/**
|
||||
* Live cross-check of [MediaUploader] against the real TS relay — skipped
|
||||
* unless `SHADE_MEDIA_LIVE_SPEC` points at the spec file a TS driver wrote.
|
||||
*
|
||||
* Run it via `bun run scripts/media-live-kotlin.ts`: the driver serves
|
||||
* `createMediaService` with two faults injected on the pieces, uploads a file
|
||||
* with the TS client and writes `{ url, seed, tsLink, tsSha256, scratch,
|
||||
* kotlinOut }`. This test opens the TS file, uploads a film in pieces and a
|
||||
* photo in one PUT, and records the links in `kotlinOut`; the driver then
|
||||
* reads the film back with the TS reader.
|
||||
*/
|
||||
class MediaLiveTest {
|
||||
|
||||
private fun sha256(b: ByteArray) = MessageDigest.getInstance("SHA-256").digest(b).joinToString("") { "%02x".format(it) }
|
||||
|
||||
private fun fromHex(s: String) =
|
||||
ByteArray(s.length / 2) { ((Character.digit(s[it * 2], 16) shl 4) + Character.digit(s[it * 2 + 1], 16)).toByte() }
|
||||
|
||||
@Test
|
||||
fun uploadsInPiecesThroughFaultsForTsToRead() {
|
||||
val specPath = System.getenv("SHADE_MEDIA_LIVE_SPEC")
|
||||
assumeTrue("SHADE_MEDIA_LIVE_SPEC not set — live cross-check skipped", !specPath.isNullOrEmpty())
|
||||
val spec = JSONObject(File(specPath!!).readText())
|
||||
val uploader = MediaUploader(spec.getString("url"), MediaOwner(fromHex(spec.getString("seed"))))
|
||||
|
||||
// Read what TS uploaded, from its link.
|
||||
val tsGrant = Grants.fromCapabilityUrl(spec.getString("tsLink")) as MediaGrant
|
||||
tsGrant.open().use { r -> assertEquals(spec.getString("tsSha256"), sha256(r.read(0, r.size.toInt()))) }
|
||||
|
||||
// A film from the phone: sealed to a file, up in three pieces (16 MiB each),
|
||||
// through a lost answer on the second and a refusal of the third.
|
||||
val plain = ByteArray(40 * 1024 * 1024).also { SecureRandom().nextBytes(it) }
|
||||
val sealedFile = File(spec.getString("scratch"), "film.smf")
|
||||
val meta = MediaMeta(name = "Fra telefonen — «Kveld».mp4", mime = "video/mp4", durationMs = 61_500, width = 1080, height = 1920)
|
||||
val progress = mutableListOf<Long>()
|
||||
val token = MediaUploader.newReadToken()
|
||||
val result = sealedFile.outputStream().buffered().use { MediaSealer.sealStream(plain.inputStream(), it, meta) }
|
||||
val film = FileRangeSource(sealedFile).use { body ->
|
||||
uploader.putSealed(result, body, token, meta) { acked, total ->
|
||||
synchronized(progress) { progress += acked }
|
||||
assertEquals(result.sealedSize, total)
|
||||
}
|
||||
}
|
||||
assertEquals(result.sealedSize, progress.last())
|
||||
assertTrue("progress only grows: $progress", progress.zipWithNext().all { (a, b) -> a <= b })
|
||||
film.open().use { r -> assertEquals(sha256(plain), sha256(r.read(0, r.size.toInt()))) }
|
||||
|
||||
// The same film again (the app was killed after the upload): the relay
|
||||
// already has it, nothing is sent, the link is the same.
|
||||
val again = FileRangeSource(sealedFile).use { uploader.putSealed(result, it, token, meta) }
|
||||
assertEquals(film.capabilityUrl("https://watch.example/"), again.capabilityUrl("https://watch.example/"))
|
||||
|
||||
// A photo: one PUT; then deleted.
|
||||
val photo = uploader.uploadBytes(ByteArray(300_000).also { SecureRandom().nextBytes(it) }, MediaMeta(name = "IMG_0001.jpg", mime = "image/jpeg"))
|
||||
photo.open().use { assertEquals("IMG_0001.jpg", it.meta.name) }
|
||||
assertTrue(uploader.delete(photo.mediaId))
|
||||
assertFalse(uploader.delete(photo.mediaId))
|
||||
|
||||
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))
|
||||
.toString(),
|
||||
)
|
||||
sealedFile.delete()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user