Skip to content

Commit

Permalink
WebdavRepo: store attachments
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xiaoruoruo committed Jun 1, 2021
1 parent 081485a commit 8b47540
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8b47540

Please sign in to comment.