2022-12-30 18:20:54 +01:00
|
|
|
//
|
|
|
|
// https://mczachurski.dev
|
2023-04-09 20:51:33 +02:00
|
|
|
// Copyright © 2023 Marcin Czachurski and the repository contributors.
|
2023-03-28 10:35:38 +02:00
|
|
|
// Licensed under the Apache License 2.0.
|
2022-12-30 18:20:54 +01:00
|
|
|
//
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2022-12-30 18:20:54 +01:00
|
|
|
import Foundation
|
2023-02-19 10:32:38 +01:00
|
|
|
import PixelfedKit
|
2023-01-24 20:38:21 +01:00
|
|
|
import Nuke
|
2022-12-30 18:20:54 +01:00
|
|
|
|
|
|
|
public class RemoteFileService {
|
|
|
|
public static let shared = RemoteFileService()
|
2023-01-05 11:55:20 +01:00
|
|
|
private init() { }
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2022-12-30 18:20:54 +01:00
|
|
|
public func fetchData(url: URL) async throws -> Data? {
|
2023-01-26 20:35:24 +01:00
|
|
|
let request = ImageRequest(
|
|
|
|
url: url,
|
|
|
|
priority: .high
|
|
|
|
)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-26 20:35:24 +01:00
|
|
|
let (data, response) = try await ImagePipeline.shared.data(for: request)
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-24 20:38:21 +01:00
|
|
|
guard let response else {
|
|
|
|
return data
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-01-08 17:56:07 +01:00
|
|
|
guard (response as? HTTPURLResponse)?.status?.responseType == .success else {
|
|
|
|
throw NetworkError.notSuccessResponse(response)
|
2022-12-30 18:20:54 +01:00
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2022-12-30 18:20:54 +01:00
|
|
|
return data
|
|
|
|
}
|
|
|
|
}
|