feat(media): albums — one link for a season or a photo album; fix path traversal in media get
An album is a sealed file (shade.album/v1) listing grants, so the relay cannot tell it from a film. `shade media put dir/` uploads every file and the album (subdirectories nest, natural order); `media get` brings an album back as a directory; /watch/ shows a grid with decrypted previews and Back returns to the album without putting grants in history. Kotlin reads albums (Albums.read) against a new shared vector. Fixed: `media get` used the sealer-chosen meta.name as the output path. Fixed a relay test race (flush() is sent, not received); the relay now reports framesIn. Verified: 64 TS tests x3, Kotlin 81/0, a 6-file album with a nested album uploaded, browsed, played and downloaded back bit-identical. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package no.zyon.shade.media
|
||||
|
||||
import org.json.JSONArray
|
||||
import org.json.JSONException
|
||||
import org.json.JSONObject
|
||||
import java.nio.ByteBuffer
|
||||
import java.nio.charset.CharacterCodingException
|
||||
import java.nio.charset.CodingErrorAction
|
||||
|
||||
/**
|
||||
* Albums — mirror of `@shade/media/src/album.ts`. An album is a sealed file
|
||||
* whose plaintext lists grants (films, pictures, nested albums, live
|
||||
* channels), so one link opens them all and the relay cannot tell it from
|
||||
* any other file.
|
||||
*/
|
||||
|
||||
const val ALBUM_KIND = "shade.album/v1"
|
||||
const val ALBUM_MIME = "application/vnd.shade.album+json"
|
||||
const val MAX_ALBUM_ITEMS = 10_000
|
||||
private const val MAX_TITLE = 512
|
||||
private const val MAX_ALBUM_BYTES = 16L * 1024 * 1024
|
||||
|
||||
class Album(val title: String, val items: List<Grant>)
|
||||
|
||||
object Albums {
|
||||
/** Parse an album's plaintext, strictly: every item must be a valid grant. */
|
||||
fun parse(bytes: ByteArray): Album {
|
||||
val text = try {
|
||||
Charsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT)
|
||||
.onUnmappableCharacter(CodingErrorAction.REPORT)
|
||||
.decode(ByteBuffer.wrap(bytes)).toString()
|
||||
} catch (e: CharacterCodingException) {
|
||||
throw MediaFormatException("album is not UTF-8")
|
||||
}
|
||||
val o = try { JSONObject(text) } catch (e: JSONException) { throw MediaFormatException("album is not a JSON object") }
|
||||
if (o.opt("kind") != ALBUM_KIND) throw MediaFormatException("not an album (kind ${o.opt("kind")})")
|
||||
val title = o.opt("title") as? String ?: throw MediaFormatException("album.title must be a string")
|
||||
if (title.length > MAX_TITLE) throw MediaFormatException("album title longer than $MAX_TITLE characters")
|
||||
val items = o.opt("items") as? JSONArray ?: throw MediaFormatException("album.items must be an array")
|
||||
if (items.length() > MAX_ALBUM_ITEMS) throw MediaFormatException("album holds more than $MAX_ALBUM_ITEMS items")
|
||||
return Album(
|
||||
title,
|
||||
(0 until items.length()).map { i ->
|
||||
val item = items.opt(i) as? JSONObject ?: throw MediaFormatException("album item $i is not a grant")
|
||||
try {
|
||||
Grants.parse(item.toString())
|
||||
} catch (e: MediaFormatException) {
|
||||
throw MediaFormatException("album item $i: ${e.message}")
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/** The items of an album already opened as a reader. */
|
||||
fun read(reader: MediaReader): Album {
|
||||
if (reader.meta.mime != ALBUM_MIME) throw MediaFormatException("not an album (${reader.meta.mime ?: "no type"})")
|
||||
if (reader.size > MAX_ALBUM_BYTES) throw MediaFormatException("album larger than 16 MiB")
|
||||
return parse(reader.read(0, reader.size.toInt()))
|
||||
}
|
||||
|
||||
/** Does this grant's meta preview name an album? */
|
||||
fun isAlbum(grant: Grant): Boolean = when (grant) {
|
||||
is MediaGrant -> grant.meta?.mime == ALBUM_MIME
|
||||
is LiveGrant -> false
|
||||
}
|
||||
}
|
||||
@@ -1,13 +1,18 @@
|
||||
package no.zyon.shade
|
||||
|
||||
import no.zyon.shade.media.ALBUM_MIME
|
||||
import no.zyon.shade.media.Albums
|
||||
import no.zyon.shade.media.ByteArraySource
|
||||
import no.zyon.shade.media.FOOTER_BYTES
|
||||
import no.zyon.shade.media.Grants
|
||||
import no.zyon.shade.media.LIVE_GRANT_KIND
|
||||
import no.zyon.shade.media.LiveContent
|
||||
import no.zyon.shade.media.LiveGrant
|
||||
import no.zyon.shade.media.LiveKeyException
|
||||
import no.zyon.shade.media.LivePublisher
|
||||
import no.zyon.shade.media.LiveSubscriber
|
||||
import no.zyon.shade.media.MEDIA_GRANT_KIND
|
||||
import no.zyon.shade.media.MediaFormatException
|
||||
import no.zyon.shade.media.MediaGrant
|
||||
import no.zyon.shade.media.MediaIntegrityException
|
||||
import no.zyon.shade.media.MediaKeys
|
||||
@@ -23,6 +28,7 @@ import org.json.JSONArray
|
||||
import org.json.JSONObject
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertThrows
|
||||
@@ -176,4 +182,30 @@ class MediaVectorTest {
|
||||
assertArrayEquals(mediaId, g.mediaId)
|
||||
assertThrows(Exception::class.java) { Grants.parse("""{"kind":"shade.media-grant/v1","mediaId":"AA","key":"AA","root":"ab"}""") }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun albumOpensFromTheSealedVector() {
|
||||
val v = byPrefix("album:").single()
|
||||
val reader = MediaReader.open(ByteArraySource(hex(v.getString("sealed"))), MediaKeys(mediaId, mediaKey, hex(v.getString("rootDigest"))))
|
||||
assertEquals(ALBUM_MIME, reader.meta.mime)
|
||||
val album = Albums.read(reader)
|
||||
assertEquals(v.getString("title"), album.title)
|
||||
val kinds = v.getJSONArray("itemKinds")
|
||||
val names = v.getJSONArray("itemNames")
|
||||
assertEquals(kinds.length(), album.items.size)
|
||||
album.items.forEachIndexed { i, g ->
|
||||
assertEquals(kinds.getString(i), if (g is MediaGrant) MEDIA_GRANT_KIND else LIVE_GRANT_KIND)
|
||||
val meta = when (g) {
|
||||
is MediaGrant -> g.meta
|
||||
is LiveGrant -> g.meta
|
||||
}
|
||||
assertEquals(names.getString(i), meta?.name)
|
||||
}
|
||||
assertTrue(Albums.isAlbum(album.items[1]))
|
||||
assertFalse(Albums.isAlbum(album.items[0]))
|
||||
// Strict: a bad item, the wrong kind, invalid UTF-8.
|
||||
assertThrows(MediaFormatException::class.java) { Albums.parse("""{"kind":"shade.album/v1","title":"","items":[{"kind":"x"}]}""".toByteArray()) }
|
||||
assertThrows(MediaFormatException::class.java) { Albums.parse("""{"kind":"shade.album/v2","title":"","items":[]}""".toByteArray()) }
|
||||
assertThrows(MediaFormatException::class.java) { Albums.parse(byteArrayOf(0xff.toByte(), 0xfe.toByte())) }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user