Working config for WebDAV

This commit is contained in:
2024-11-17 09:12:12 +01:00
parent 83be0fa5fb
commit 308092415b
7 changed files with 229 additions and 103 deletions

View File

@@ -15,21 +15,25 @@ class FileLogbookRepository: LogbookRepository {
}
override fun loadLogbook(context: Context, listener: LogbookLoadedListener) {
val logbook = Logbook()
val file = File(context.getFilesDir(), "data.json")
try {
val json = FileInputStream(file).bufferedReader().use { it.readText() }
val ja = JSONArray(json)
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
listener.onLogbookLoaded(loadLogbook(context))
} catch (e: FileNotFoundException) {
Log.d(TAG, "No logbook file found")
listener.onIOError(e)
}
listener.onLogbookLoaded(logbook)
}
fun loadLogbook(context: Context): Logbook {
val logbook = Logbook()
val file = File(context.getFilesDir(), "data.json")
val json = FileInputStream(file).bufferedReader().use { it.readText() }
val ja = JSONArray(json)
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
return logbook
}
override fun saveLogbook(

View File

@@ -8,7 +8,7 @@ class LocalSettingsRepository(val context: Context) {
companion object {
val SHARED_PREFS_FILE_NAME = "lunasettings"
val SHARED_PREFS_BB_CONTENT = "bbcontent"
val SHARED_PREFS_DATA_REPO = "webdav_url"
val SHARED_PREFS_DATA_REPO = "data_repo"
val SHARED_PREFS_DAV_URL = "webdav_url"
val SHARED_PREFS_DAV_USER = "webdav_user"
val SHARED_PREFS_DAV_PASS = "webdav_password"

View File

@@ -21,5 +21,8 @@ interface LogbookLoadedListener {
interface LogbookSavedListener {
fun onLogbookSaved()
fun onError(error: String)
fun onIOError(error: IOException)
fun onWebDAVError(error: SardineException)
fun onJSONError(error: JSONException)
fun onError(error: Exception)
}

View File

@@ -10,6 +10,7 @@ import kotlinx.coroutines.Runnable
import org.json.JSONArray
import org.json.JSONException
import java.io.BufferedReader
import java.io.FileNotFoundException
import java.io.IOException
import java.net.SocketTimeoutException
import kotlin.io.bufferedReader
@@ -31,16 +32,7 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
override fun loadLogbook(context: Context, listener: LogbookLoadedListener) {
Thread(Runnable {
try {
val inputStream = sardine.get("$webDavURL/$FILE_NAME")
val json = inputStream.bufferedReader().use(BufferedReader::readText)
inputStream.close()
val ja = JSONArray(json)
val logbook = Logbook()
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
val logbook = loadLogbook(context)
listener.onLogbookLoaded(logbook)
} catch (e: SardineException) {
Log.e(TAG, e.toString())
@@ -60,31 +52,115 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
}).start()
}
private fun loadLogbook(context: Context): Logbook {
val inputStream = sardine.get("$webDavURL/$FILE_NAME")
val json = inputStream.bufferedReader().use(BufferedReader::readText)
inputStream.close()
val ja = JSONArray(json)
val logbook = Logbook()
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
return logbook
}
override fun saveLogbook(context: Context, logbook: Logbook, listener: LogbookSavedListener) {
Thread(Runnable {
// Lock logbook on WebDAV to avoid concurrent changes
//sardine.lock(getUrl())
// Reload logbook from WebDAV
// Merge logbooks (based on time)
// Write logbook
// Unlock logbook on WebDAV
//sardine.unlock(getUrl())
val ja = JSONArray()
for (l in logbook.logs) {
ja.put(l.toJson())
}
try {
sardine.put(getUrl(), ja.toString().toByteArray())
saveLogbook(context, logbook)
listener.onLogbookSaved()
} catch (e: SardineException) {
listener.onError(e.toString())
Log.e(TAG, e.toString())
listener.onWebDAVError(e)
} catch (e: IOException) {
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: SocketTimeoutException) {
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: JSONException) {
Log.e(TAG, e.toString())
listener.onJSONError(e)
} catch (e: Exception) {
listener.onError(e)
}
}).start()
}
private fun saveLogbook(context: Context, logbook: Logbook) {
// Lock logbook on WebDAV to avoid concurrent changes
//sardine.lock(getUrl())
// Reload logbook from WebDAV
// Merge logbooks (based on time)
// Write logbook
// Unlock logbook on WebDAV
//sardine.unlock(getUrl())
val ja = JSONArray()
for (l in logbook.logs) {
ja.put(l.toJson())
}
sardine.put(getUrl(), ja.toString().toByteArray())
}
/**
* Connect to server and check if a logbook already exists.
* If it does not exist, try to upload the local one (or create a new one).
*/
fun createLogbook(context: Context, listener: LogbookCreatedListener) {
Thread(Runnable {
try {
loadLogbook(context)
listener.onLogbookCreated()
} catch (e: SardineException) {
if (e.toString().contains("404")) {
// Connection successful, but no existing save. Upload the local one.
try {
val flr = FileLogbookRepository()
val logbook = flr.loadLogbook(context)
saveLogbook(context, logbook)
Log.d(TAG, "Local logbook file found, uploaded")
listener.onLogbookCreated()
} catch (e: FileNotFoundException) {
Log.d(TAG, "No local logbook file found, uploading empty file")
saveLogbook(context, Logbook())
listener.onLogbookCreated()
} catch (e: SardineException) {
Log.e(TAG, "Unable to upload logbook: $e")
listener.onWebDAVError(e)
}
} else {
Log.e(TAG, e.toString())
listener.onWebDAVError(e)
}
} catch (e: IOException) {
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: SocketTimeoutException) {
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: JSONException) {
Log.e(TAG, e.toString())
listener.onJSONError(e)
} catch (e: Exception) {
listener.onError(e)
}
}).start()
}
private fun getUrl(): String {
return "$webDavURL/$FILE_NAME"
}
interface LogbookCreatedListener {
fun onLogbookCreated()
fun onIOError(error: okio.IOException)
fun onWebDAVError(error: SardineException)
fun onJSONError(error: JSONException)
fun onError(error: Exception)
}
}