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; }