|
|
|
@@ -17,10 +17,14 @@ import java.security.MessageDigest
|
|
|
|
|
* The data a box already has belongs to the original server (the only one Orbit could use before an address could be typed
|
|
|
|
|
* in), and it stays exactly where it always was: nothing is moved, copied or rewritten, so an older Orbit still finds all of
|
|
|
|
|
* it. Any other server's data lives under servers/<key>, where an older Orbit never looks.
|
|
|
|
|
*
|
|
|
|
|
* No server is built in (launch blocker, 10 November 2026): the original server is the one named in a small note,
|
|
|
|
|
* original-server, beside its data. A box from before the note learns it once from its own saved sign-in; a new box's first
|
|
|
|
|
* server signed in to becomes its original. Until then nothing is open and the sign-in's address field is empty.
|
|
|
|
|
*/
|
|
|
|
|
internal class ServerWorkspace(val origin: String, val root: File, val artworkDir: File, val keyAlias: String) {
|
|
|
|
|
/** The server this box used before addresses could be typed in: its data is the box's existing data, where it was. */
|
|
|
|
|
val original get() = origin == EmbyServer.DEFAULT
|
|
|
|
|
internal class ServerWorkspace(val origin: String, val root: File, val artworkDir: File, val keyAlias: String,
|
|
|
|
|
/** The box's first server, whose data sits at the top of no_backup where it always was (see [Workspaces.originalOrigin]). */
|
|
|
|
|
val original: Boolean) {
|
|
|
|
|
val connectionFile get() = File(root, "orbit-connection.v1")
|
|
|
|
|
val catalogueFile get() = File(root, "catalogue.db")
|
|
|
|
|
val scheduleDir get() = File(root, "schedule")
|
|
|
|
@@ -38,30 +42,65 @@ internal object Workspaces {
|
|
|
|
|
private const val ORIGINAL_ALIAS = "orbit-connection-key-v1"
|
|
|
|
|
private const val ACTIVE_FILE = "active-server"
|
|
|
|
|
private const val ORIGIN_FILE = "server"
|
|
|
|
|
private const val ORIGINAL_FILE = "original-server"
|
|
|
|
|
/**
|
|
|
|
|
* Paul's own server, as a SHA-256 of its address, so the address itself isn't in the app: his decisions by item apply
|
|
|
|
|
* there and nowhere else, and a box from before the note whose sign-in can't be read gives its data to that server only.
|
|
|
|
|
*/
|
|
|
|
|
private const val PAULS_SERVER_SHA256 = "8a2a645acebc3401b07585a214d4b7dd0f24e254b8377e4b59128931c2376e80"
|
|
|
|
|
internal var isPaulsServer: (String) -> Boolean = { origin ->
|
|
|
|
|
MessageDigest.getInstance("SHA-256").digest(origin.toByteArray(Charsets.UTF_8)).joinToString("") { "%02x".format(it) } == PAULS_SERVER_SHA256
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var current by mutableStateOf<ServerWorkspace?>(null)
|
|
|
|
|
private set
|
|
|
|
|
/** Whether the original server is the one open (or none is yet, as in tests): Paul's own decisions by item apply only there. */
|
|
|
|
|
val originalOpen: Boolean get() = current?.original ?: true
|
|
|
|
|
/** Whether Paul's server is the one open (or none is yet, as in tests): his own decisions by item apply only there. */
|
|
|
|
|
val paulsServerOpen: Boolean get() = current?.origin?.let { isPaulsServer(it) } ?: true
|
|
|
|
|
|
|
|
|
|
/** A short, stable folder name for [origin]. */
|
|
|
|
|
fun keyOf(origin: String): String = MessageDigest.getInstance("SHA-256").digest(origin.toByteArray(Charsets.UTF_8))
|
|
|
|
|
.take(8).joinToString("") { "%02x".format(it) }
|
|
|
|
|
|
|
|
|
|
private fun readOrigin(file: File): String? = runCatching { file.takeIf { it.isFile && it.length() <= 300 }?.readText()?.trim() }.getOrNull()
|
|
|
|
|
?.takeIf { EmbyServer.normalise(it) == it }
|
|
|
|
|
|
|
|
|
|
/** The box's original server, from its note; null on a new box, or a box from before the note until it learns it. */
|
|
|
|
|
fun originalOrigin(noBackup: File): String? = readOrigin(File(noBackup, ORIGINAL_FILE))
|
|
|
|
|
|
|
|
|
|
private fun rootLayout(noBackup: File, cacheDir: File, origin: String) = ServerWorkspace(origin, noBackup, File(cacheDir, "artwork"), ORIGINAL_ALIAS, original = true)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Whether [origin] would be the original server: the one in the note, or, with no note yet, the first server on a box with
|
|
|
|
|
* no data of its own, or Paul's server on a box from before the note (the only server an older Orbit could use).
|
|
|
|
|
*/
|
|
|
|
|
private fun isOriginal(noBackup: File, cacheDir: File, origin: String): Boolean = originalOrigin(noBackup)?.let { it == origin }
|
|
|
|
|
?: (!hasData(rootLayout(noBackup, cacheDir, origin)) || isPaulsServer(origin))
|
|
|
|
|
|
|
|
|
|
/** Where [origin]'s data lives under [noBackup] and [cacheDir]. Nothing is created here. */
|
|
|
|
|
fun layout(noBackup: File, cacheDir: File, origin: String): ServerWorkspace =
|
|
|
|
|
if (origin == EmbyServer.DEFAULT) ServerWorkspace(origin, noBackup, File(cacheDir, "artwork"), ORIGINAL_ALIAS)
|
|
|
|
|
else keyOf(origin).let { key -> ServerWorkspace(origin, File(noBackup, "servers/$key"), File(cacheDir, "artwork-$key"), "$ORIGINAL_ALIAS-$key") }
|
|
|
|
|
if (isOriginal(noBackup, cacheDir, origin)) rootLayout(noBackup, cacheDir, origin)
|
|
|
|
|
else keyOf(origin).let { key -> ServerWorkspace(origin, File(noBackup, "servers/$key"), File(cacheDir, "artwork-$key"), "$ORIGINAL_ALIAS-$key", original = false) }
|
|
|
|
|
|
|
|
|
|
/** The server whose workspace was open last: the original server until another has been signed in to. */
|
|
|
|
|
fun activeOrigin(noBackup: File): String {
|
|
|
|
|
val saved = runCatching { File(noBackup, ACTIVE_FILE).takeIf { it.isFile && it.length() <= 300 }?.readText()?.trim() }.getOrNull() ?: return EmbyServer.DEFAULT
|
|
|
|
|
return saved.takeIf { EmbyServer.normalise(it) == it } ?: EmbyServer.DEFAULT
|
|
|
|
|
/**
|
|
|
|
|
* The server whose workspace was open last, or the original server, or null when there's none yet. A box from before the
|
|
|
|
|
* note has its original server's address only in its saved sign-in: [savedOrigin] reads it from there.
|
|
|
|
|
*/
|
|
|
|
|
fun activeOrigin(noBackup: File, savedOrigin: () -> String? = { null }): String? =
|
|
|
|
|
readOrigin(File(noBackup, ACTIVE_FILE)) ?: originalOrigin(noBackup)
|
|
|
|
|
?: if (File(noBackup, "orbit-connection.v1").exists()) savedOrigin()?.takeIf { EmbyServer.normalise(it) == it } else null
|
|
|
|
|
|
|
|
|
|
/** The open workspace, opening the last one used if none is yet; with no server yet, the box's own empty place for one. */
|
|
|
|
|
@Synchronized fun current(context: Context): ServerWorkspace {
|
|
|
|
|
current?.let { return it }
|
|
|
|
|
val app = context.applicationContext
|
|
|
|
|
val origin = activeOrigin(app.noBackupFilesDir) {
|
|
|
|
|
// Once, on a box from before the note: the address its own sign-in was saved with.
|
|
|
|
|
RememberedConnectionStore(File(app.noBackupFilesDir, "orbit-connection.v1"), ORIGINAL_ALIAS, "").savedOrigin()
|
|
|
|
|
}
|
|
|
|
|
if (origin != null) return open(app, origin)
|
|
|
|
|
return rootLayout(app.noBackupFilesDir, app.cacheDir, "").also { it.appContext = app; current = it }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** The open workspace, opening the last one used if none is yet. */
|
|
|
|
|
@Synchronized fun current(context: Context): ServerWorkspace = current ?: open(context, activeOrigin(context.noBackupFilesDir))
|
|
|
|
|
|
|
|
|
|
@Synchronized fun open(context: Context, origin: String): ServerWorkspace {
|
|
|
|
|
val app = context.applicationContext
|
|
|
|
|
return openIn(app.noBackupFilesDir, app.cacheDir, origin) { it.appContext = app }
|
|
|
|
@@ -76,7 +115,8 @@ internal object Workspaces {
|
|
|
|
|
require(EmbyServer.normalise(origin) == origin)
|
|
|
|
|
if (current?.origin == origin) return current!!
|
|
|
|
|
val next = layout(noBackup, cacheDir, origin).also(prepare)
|
|
|
|
|
if (!next.original) {
|
|
|
|
|
if (next.original) { if (originalOrigin(noBackup) == null) replace(File(noBackup, ORIGINAL_FILE), origin) }
|
|
|
|
|
else {
|
|
|
|
|
val note = File(next.root, ORIGIN_FILE)
|
|
|
|
|
if (note.exists()) check(note.readText().trim() == origin) { "A server folder belongs to another address." }
|
|
|
|
|
else { next.root.mkdirs(); replace(note, origin) }
|
|
|
|
@@ -93,7 +133,8 @@ internal object Workspaces {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** For tests: no workspace open. */
|
|
|
|
|
@Synchronized internal fun reset() { current?.close(); current = null; EmbyServer.origin = EmbyServer.DEFAULT }
|
|
|
|
|
@Synchronized internal fun reset() { current?.close(); current = null; EmbyServer.origin = ""; isPaulsServer = defaultPaulsCheck }
|
|
|
|
|
private val defaultPaulsCheck = isPaulsServer
|
|
|
|
|
|
|
|
|
|
/** A small file written whole or not at all. */
|
|
|
|
|
private fun replace(target: File, text: String) {
|
|
|
|
@@ -108,9 +149,10 @@ internal object Workspaces {
|
|
|
|
|
|
|
|
|
|
/** Every server with data on this box, the original first if it has any. Read only. */
|
|
|
|
|
fun known(noBackup: File, cacheDir: File): List<String> {
|
|
|
|
|
val original = originalOrigin(noBackup)
|
|
|
|
|
val others = File(noBackup, "servers").listFiles().orEmpty().mapNotNull { dir -> File(dir, ORIGIN_FILE).takeIf { it.isFile }?.readText()?.trim() }
|
|
|
|
|
.filter { EmbyServer.normalise(it) == it && it != EmbyServer.DEFAULT }.sorted()
|
|
|
|
|
return (listOf(EmbyServer.DEFAULT) + others).filter { hasData(layout(noBackup, cacheDir, it)) }
|
|
|
|
|
.filter { EmbyServer.normalise(it) == it && it != original }.sorted()
|
|
|
|
|
return (listOfNotNull(original) + others).filter { hasData(layout(noBackup, cacheDir, it)) }
|
|
|
|
|
}
|
|
|
|
|
fun known(context: Context) = known(context.noBackupFilesDir, context.cacheDir)
|
|
|
|
|
|
|
|
|
|