feat(android): UploadJobs — an upload from a phone survives the app being killed

MediaUploader.upload(jobs, key, …) seals once into an app-private job
directory and resumes with the same media id; a job already done gives its
grant again unless the relay says the file is gone. OS file locks keep one
upload per job. The live cross-check now kills a job after its first piece
and resumes it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-19 00:21:52 +02:00
parent f5125b8f66
commit b53dcd7813
5 changed files with 377 additions and 3 deletions

View File

@@ -7,9 +7,11 @@ 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 no.zyon.shade.media.UploadJobs
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotEquals
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Test
@@ -69,6 +71,38 @@ class MediaLiveTest {
val again = FileRangeSource(sealedFile).use { uploader.putSealed(result, it, token, meta) }
assertEquals(film.capabilityUrl("https://watch.example/"), again.capabilityUrl("https://watch.example/"))
// A shared video as an upload job: the app is killed after the first
// piece; the next start resumes past it, and sharing it a third time
// sends nothing and gives the same link.
val jobs = UploadJobs(File(spec.getString("scratch"), "jobs"))
val video = ByteArray(36 * 1024 * 1024).also { SecureRandom().nextBytes(it) }
val key = jobs.key(spec.getString("url"), "content://media/external/video/42", video.size.toString())
val videoMeta = MediaMeta(name = "VID_0042.mp4", mime = "video/mp4")
var opened = 0
val open = { opened++; video.inputStream() }
class Killed : RuntimeException()
try {
uploader.upload(jobs, key, "VID_0042.mp4", videoMeta, open) { acked, _ -> if (acked > 0) throw Killed() }
error("the upload should have been cut short")
} catch (e: Killed) {
// as if the app was killed mid-upload
}
assertEquals(1, jobs.unfinished().size)
var firstSeen = -1L
val resumed = uploader.upload(jobs, key, "VID_0042.mp4", videoMeta, open) { acked, _ -> if (firstSeen < 0) firstSeen = acked }
assertTrue("resumed from $firstSeen", firstSeen > 0)
assertEquals("sealed once", 1, opened)
resumed.open().use { r -> assertEquals(sha256(video), sha256(r.read(0, r.size.toInt()))) }
val third = uploader.upload(jobs, key, "VID_0042.mp4", videoMeta, open)
assertEquals(resumed.encode(), third.encode())
assertEquals(1, opened)
assertTrue(jobs.unfinished().isEmpty())
// Deleted from the relay since: the next share is a new upload with a new link.
assertTrue(uploader.delete(resumed.mediaId))
val fresh = uploader.upload(jobs, key, "VID_0042.mp4", videoMeta, open)
assertNotEquals(resumed.encode(), fresh.encode())
assertEquals(2, opened)
// 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) }