Skip to content

Commit b8a73e2

Browse files
author
Erik Gomez
committed
add gzip decompression logic
1 parent f938ea1 commit b8a73e2

File tree

1 file changed

+56
-3
lines changed

1 file changed

+56
-3
lines changed

Nudge/3rd Party Assets/sofa.swift

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,39 @@ extension MacOSDataFeed {
222222
}
223223

224224
class SOFA: NSObject, URLSessionDelegate {
225+
func decompressGzip(data: Data) -> Data? {
226+
let tempDir = FileManager.default.temporaryDirectory
227+
let compressedFileURL = tempDir.appendingPathComponent(UUID().uuidString)
228+
let decompressedFileURL = tempDir.appendingPathComponent(UUID().uuidString)
229+
230+
do {
231+
// Write compressed data to a temporary file
232+
try data.write(to: compressedFileURL)
233+
234+
// Use gzip to decompress the file
235+
let result = SubProcessUtilities().runProcess(launchPath: "/usr/bin/gzip", arguments: ["--decompress", "--to-stdout", compressedFileURL.path])
236+
237+
// Clean up temporary files
238+
try FileManager.default.removeItem(at: compressedFileURL)
239+
240+
if result.exitCode != 0 {
241+
LogManager.error("gzip failed with status \(result.exitCode): \(result.error)", logger: utilsLog)
242+
return nil
243+
}
244+
245+
// Convert the output string back to Data
246+
guard let decompressedData = result.output.data(using: .utf8) else {
247+
LogManager.error("Failed to convert decompressed output to Data", logger: utilsLog)
248+
return nil
249+
}
250+
251+
return decompressedData
252+
} catch {
253+
LogManager.error("Failed to decompress gzip data using command: \(error.localizedDescription)", logger: utilsLog)
254+
return nil
255+
}
256+
}
257+
225258
func URLSync(url: URL, maxRetries: Int = 3) -> (data: Data?, response: URLResponse?, error: Error?, responseCode: Int?, eTag: String?) {
226259
let semaphore = DispatchSemaphore(value: 0)
227260
let lastEtag = Globals.nudgeDefaults.string(forKey: "LastEtag") ?? ""
@@ -245,9 +278,10 @@ class SOFA: NSObject, URLSessionDelegate {
245278
// Retry loop
246279
while attempts < maxRetries {
247280
attempts += 1
248-
let task = session.dataTask(with: request) { data, resp, error in
281+
let task = session.dataTask(with: request) { [weak self] data, resp, error in
282+
guard let self = self else { return } // To handle self being nil
249283
guard let httpResponse = resp as? HTTPURLResponse else {
250-
print("Error receiving response: \(error?.localizedDescription ?? "No error information")")
284+
LogManager.error("Error receiving response: \(error?.localizedDescription ?? "No error information")", logger: utilsLog)
251285
semaphore.signal()
252286
return
253287
}
@@ -258,11 +292,30 @@ class SOFA: NSObject, URLSessionDelegate {
258292
eTag = etag
259293
}
260294
successfulQuery = true
295+
296+
if let encoding = httpResponse.allHeaderFields["Content-Encoding"] as? String {
297+
LogManager.debug("Content-Encoding: \(encoding)", logger: utilsLog)
298+
299+
if encoding == "gzip", let compressedData = data {
300+
responseData = self.decompressGzip(data: compressedData)
301+
302+
if responseData == nil {
303+
LogManager.error("Failed to decompress gzip data", logger: utilsLog)
304+
responseData = data // Fall back to using the original data
305+
} else {
306+
LogManager.debug("Successfully decompressed gzip data", logger: utilsLog)
307+
}
308+
} else {
309+
responseData = data
310+
}
311+
} else {
312+
responseData = data
313+
}
314+
261315
} else if responseCode == 304 {
262316
successfulQuery = true
263317
}
264318

265-
responseData = data
266319
response = resp
267320
responseError = error
268321
semaphore.signal()

0 commit comments

Comments
 (0)