Skip to content

Commit 8b47540

Browse files
committed
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.
1 parent 081485a commit 8b47540

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

app/src/main/java/com/orgzly/android/repos/WebdavRepo.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.thegrizzlylabs.sardineandroid.impl.OkHttpSardine
88
import okhttp3.OkHttpClient
99
import okio.Buffer
1010
import java.io.File
11+
import java.io.FileNotFoundException
1112
import java.io.FileOutputStream
1213
import java.io.InputStream
1314
import java.security.KeyStore
@@ -165,7 +166,36 @@ class WebdavRepo(
165166
}
166167

167168
override fun storeFile(file: File?, pathInRepo: String?, fileName: String?): VersionedRook {
168-
TODO("Not yet implemented")
169+
if (file == null || !file.exists()) {
170+
throw FileNotFoundException("File $file does not exist")
171+
}
172+
173+
val folderUri = Uri.withAppendedPath(uri, pathInRepo)
174+
val fileUrl = Uri.withAppendedPath(folderUri, fileName).toUrl()
175+
176+
createRecursive(uri.toUrl(), pathInRepo!!)
177+
178+
sardine.put(fileUrl, file, null)
179+
180+
return sardine.list(fileUrl).first().toVersionedRook()
181+
}
182+
183+
private fun createRecursive(parent: String, path: String): String {
184+
if ("." == path || "" == path) {
185+
return parent
186+
}
187+
val l = path.lastIndexOf('/')
188+
val p = if (l >= 0) {
189+
createRecursive(parent, path.substring(0, l))
190+
} else {
191+
parent
192+
}
193+
val subdir = path.substring(l + 1)
194+
val folder = p + "/" + subdir
195+
if (!sardine.exists(folder)) {
196+
sardine.createDirectory(folder)
197+
}
198+
return folder
169199
}
170200

171201
override fun renameBook(from: Uri, name: String?): VersionedRook {

0 commit comments

Comments
 (0)