[API] Fix downloading attachments with different name. Handle UTF encoded download names.

This commit is contained in:
Kuba Szczodrzyński 2020-04-07 13:57:12 +02:00
parent d56afb034b
commit 00a90a14dc
3 changed files with 24 additions and 4 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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<File> {
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;
}