From 8b47540678ffde38699ef4c2dbc8ae9b2650c12b Mon Sep 17 00:00:00 2001 From: Xinruo Sun Date: Mon, 31 May 2021 21:53:02 -0700 Subject: [PATCH] WebdavRepo: store attachments Tested with a webdav server to confirm that attachment and its parent directories can be created. Caveat: - The attachment: link cannot be resolved. Because this link currently only resolves to local files, it cannot deal with remote files (like webdav). To handle remote files, need additional changes to download and cache the remote files. --- .../com/orgzly/android/repos/WebdavRepo.kt | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt b/app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt index 59cdae15c..dfc11b021 100644 --- a/app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt +++ b/app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt @@ -8,6 +8,7 @@ import com.thegrizzlylabs.sardineandroid.impl.OkHttpSardine import okhttp3.OkHttpClient import okio.Buffer import java.io.File +import java.io.FileNotFoundException import java.io.FileOutputStream import java.io.InputStream import java.security.KeyStore @@ -165,7 +166,36 @@ class WebdavRepo( } override fun storeFile(file: File?, pathInRepo: String?, fileName: String?): VersionedRook { - TODO("Not yet implemented") + if (file == null || !file.exists()) { + throw FileNotFoundException("File $file does not exist") + } + + val folderUri = Uri.withAppendedPath(uri, pathInRepo) + val fileUrl = Uri.withAppendedPath(folderUri, fileName).toUrl() + + createRecursive(uri.toUrl(), pathInRepo!!) + + sardine.put(fileUrl, file, null) + + return sardine.list(fileUrl).first().toVersionedRook() + } + + private fun createRecursive(parent: String, path: String): String { + if ("." == path || "" == path) { + return parent + } + val l = path.lastIndexOf('/') + val p = if (l >= 0) { + createRecursive(parent, path.substring(0, l)) + } else { + parent + } + val subdir = path.substring(l + 1) + val folder = p + "/" + subdir + if (!sardine.exists(folder)) { + sardine.createDirectory(folder) + } + return folder } override fun renameBook(from: Uri, name: String?): VersionedRook {