[API/Librus] Fix attachment downloading.

This commit is contained in:
Kuba Szczodrzyński 2020-03-29 15:16:35 +02:00
parent 55ff9173be
commit bc0918a115
3 changed files with 13 additions and 8 deletions

View File

@ -252,10 +252,10 @@ open class LibrusMessages(open val data: DataLibrus, open val lastSync: Long?) {
.enqueue()
}
fun sandboxGetFile(tag: String, action: String, targetFile: File, onSuccess: (file: File) -> Unit,
fun sandboxGetFile(tag: String, url: String, targetFile: File, onSuccess: (file: File) -> Unit,
onProgress: (written: Long, total: Long) -> Unit) {
d(tag, "Request: Librus/Messages - $LIBRUS_SANDBOX_URL$action")
d(tag, "Request: Librus/Messages - $url")
val callback = object : FileCallbackHandler(targetFile) {
override fun onSuccess(file: File?, response: Response?) {
@ -291,7 +291,7 @@ open class LibrusMessages(open val data: DataLibrus, open val lastSync: Long?) {
}
Request.builder()
.url("$LIBRUS_SANDBOX_URL$action")
.url("$url")
.userAgent(SYNERGIA_USER_AGENT)
.post()
.callback(callback)

View File

@ -8,6 +8,7 @@ import kotlinx.coroutines.*
import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.data.api.ERROR_FILE_DOWNLOAD
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.data.LibrusMessages
@ -53,11 +54,10 @@ class LibrusMessagesGetAttachment(override val data: DataLibrus,
val attachmentKey = keyMatcher[1]
getAttachmentCheckKey(attachmentKey) {
downloadAttachment(attachmentKey)
downloadAttachment("${LIBRUS_SANDBOX_URL}CSDownload&singleUseKey=$attachmentKey")
}
} else {
data.error(ApiError(TAG, ERROR_FILE_DOWNLOAD)
.withApiResponse(doc.toString()))
downloadAttachment(downloadLink)
}
}
}
@ -91,10 +91,10 @@ class LibrusMessagesGetAttachment(override val data: DataLibrus,
}
}
private fun downloadAttachment(attachmentKey: String) {
private fun downloadAttachment(url: String) {
val targetFile = File(Utils.getStorageDir(), attachmentName)
sandboxGetFile(TAG, "CSDownload&singleUseKey=$attachmentKey", targetFile, { file ->
sandboxGetFile(TAG, url, targetFile, { file ->
val event = AttachmentGetEvent(
profileId,

View File

@ -109,6 +109,11 @@ class LibrusMessagesGetList(override val data: DataLibrus,
id
)
element.select("isAnyFileAttached")?.text()?.let {
if (it == "1")
messageObject.overrideHasAttachments = true
}
data.messageIgnoreList.add(messageObject)
data.messageRecipientList.add(messageRecipientObject)
data.setSeenMetadataList.add(Metadata(