From b633fa1e0f02114fa5d7f0dc2d40f5e4dea426a6 Mon Sep 17 00:00:00 2001 From: Donny Wals Date: Mon, 2 Sep 2024 14:50:46 +0200 Subject: [PATCH] Fixed a crash that made large uploads consume way too much memory --- Sources/TUSKit/Tasks/UploadDataTask.swift | 12 +++++++----- .../TUSKitExample.xcodeproj/project.pbxproj | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Sources/TUSKit/Tasks/UploadDataTask.swift b/Sources/TUSKit/Tasks/UploadDataTask.swift index 771729f2..7d24ffeb 100644 --- a/Sources/TUSKit/Tasks/UploadDataTask.swift +++ b/Sources/TUSKit/Tasks/UploadDataTask.swift @@ -76,10 +76,12 @@ final class UploadDataTask: NSObject, IdentifiableTask { return } - let dataToUpload: Data + let dataSize: Int let file: URL do { - dataToUpload = try loadData() + let attr = try FileManager.default.attributesOfItem(atPath: metaData.filePath.path) + dataSize = attr[FileAttributeKey.size] as! Int + file = try prepareUploadFile() } catch let error { let tusError = TUSClientError.couldNotLoadData(underlyingError: error) @@ -104,7 +106,7 @@ final class UploadDataTask: NSObject, IdentifiableTask { sessionTask = task if #available(iOS 11.0, macOS 10.13, *) { - observeTask(task: task, size: dataToUpload.count) + observeTask(task: task, size: dataSize) } } @@ -190,8 +192,8 @@ final class UploadDataTask: NSObject, IdentifiableTask { Note that compiler and api says that readToEnd is available on macOS 10.15.4 and higher, but yet github actions of 10.15.7 fails to find the member. return try fileHandle.readToEnd() */ - } else { // No range, older versions - data = fileHandle.readDataToEndOfFile() + } else { // No range, we're uploading the file in full so no need to read / recopy + return metaData.filePath } return try files.store(data: data, id: metaData.id, preferredFileExtension: "uploadData") diff --git a/TUSKitExample/TUSKitExample.xcodeproj/project.pbxproj b/TUSKitExample/TUSKitExample.xcodeproj/project.pbxproj index d2766f9f..aa185604 100644 --- a/TUSKitExample/TUSKitExample.xcodeproj/project.pbxproj +++ b/TUSKitExample/TUSKitExample.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 52; + objectVersion = 54; objects = { /* Begin PBXBuildFile section */ @@ -545,7 +545,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"TUSKitExample/Preview Content\""; - DEVELOPMENT_TEAM = G43DLT797F; + DEVELOPMENT_TEAM = 4JMM8JMG3H; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = TUSKitExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.1; @@ -567,7 +567,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; DEVELOPMENT_ASSET_PATHS = "\"TUSKitExample/Preview Content\""; - DEVELOPMENT_TEAM = G43DLT797F; + DEVELOPMENT_TEAM = 4JMM8JMG3H; ENABLE_PREVIEWS = YES; INFOPLIST_FILE = TUSKitExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 14.1;