From 00a90a14dc17d409d389ed7f0abf3ae7a77fc298 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 7 Apr 2020 13:57:12 +0200 Subject: [PATCH] [API] Fix downloading attachments with different name. Handle UTF encoded download names. --- .../helper/OneDriveDownloadAttachment.kt | 4 +++- .../ui/modules/views/AttachmentsView.kt | 17 +++++++++++++++-- .../mhttp/callback/FileCallbackHandler.java | 7 ++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/helper/OneDriveDownloadAttachment.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/helper/OneDriveDownloadAttachment.kt index f657838b..25b78527 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/helper/OneDriveDownloadAttachment.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/helper/OneDriveDownloadAttachment.kt @@ -35,6 +35,7 @@ class OneDriveDownloadAttachment( .callback(object : TextCallbackHandler() { override fun onSuccess(text: String, response: Response) { val location = response.headers().get("Location") + // https://onedrive.live.com/redir?resid=D75496A2EB87531C!706&authkey=!ABjZeh3pHMqj11Q if (location?.contains("onedrive.live.com/redir?resid=") != true) { onError(ApiError(TAG, ERROR_ONEDRIVE_DOWNLOAD) .withApiResponse(text) @@ -43,7 +44,8 @@ class OneDriveDownloadAttachment( } val url = location .replace("onedrive.live.com/redir?resid=", "storage.live.com/items/") - .replace("&", "?") + .replace("?", "&") + .replaceFirst("&", "?") downloadFile(url) } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/views/AttachmentsView.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/views/AttachmentsView.kt index 682a86a5..482ab847 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/views/AttachmentsView.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/views/AttachmentsView.kt @@ -94,7 +94,20 @@ class AttachmentsView @JvmOverloads constructor( try { val attachmentFileName = Utils.getStringFromFile(attachmentDataFile) val attachmentFile = File(attachmentFileName) - attachmentFile.exists() + // get the correct file name and update + if (attachmentFile.exists()) { + + // get the download url before updating file name + val fileUrl = item.name.substringAfter(":", missingDelimiterValue = "") + // update file name with the downloaded one + item.name = attachmentFile.name + // save the download url back + if (fileUrl != "") + item.name += ":$fileUrl" + + true + } + else false } catch (e: Exception) { e.printStackTrace() false @@ -141,7 +154,7 @@ class AttachmentsView @JvmOverloads constructor( attachment.isDownloaded = true // get the download url before updating file name - val fileUrl = attachment.name.substringBefore(":", missingDelimiterValue = "") + val fileUrl = attachment.name.substringAfter(":", missingDelimiterValue = "") // update file name with the downloaded one attachment.name = File(event.fileName).name // save the download url back diff --git a/mhttp/src/main/java/im/wangchao/mhttp/callback/FileCallbackHandler.java b/mhttp/src/main/java/im/wangchao/mhttp/callback/FileCallbackHandler.java index 0feeebfa..796ec1cf 100644 --- a/mhttp/src/main/java/im/wangchao/mhttp/callback/FileCallbackHandler.java +++ b/mhttp/src/main/java/im/wangchao/mhttp/callback/FileCallbackHandler.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URLDecoder; import im.wangchao.mhttp.AbsCallbackHandler; import im.wangchao.mhttp.Accept; @@ -69,7 +70,11 @@ public class FileCallbackHandler extends AbsCallbackHandler { if (this.file.isDirectory()) { String contentDisposition = response.header("content-disposition"); if (contentDisposition != null) { - String filename = contentDisposition.substring(contentDisposition.indexOf("\"")+1, contentDisposition.lastIndexOf("\"")); + if (contentDisposition.contains("*=UTF-8")) { + contentDisposition = contentDisposition.replace("*=UTF-8''", "\"") + "\""; + contentDisposition = URLDecoder.decode(contentDisposition, "UTF-8"); + } + String filename = contentDisposition.substring(contentDisposition.indexOf("\"") + 1, contentDisposition.lastIndexOf("\"")); this.file = new File(file, filename); file = this.file; }