[API/Librus] Fix attachment downloading, once again.

This commit is contained in:
Kuba Szczodrzyński 2020-03-29 15:30:30 +02:00
parent c6e1ff2164
commit 6deb408d80
2 changed files with 12 additions and 9 deletions

View File

@ -253,6 +253,7 @@ open class LibrusMessages(open val data: DataLibrus, open val lastSync: Long?) {
} }
fun sandboxGetFile(tag: String, url: String, targetFile: File, onSuccess: (file: File) -> Unit, fun sandboxGetFile(tag: String, url: String, targetFile: File, onSuccess: (file: File) -> Unit,
method: Int = GET,
onProgress: (written: Long, total: Long) -> Unit) { onProgress: (written: Long, total: Long) -> Unit) {
d(tag, "Request: Librus/Messages - $url") d(tag, "Request: Librus/Messages - $url")
@ -291,9 +292,14 @@ open class LibrusMessages(open val data: DataLibrus, open val lastSync: Long?) {
} }
Request.builder() Request.builder()
.url("$url") .url(url)
.userAgent(SYNERGIA_USER_AGENT) .userAgent(SYNERGIA_USER_AGENT)
.post() .also {
when (method) {
POST -> it.post()
else -> it.get()
}
}
.callback(callback) .callback(callback)
.build() .build()
.enqueue() .enqueue()

View File

@ -6,10 +6,7 @@ package pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.messages
import kotlinx.coroutines.* import kotlinx.coroutines.*
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.data.api.ERROR_FILE_DOWNLOAD import pl.szczodrzynski.edziennik.data.api.*
import pl.szczodrzynski.edziennik.data.api.EXCEPTION_LIBRUS_MESSAGES_REQUEST
import pl.szczodrzynski.edziennik.data.api.LIBRUS_SANDBOX_URL
import pl.szczodrzynski.edziennik.data.api.Regexes
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus import pl.szczodrzynski.edziennik.data.api.edziennik.librus.DataLibrus
import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusMessages import pl.szczodrzynski.edziennik.data.api.edziennik.librus.data.LibrusMessages
import pl.szczodrzynski.edziennik.data.api.events.AttachmentGetEvent import pl.szczodrzynski.edziennik.data.api.events.AttachmentGetEvent
@ -54,10 +51,10 @@ class LibrusMessagesGetAttachment(override val data: DataLibrus,
val attachmentKey = keyMatcher[1] val attachmentKey = keyMatcher[1]
getAttachmentCheckKey(attachmentKey) { getAttachmentCheckKey(attachmentKey) {
downloadAttachment("${LIBRUS_SANDBOX_URL}CSDownload&singleUseKey=$attachmentKey") downloadAttachment("${LIBRUS_SANDBOX_URL}CSDownload&singleUseKey=$attachmentKey", method = POST)
} }
} else { } else {
downloadAttachment(downloadLink) downloadAttachment(downloadLink, method = GET)
} }
} }
} }
@ -91,7 +88,7 @@ class LibrusMessagesGetAttachment(override val data: DataLibrus,
} }
} }
private fun downloadAttachment(url: String) { private fun downloadAttachment(url: String, method: Int = GET) {
val targetFile = File(Utils.getStorageDir(), attachmentName) val targetFile = File(Utils.getStorageDir(), attachmentName)
sandboxGetFile(TAG, url, targetFile, { file -> sandboxGetFile(TAG, url, targetFile, { file ->