-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ios): download arkzkey and wasm from server and generate proof
- Loading branch information
1 parent
92e9e5e
commit 7a7c467
Showing
3 changed files
with
300 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// FileDownloader.swift | ||
// MoproKit | ||
// | ||
// Copyright © 2024 CocoaPods. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
class FileDownloader { | ||
|
||
static func loadFileSync(url: URL, completion: @escaping (String?, Error?) -> Void) | ||
{ | ||
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | ||
|
||
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent) | ||
|
||
if FileManager().fileExists(atPath: destinationUrl.path) | ||
{ | ||
print("File already exists [\(destinationUrl.path)]") | ||
completion(destinationUrl.path, nil) | ||
} | ||
else if let dataFromURL = NSData(contentsOf: url) | ||
{ | ||
if dataFromURL.write(to: destinationUrl, atomically: true) | ||
{ | ||
print("file saved [\(destinationUrl.path)]") | ||
completion(destinationUrl.path, nil) | ||
} | ||
else | ||
{ | ||
print("error saving file") | ||
let error = NSError(domain:"Error saving file", code:1001, userInfo:nil) | ||
completion(destinationUrl.path, error) | ||
} | ||
} | ||
else | ||
{ | ||
let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil) | ||
completion(destinationUrl.path, error) | ||
} | ||
} | ||
|
||
static func loadFileAsync(url: URL, completion: @escaping (String?, Error?) -> Void) | ||
{ | ||
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! | ||
|
||
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent) | ||
|
||
if FileManager().fileExists(atPath: destinationUrl.path) | ||
{ | ||
print("File already exists [\(destinationUrl.path)]") | ||
completion(destinationUrl.path, nil) | ||
} | ||
else | ||
{ | ||
let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil) | ||
var request = URLRequest(url: url) | ||
request.httpMethod = "GET" | ||
let task = session.dataTask(with: request, completionHandler: | ||
{ | ||
data, response, error in | ||
if error == nil | ||
{ | ||
if let response = response as? HTTPURLResponse | ||
{ | ||
if response.statusCode == 200 | ||
{ | ||
if let data = data | ||
{ | ||
if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic) | ||
{ | ||
completion(destinationUrl.path, error) | ||
} | ||
else | ||
{ | ||
completion(destinationUrl.path, error) | ||
} | ||
} | ||
else | ||
{ | ||
completion(destinationUrl.path, error) | ||
} | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
completion(destinationUrl.path, error) | ||
} | ||
}) | ||
task.resume() | ||
} | ||
} | ||
} |
Oops, something went wrong.