aboutsummaryrefslogtreecommitdiff
path: root/Hotline/Models
diff options
context:
space:
mode:
authorDustin Mierau <dustin@mierau.me>2024-01-14 19:52:32 -0800
committerDustin Mierau <dustin@mierau.me>2024-01-14 19:52:32 -0800
commitfffb072dcd736a061186f44511d2a10cb41e8b42 (patch)
treebd856778e6cd9710d20d2889b904ff3ba5a33404 /Hotline/Models
parentff7445f1c273a79d0da70845073b58e30680c778 (diff)
Add support for downloading files with resource forks! Also properly set HFS creator code and type on downloaded files when specified by the server.
Diffstat (limited to 'Hotline/Models')
-rw-r--r--Hotline/Models/Hotline.swift37
-rw-r--r--Hotline/Models/TransferInfo.swift1
2 files changed, 38 insertions, 0 deletions
diff --git a/Hotline/Models/Hotline.swift b/Hotline/Models/Hotline.swift
index f15bbb6..814ade5 100644
--- a/Hotline/Models/Hotline.swift
+++ b/Hotline/Models/Hotline.swift
@@ -473,6 +473,42 @@ final class Hotline: HotlineClientDelegate, HotlineFileClientDelegate {
}
}
+ @MainActor func downloadFileTo(url fileURL: URL, fileName: String, path: [String], progress progressCallback: ((TransferInfo, Double) -> Void)? = nil, complete callback: ((TransferInfo, URL) -> Void)? = nil) {
+ var fullPath: [String] = []
+ if path.count > 1 {
+ fullPath = Array(path[0..<path.count-1])
+ }
+
+ self.client.sendDownloadFile(name: fileName, path: fullPath) { [weak self] success, downloadReferenceNumber, downloadTransferSize, downloadFileSize, downloadWaitingCount in
+ print("GOT DOWNLOAD REPLY:")
+ print("\tSUCCESS?", success)
+ print("\tTRANSFER SIZE: \(downloadTransferSize.debugDescription)")
+ print("\tFILE SIZE: \(downloadFileSize.debugDescription)")
+ print("\tREFERENCE NUM: \(downloadReferenceNumber.debugDescription)")
+ print("\tWAITING COUNT: \(downloadWaitingCount.debugDescription)")
+
+ if
+ let self = self,
+ let address = self.server?.address,
+ let port = self.server?.port,
+ let referenceNumber = downloadReferenceNumber,
+ let transferSize = downloadTransferSize {
+
+ print("DOWNLOADING TO MEMORY")
+ let fileClient = HotlineFileClient(address: address, port: UInt16(port), reference: referenceNumber, size: UInt32(transferSize), type: .file)
+ fileClient.delegate = self
+ self.downloads.append(fileClient)
+
+ let transfer = TransferInfo(id: referenceNumber, title: fileName, size: UInt(transferSize))
+ transfer.downloadCallback = callback
+ transfer.progressCallback = progressCallback
+ self.transfers.append(transfer)
+
+ fileClient.downloadToFile(to: fileURL)
+ }
+ }
+ }
+
@MainActor func previewFile(_ fileName: String, path: [String], complete callback: ((PreviewFileInfo?) -> Void)? = nil) {
var fullPath: [String] = []
if path.count > 1 {
@@ -748,6 +784,7 @@ final class Hotline: HotlineClientDelegate, HotlineFileClientDelegate {
if let transfer = self.transfers.first(where: { $0.id == reference }) {
transfer.progress = progress
transfer.timeRemaining = timeRemaining
+ transfer.progressCallback?(transfer, progress)
}
case .failed(_):
if let i = self.downloads.firstIndex(where: { $0.referenceNumber == reference }) {
diff --git a/Hotline/Models/TransferInfo.swift b/Hotline/Models/TransferInfo.swift
index 546efd3..8b8d97a 100644
--- a/Hotline/Models/TransferInfo.swift
+++ b/Hotline/Models/TransferInfo.swift
@@ -14,6 +14,7 @@ class TransferInfo: Identifiable, Equatable, Hashable {
// For file based transfers (i.e. not previews)
var fileURL: URL? = nil
+ var progressCallback: ((TransferInfo, Double) -> Void)? = nil
var downloadCallback: ((TransferInfo, URL) -> Void)? = nil
var previewCallback: ((TransferInfo, Data) -> Void)? = nil