forked from Ivasoft/mattermost-mobile
Use timeout defaults for iOS Share Extension and Notification Service (#7051)
* Use timeout defaults for iOS Share Extension and Notification Service * more logs * Add more logs, handle errors and safe parse the filename
This commit is contained in:
@@ -24,8 +24,6 @@ public class Network: NSObject {
|
||||
let config = URLSessionConfiguration.default
|
||||
config.httpAdditionalHeaders = ["X-Requested-With": "XMLHttpRequest"]
|
||||
config.allowsCellularAccess = true
|
||||
config.timeoutIntervalForRequest = 10
|
||||
config.timeoutIntervalForResource = 10
|
||||
config.httpMaximumConnectionsPerHost = 10
|
||||
|
||||
self.session = URLSession.init(
|
||||
|
||||
@@ -15,8 +15,6 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
config.waitsForConnectivity = true
|
||||
config.httpAdditionalHeaders = ["X-Requested-With": "XMLHttpRequest"]
|
||||
config.allowsCellularAccess = true
|
||||
config.timeoutIntervalForRequest = 10
|
||||
config.timeoutIntervalForResource = 10
|
||||
config.httpMaximumConnectionsPerHost = 10
|
||||
|
||||
self.backgroundSession = URLSession.init(
|
||||
@@ -74,15 +72,17 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
if let fileInfos = json.object(forKey: "file_infos") as? NSArray,
|
||||
fileInfos.count > 0 {
|
||||
let fileData = fileInfos[0] as! NSDictionary
|
||||
let fileId = fileData.object(forKey: "id") as! String
|
||||
let fileId = fileData.object(forKey: "id") as? String ?? "no file id"
|
||||
let filename = fileData.object(forKey: "name") as? String ?? "no file name"
|
||||
appendCompletedUploadToSession(id: id, fileId: fileId)
|
||||
let total = uploadData.totalFiles
|
||||
let count = uploadData.fileIds.count + 1
|
||||
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: identifier=%{public}@ did upload file %{public}@ total files %{public}@ of %{public}@",
|
||||
"Mattermost BackgroundSession: identifier=%{public}@ did upload file %{public}@ with ID %{public}@ total files %{public}@ of %{public}@",
|
||||
id,
|
||||
filename,
|
||||
fileId,
|
||||
"\(count)",
|
||||
"\(total)"
|
||||
@@ -90,8 +90,9 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: Append file to session identifier=%{public}@ file=%{public}@",
|
||||
"Mattermost BackgroundSession: Append file to session identifier=%{public}@ file=%{public}@ with ID %{public}@",
|
||||
id,
|
||||
filename,
|
||||
fileId
|
||||
)
|
||||
} else {
|
||||
@@ -121,7 +122,7 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
else {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: didCompleteWithError failed to getUploadSessionData identifier=%{public}@",
|
||||
"Mattermost BackgroundSession: didCompleteWithError delegate failed to getUploadSessionData identifier=%{public}@",
|
||||
session.configuration.identifier ?? "no identifier"
|
||||
)
|
||||
return
|
||||
@@ -132,7 +133,7 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
let count = data.fileIds.count
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: didCompleteWithError for identifier=%{public}@ total files %{public}@ of %{public}@",
|
||||
"Mattermost BackgroundSession: didCompleteWithError delegate for identifier=%{public}@ total files %{public}@ of %{public}@",
|
||||
id,
|
||||
"\(count)",
|
||||
"\(total)"
|
||||
@@ -152,7 +153,7 @@ extension ShareExtension: URLSessionDataDelegate {
|
||||
} else if error != nil {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: didCompleteWithError failed identifier=%{public}@ with error %{public}@",
|
||||
"Mattermost BackgroundSession: didCompleteWithError delegate failed identifier=%{public}@ with error %{public}@",
|
||||
session.configuration.identifier ?? "no identifier",
|
||||
error?.localizedDescription ?? "no error description available"
|
||||
)
|
||||
|
||||
@@ -10,7 +10,7 @@ import os.log
|
||||
|
||||
extension ShareExtension {
|
||||
public func uploadFiles(serverUrl: String, channelId: String, message: String,
|
||||
files: [String], completionHandler: @escaping () -> Void) -> String? {
|
||||
files: [String], completionHandler: @escaping () -> Void) -> String? {
|
||||
let id = "mattermost-share-upload-\(UUID().uuidString)"
|
||||
|
||||
createUploadSessionData(
|
||||
@@ -23,23 +23,57 @@ files: [String], completionHandler: @escaping () -> Void) -> String? {
|
||||
|
||||
if !files.isEmpty {
|
||||
createBackroundSession(id: id)
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: uploading %{public}@ files for identifier=%{public}@",
|
||||
String(files.count),
|
||||
id
|
||||
)
|
||||
for file in files {
|
||||
if let fileUrl = URL(string: file),
|
||||
fileUrl.isFileURL {
|
||||
let filename = fileUrl.lastPathComponent
|
||||
|
||||
if let url = URL(string: "\(serverUrl)/api/v4/files?channel_id=\(channelId)&filename=\(filename)") {
|
||||
let safeFilename = filename.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
|
||||
if let safeFilename = safeFilename,
|
||||
let url = URL(string: "\(serverUrl)/api/v4/files?channel_id=\(channelId)&filename=\(safeFilename)") {
|
||||
var uploadRequest = URLRequest(url: url)
|
||||
uploadRequest.httpMethod = "POST"
|
||||
uploadRequest.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
|
||||
|
||||
if let task = backgroundSession?.uploadTask(
|
||||
with: uploadRequest,
|
||||
fromFile: fileUrl
|
||||
) {
|
||||
if let task = backgroundSession?.uploadTask(with: uploadRequest, fromFile: fileUrl) {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: Start uploading file %{public}@ for identifier=%{public}@",
|
||||
filename,
|
||||
id
|
||||
)
|
||||
|
||||
task.resume()
|
||||
} else {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: Task not created to upload file %{public}@ for identifier=%{public}@",
|
||||
filename,
|
||||
id
|
||||
)
|
||||
}
|
||||
} else {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: The file %{public}@ for identifier=%{public}@ could not be processed for upload",
|
||||
filename,
|
||||
id
|
||||
)
|
||||
return "The file \(filename) could not be processed for upload"
|
||||
}
|
||||
} else {
|
||||
os_log(
|
||||
OSLogType.default,
|
||||
"Mattermost BackgroundSession: File %{public}@ for identifier=%{public}@ not found or is not a valid URL",
|
||||
file,
|
||||
id
|
||||
)
|
||||
return "File not found \(file)"
|
||||
}
|
||||
}
|
||||
completionHandler()
|
||||
|
||||
Reference in New Issue
Block a user