mirror of
https://github.com/szkolny-eu/szkolny-android.git
synced 2025-01-18 12:56:45 -06:00
[API] Fix downloading attachments with different name. Handle UTF encoded download names.
This commit is contained in:
parent
d56afb034b
commit
00a90a14dc
@ -35,6 +35,7 @@ class OneDriveDownloadAttachment(
|
|||||||
.callback(object : TextCallbackHandler() {
|
.callback(object : TextCallbackHandler() {
|
||||||
override fun onSuccess(text: String, response: Response) {
|
override fun onSuccess(text: String, response: Response) {
|
||||||
val location = response.headers().get("Location")
|
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) {
|
if (location?.contains("onedrive.live.com/redir?resid=") != true) {
|
||||||
onError(ApiError(TAG, ERROR_ONEDRIVE_DOWNLOAD)
|
onError(ApiError(TAG, ERROR_ONEDRIVE_DOWNLOAD)
|
||||||
.withApiResponse(text)
|
.withApiResponse(text)
|
||||||
@ -43,7 +44,8 @@ class OneDriveDownloadAttachment(
|
|||||||
}
|
}
|
||||||
val url = location
|
val url = location
|
||||||
.replace("onedrive.live.com/redir?resid=", "storage.live.com/items/")
|
.replace("onedrive.live.com/redir?resid=", "storage.live.com/items/")
|
||||||
.replace("&", "?")
|
.replace("?", "&")
|
||||||
|
.replaceFirst("&", "?")
|
||||||
downloadFile(url)
|
downloadFile(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +94,20 @@ class AttachmentsView @JvmOverloads constructor(
|
|||||||
try {
|
try {
|
||||||
val attachmentFileName = Utils.getStringFromFile(attachmentDataFile)
|
val attachmentFileName = Utils.getStringFromFile(attachmentDataFile)
|
||||||
val attachmentFile = File(attachmentFileName)
|
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) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
false
|
false
|
||||||
@ -141,7 +154,7 @@ class AttachmentsView @JvmOverloads constructor(
|
|||||||
attachment.isDownloaded = true
|
attachment.isDownloaded = true
|
||||||
|
|
||||||
// get the download url before updating file name
|
// 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
|
// update file name with the downloaded one
|
||||||
attachment.name = File(event.fileName).name
|
attachment.name = File(event.fileName).name
|
||||||
// save the download url back
|
// save the download url back
|
||||||
|
@ -6,6 +6,7 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.net.URLDecoder;
|
||||||
|
|
||||||
import im.wangchao.mhttp.AbsCallbackHandler;
|
import im.wangchao.mhttp.AbsCallbackHandler;
|
||||||
import im.wangchao.mhttp.Accept;
|
import im.wangchao.mhttp.Accept;
|
||||||
@ -69,7 +70,11 @@ public class FileCallbackHandler extends AbsCallbackHandler<File> {
|
|||||||
if (this.file.isDirectory()) {
|
if (this.file.isDirectory()) {
|
||||||
String contentDisposition = response.header("content-disposition");
|
String contentDisposition = response.header("content-disposition");
|
||||||
if (contentDisposition != null) {
|
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);
|
this.file = new File(file, filename);
|
||||||
file = this.file;
|
file = this.file;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user