Skip to content

Commit 90a189d

Browse files
committed
Add print statements for visibility (remove when PR is finalized)
1 parent c52a62e commit 90a189d

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

Sources/Hub/Downloader.swift

+44-5
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,18 @@ class Downloader: NSObject, ObservableObject {
8989
timeout: TimeInterval,
9090
numRetries: Int
9191
) {
92+
print("[Downloader] Setting up download for \(url.lastPathComponent)")
93+
print("[Downloader] Destination: \(destination.path)")
94+
print("[Downloader] Temp file: \(tempFilePath?.path ?? "none")")
95+
9296
// If we have an expected size and resumeSize, calculate initial progress
9397
if let expectedSize = expectedSize, expectedSize > 0 && resumeSize > 0 {
9498
let initialProgress = Double(resumeSize) / Double(expectedSize)
9599
downloadState.value = .downloading(initialProgress)
100+
print("[Downloader] Resuming from \(resumeSize)/\(expectedSize) bytes (\(Int(initialProgress * 100))%)")
96101
} else {
97102
downloadState.value = .downloading(0)
103+
print("[Downloader] Starting new download")
98104
}
99105

100106
urlSession?.getAllTasks { tasks in
@@ -148,6 +154,9 @@ class Downloader: NSObject, ObservableObject {
148154
// If the reported resumeSize doesn't match the file size, trust the file size
149155
if existingSize != resumeSize {
150156
self.downloadedSize = existingSize
157+
print("[Downloader] Found existing temp file with \(existingSize) bytes (different from resumeSize: \(resumeSize))")
158+
} else {
159+
print("[Downloader] Found existing temp file with \(existingSize) bytes")
151160
}
152161
} else {
153162
// Create new temp file with predictable path for future resume
@@ -160,6 +169,7 @@ class Downloader: NSObject, ObservableObject {
160169
})
161170
let hashedName = "\(filename)-\(stableHash)"
162171
tempURL = FileManager.default.temporaryDirectory.appendingPathComponent(hashedName)
172+
print("[Downloader] Creating new temp file at \(tempURL.path)")
163173
FileManager.default.createFile(atPath: tempURL.path, contents: nil)
164174
}
165175

@@ -168,6 +178,7 @@ class Downloader: NSObject, ObservableObject {
168178

169179
// If we're resuming, seek to end of file first
170180
if existingSize > 0 {
181+
print("[Downloader] Seeking to end of existing file (\(existingSize) bytes)")
171182
try tempFile.seekToEnd()
172183
}
173184

@@ -176,12 +187,16 @@ class Downloader: NSObject, ObservableObject {
176187

177188
// Clean up and move the completed download to its final destination
178189
tempFile.closeFile()
190+
print("[Downloader] Download completed with total size \(self.downloadedSize) bytes")
191+
print("[Downloader] Moving temp file to destination: \(self.destination.path)")
179192
try FileManager.default.moveDownloadedFile(from: tempURL, to: self.destination)
180193

181194
// Clear temp file reference since it's been moved
182195
self.tempFilePath = nil
196+
print("[Downloader] Download successfully completed")
183197
self.downloadState.value = .completed(self.destination)
184198
} catch {
199+
print("[Downloader] Error: \(error)")
185200
self.downloadState.value = .failed(error)
186201
}
187202
}
@@ -214,16 +229,27 @@ class Downloader: NSObject, ObservableObject {
214229
var newRequest = request
215230
if resumeSize > 0 {
216231
newRequest.setValue("bytes=\(resumeSize)-", forHTTPHeaderField: "Range")
232+
print("[Downloader] Adding Range header: bytes=\(resumeSize)-")
217233
}
218234

219235
// Start the download and get the byte stream
220236
let (asyncBytes, response) = try await session.bytes(for: newRequest)
221237

222-
guard let response = response as? HTTPURLResponse else {
238+
guard let httpResponse = response as? HTTPURLResponse else {
239+
print("[Downloader] Error: Non-HTTP response received")
223240
throw DownloadError.unexpectedError
224241
}
242+
243+
print("[Downloader] Received HTTP \(httpResponse.statusCode) response")
244+
if let contentRange = httpResponse.value(forHTTPHeaderField: "Content-Range") {
245+
print("[Downloader] Content-Range: \(contentRange)")
246+
}
247+
if let contentLength = httpResponse.value(forHTTPHeaderField: "Content-Length") {
248+
print("[Downloader] Content-Length: \(contentLength)")
249+
}
225250

226-
guard (200..<300).contains(response.statusCode) else {
251+
guard (200..<300).contains(httpResponse.statusCode) else {
252+
print("[Downloader] Error: HTTP status code \(httpResponse.statusCode)")
227253
throw DownloadError.unexpectedError
228254
}
229255

@@ -277,7 +303,10 @@ class Downloader: NSObject, ObservableObject {
277303
// Verify the downloaded file size matches the expected size
278304
let actualSize = try tempFile.seekToEnd()
279305
if let expectedSize = expectedSize, expectedSize != actualSize {
306+
print("[Downloader] Error: Size mismatch - expected \(expectedSize) bytes but got \(actualSize) bytes")
280307
throw DownloadError.unexpectedError
308+
} else {
309+
print("[Downloader] Final verification passed, size: \(actualSize) bytes")
281310
}
282311
}
283312

@@ -363,6 +392,7 @@ extension Downloader {
363392
/// Persists the current download state to UserDefaults
364393
func persistState() {
365394
guard let tempFilePath = self.tempFilePath else {
395+
print("[Downloader] Cannot persist state: No temp file path")
366396
return // Nothing to persist if no temp file
367397
}
368398

@@ -376,8 +406,9 @@ extension Downloader {
376406
var states = Downloader.getPersistedStates()
377407
states[sourceURL.absoluteString] = data
378408
UserDefaults.standard.set(states, forKey: "SwiftTransformers.ActiveDownloads")
409+
print("[Downloader] Persisted download state for \(sourceURL.lastPathComponent) - \(downloadedSize) bytes downloaded")
379410
} catch {
380-
print("Error persisting download state: \(error)")
411+
print("[Downloader] Error persisting download state: \(error)")
381412
}
382413
}
383414

@@ -386,6 +417,7 @@ extension Downloader {
386417
var states = Downloader.getPersistedStates()
387418
states.removeValue(forKey: sourceURL.absoluteString)
388419
UserDefaults.standard.set(states, forKey: "SwiftTransformers.ActiveDownloads")
420+
print("[Downloader] Removed persisted state for \(sourceURL.lastPathComponent)")
389421
}
390422

391423
/// Get all persisted download states
@@ -398,16 +430,19 @@ extension Downloader {
398430
let states = getPersistedStates()
399431
let decoder = JSONDecoder()
400432

433+
print("[Downloader] Found \(states.count) persisted download states")
401434
var resumedDownloaders: [Downloader] = []
402435

403-
for (_, stateData) in states {
436+
for (url, stateData) in states {
404437
do {
405438
let state = try decoder.decode(PersistableDownloadState.self, from: stateData)
439+
print("[Downloader] Trying to resume download for: \(state.sourceURL.lastPathComponent)")
406440

407441
// Check if temp file still exists
408442
if FileManager.default.fileExists(atPath: state.tempFilePath.path) {
409443
let attributes = try FileManager.default.attributesOfItem(atPath: state.tempFilePath.path)
410444
let fileSize = attributes[.size] as? Int ?? 0
445+
print("[Downloader] Found existing temp file with \(fileSize) bytes")
411446

412447
// Create a new downloader that resumes from the temp file
413448
let downloader = Downloader(
@@ -420,12 +455,16 @@ extension Downloader {
420455
)
421456

422457
resumedDownloaders.append(downloader)
458+
print("[Downloader] Successfully resumed download for \(state.sourceURL.lastPathComponent)")
459+
} else {
460+
print("[Downloader] Temp file not found at \(state.tempFilePath.path)")
423461
}
424462
} catch {
425-
print("Error restoring download: \(error)")
463+
print("[Downloader] Error restoring download for \(url): \(error)")
426464
}
427465
}
428466

467+
print("[Downloader] Successfully resumed \(resumedDownloaders.count) downloads")
429468
return resumedDownloaders
430469
}
431470
}

0 commit comments

Comments
 (0)