From 3723abbbbb3582435e6eadd3c884c66eee4a5c48 Mon Sep 17 00:00:00 2001 From: Kacper Ziubryniewicz Date: Sat, 28 Dec 2019 17:02:43 +0100 Subject: [PATCH] [API/Idziennik] Add getting attachments. --- .../edziennik/data/api/Constants.kt | 1 + .../edziennik/data/api/Errors.kt | 1 + .../data/api/edziennik/idziennik/Idziennik.kt | 7 ++- .../edziennik/idziennik/data/IdziennikWeb.kt | 52 ++++++++++++++++ .../data/web/IdziennikWebGetAttachment.kt | 60 +++++++++++++++++++ app/src/main/res/values/errors.xml | 2 + 6 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGetAttachment.kt diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt index 5768f95f..19763302 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Constants.kt @@ -70,6 +70,7 @@ const val IDZIENNIK_WEB_ATTENDANCE = "mod_panelRodzica/obecnosci/WS_obecnosciUcz const val IDZIENNIK_WEB_ANNOUNCEMENTS = "mod_panelRodzica/tabOgl/WS_tablicaOgloszen.asmx/GetOgloszenia" const val IDZIENNIK_WEB_MESSAGES_LIST = "mod_komunikator/WS_wiadomosci.asmx/PobierzListeWiadomosci" const val IDZIENNIK_WEB_GET_MESSAGE = "mod_komunikator/WS_wiadomosci.asmx/PobierzWiadomosc" +const val IDZIENNIK_WEB_GET_ATTACHMENT = "mod_komunikator/Download.ashx" val IDZIENNIK_API_USER_AGENT = SYSTEM_USER_AGENT const val IDZIENNIK_API_URL = "https://iuczniowie.progman.pl/idziennik/api" diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt index 24d93481..01f3f3d1 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/Errors.kt @@ -184,5 +184,6 @@ const val EXCEPTION_IDZIENNIK_WEB_REQUEST = 912 const val EXCEPTION_IDZIENNIK_WEB_API_REQUEST = 913 const val EXCEPTION_IDZIENNIK_API_REQUEST = 914 const val EXCEPTION_EDUDZIENNIK_WEB_REQUEST = 920 +const val EXCEPTION_EDUDZIENNIK_FILE_REQUEST = 921 const val LOGIN_NO_ARGUMENTS = 1201 diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt index e45f29c3..66a96a6d 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/Idziennik.kt @@ -8,6 +8,7 @@ import com.google.gson.JsonObject import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.data.api.CODE_INTERNAL_LIBRUS_ACCOUNT_410 import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.IdziennikData +import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.web.IdziennikWebGetAttachment import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.web.IdziennikWebGetMessage import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.firstlogin.IdziennikFirstLogin import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.login.IdziennikLogin @@ -83,7 +84,11 @@ class Idziennik(val app: App, val profile: Profile?, val loginStore: LoginStore, } override fun getAttachment(message: Message, attachmentId: Long, attachmentName: String) { - + IdziennikLoginWeb(data) { + IdziennikWebGetAttachment(data, message, attachmentId, attachmentName) { + completed() + } + } } override fun firstLogin() { diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikWeb.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikWeb.kt index c7b6820a..b454a2ff 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikWeb.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/IdziennikWeb.kt @@ -8,12 +8,14 @@ import com.google.gson.JsonArray import com.google.gson.JsonObject import im.wangchao.mhttp.Request import im.wangchao.mhttp.Response +import im.wangchao.mhttp.callback.FileCallbackHandler import im.wangchao.mhttp.callback.JsonCallbackHandler import im.wangchao.mhttp.callback.TextCallbackHandler import pl.szczodrzynski.edziennik.data.api.* import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.DataIdziennik import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.utils.Utils.d +import java.io.File import java.net.HttpURLConnection.HTTP_INTERNAL_ERROR import java.net.HttpURLConnection.HTTP_UNAUTHORIZED @@ -156,4 +158,54 @@ open class IdziennikWeb(open val data: DataIdziennik) { .build() .enqueue() } + + fun webGetFile(tag: String, endpoint: String, targetFile: File, parameters: Map, + onSuccess: (file: File) -> Unit, onProgress: (written: Long, total: Long) -> Unit) { + + d(tag, "Request: Idziennik/Web - $IDZIENNIK_WEB_URL/$endpoint") + + val callback = object : FileCallbackHandler(targetFile) { + override fun onSuccess(file: File?, response: Response?) { + if (file == null) { + data.error(ApiError(TAG, ERROR_FILE_DOWNLOAD) + .withResponse(response)) + return + } + + try { + onSuccess(file) + } catch (e: Exception) { + data.error(ApiError(tag, EXCEPTION_EDUDZIENNIK_FILE_REQUEST) + .withResponse(response) + .withThrowable(e)) + } + } + + override fun onProgress(bytesWritten: Long, bytesTotal: Long) { + try { + onProgress(bytesWritten, bytesTotal) + } catch (e: Exception) { + data.error(ApiError(tag, EXCEPTION_EDUDZIENNIK_FILE_REQUEST) + .withThrowable(e)) + } + } + + override fun onFailure(response: Response?, throwable: Throwable?) { + data.error(ApiError(tag, ERROR_REQUEST_FAILURE) + .withResponse(response) + .withThrowable(throwable)) + } + } + + Request.builder() + .url("$IDZIENNIK_WEB_URL/$endpoint") + .userAgent(IDZIENNIK_USER_AGENT) + .apply { + parameters.forEach { (k, v) -> addParameter(k, v) } + } + .post() + .callback(callback) + .build() + .enqueue() + } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGetAttachment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGetAttachment.kt new file mode 100644 index 00000000..16e5ce7f --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/idziennik/data/web/IdziennikWebGetAttachment.kt @@ -0,0 +1,60 @@ +/* + * Copyright (c) Kacper Ziubryniewicz 2019-12-28 + */ + +package pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.web + +import org.greenrobot.eventbus.EventBus +import pl.szczodrzynski.edziennik.data.api.IDZIENNIK_WEB_GET_ATTACHMENT +import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.DataIdziennik +import pl.szczodrzynski.edziennik.data.api.edziennik.idziennik.data.IdziennikWeb +import pl.szczodrzynski.edziennik.data.api.events.AttachmentGetEvent +import pl.szczodrzynski.edziennik.data.db.modules.messages.Message +import pl.szczodrzynski.edziennik.get +import pl.szczodrzynski.edziennik.utils.Utils +import java.io.File + +class IdziennikWebGetAttachment( + override val data: DataIdziennik, val message: Message, val attachmentId: Long, + val attachmentName: String, val onSuccess: () -> Unit +) : IdziennikWeb(data) { + companion object { + const val TAG = "IdziennikWebGetAttachment" + } + + init { + val messageId = "\\[META:([A-z0-9]+);([0-9-]+)]".toRegex().find(message.body ?: "")?.get(2) ?: -1 + val targetFile = File(Utils.getStorageDir(), attachmentName) + + webGetFile(TAG, IDZIENNIK_WEB_GET_ATTACHMENT, targetFile, mapOf( + "id" to messageId, + "fileName" to attachmentName + ), { file -> + val event = AttachmentGetEvent( + profileId, + message.id, + attachmentId, + AttachmentGetEvent.TYPE_FINISHED, + file.absolutePath + ) + + val attachmentDataFile = File(Utils.getStorageDir(), ".${profileId}_${event.messageId}_${event.attachmentId}") + Utils.writeStringToFile(attachmentDataFile, event.fileName) + + EventBus.getDefault().post(event) + + onSuccess() + + }) { written, _ -> + val event = AttachmentGetEvent( + profileId, + message.id, + attachmentId, + AttachmentGetEvent.TYPE_PROGRESS, + bytesWritten = written + ) + + EventBus.getDefault().post(event) + } + } +} diff --git a/app/src/main/res/values/errors.xml b/app/src/main/res/values/errors.xml index 334e0d25..b66a1f4b 100644 --- a/app/src/main/res/values/errors.xml +++ b/app/src/main/res/values/errors.xml @@ -152,6 +152,7 @@ EXCEPTION_IDZIENNIK_WEB_API_REQUEST EXCEPTION_IDZIENNIK_API_REQUEST EXCEPTION_EDUDZIENNIK_WEB_REQUEST + EXCEPTION_EDUDZIENNIK_FILE_REQUEST LOGIN_NO_ARGUMENTS @@ -303,6 +304,7 @@ EXCEPTION_IDZIENNIK_WEB_API_REQUEST EXCEPTION_IDZIENNIK_API_REQUEST Wystąpił błąd + Wystąpił błąd podczas pobierania pliku Nie podano parametrów