Skip to content

Commit

Permalink
fix compatibility with ios 14, macos 11, and swift 6
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxfrazer committed Jun 19, 2022
1 parent a3e1dde commit 8991f33
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 29 deletions.
37 changes: 8 additions & 29 deletions Sources/RealityToolkit/RealityToolkit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import OSLog
/// - useCache: Use the local file if it already exists. Default `true`
/// - Returns: Returns the URL the file is downloaded to.
public static func downloadRemoteFile(
contentsOf url: URL, saveTo destination: URL? = nil, useCache: Bool = true) async throws -> URL {
contentsOf url: URL, saveTo destination: URL? = nil, useCache: Bool = true
) async throws -> URL {
if !url.absoluteString.hasPrefix("http") {
RealityToolkit.RUIPrint("URL is already local. To move the file, call `FileManager.default.moveItem`")
if FileManager.default.fileExists(atPath: url.path) { return url }
Expand All @@ -56,37 +57,15 @@ import OSLog
throw LoadRemoteError.cannotDelete
}
}
var moveFrom: URL!
if #available(iOS 15.0, macOS 12.0, *) {
(moveFrom, _) = try await URLSession.shared.download(from: url)
let (moveFrom, _) = try await URLSession.shared.download(from: url)
try FileManager.default.moveItem(
atPath: moveFrom.path, toPath: endLocation.path
)
} else {
let req = URLRequest(url: url)
var throwableErr: Error?
let semaphore = DispatchSemaphore(value: 0)
let task = URLSession.shared.downloadTask(with: req) { downloadURL, _, err in
defer { semaphore.signal() }
if let err = err {
RealityToolkit.RUIPrint("Could not download item: \(err)")
throwableErr = err
return
}
moveFrom = downloadURL
}
task.resume()
semaphore.wait()
if let throwableErr = throwableErr {
throw throwableErr
}
if moveFrom == nil {
throw LoadRemoteError.downloadError
}
let data = try Data(contentsOf: url)
try data.write(to: endLocation)
}

// let (url, _) = try await URLSession.shared.download(from: url)
try FileManager.default.moveItem(
atPath: moveFrom.path, toPath: endLocation.path
)

return endLocation
}
}
3 changes: 3 additions & 0 deletions Tests/RealityToolkitTests/DownloadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ final class Download_Tests: XCTestCase {
} catch let err as URLError {
XCTAssertTrue(err.errorCode == URLError.Code.cannotFindHost.rawValue, "Wrong kind of error coming back")
return
} catch let err as CocoaError {
XCTAssertEqual(err.errorCode, CocoaError.Code.fileReadUnknown.rawValue, "Wrong kind of error coming back")
return
}
XCTFail("The download should have failed")
}
Expand Down

0 comments on commit 8991f33

Please sign in to comment.