2019-06-17 14:49:23 -07:00
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// FetchSourceOperation.swift
|
2019-06-17 14:49:23 -07:00
|
|
|
// AltStore
|
|
|
|
|
//
|
2019-07-30 17:00:04 -07:00
|
|
|
// Created by Riley Testut on 7/30/19.
|
2019-06-17 14:49:23 -07:00
|
|
|
// Copyright © 2019 Riley Testut. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
2020-03-24 13:27:44 -07:00
|
|
|
import CoreData
|
|
|
|
|
|
2020-09-03 16:39:08 -07:00
|
|
|
import AltStoreCore
|
2019-06-17 14:49:23 -07:00
|
|
|
import Roxas
|
|
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
func matches(for regex: String, in text: String) -> [String] {
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
let regex = try NSRegularExpression(pattern: regex)
|
|
|
|
|
let results = regex.matches(in: text,
|
|
|
|
|
range: NSRange(text.startIndex..., in: text))
|
|
|
|
|
return results.map {
|
|
|
|
|
String(text[Range($0.range, in: text)!])
|
|
|
|
|
}
|
|
|
|
|
} catch let error {
|
|
|
|
|
print("invalid regex: \(error.localizedDescription)")
|
|
|
|
|
return []
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func containsRedirect(_ response: URLResponse?, data: Data?) -> String? {
|
|
|
|
|
if let httpResponse = response as? HTTPURLResponse {
|
|
|
|
|
print("Request status code: \(httpResponse.statusCode)")
|
|
|
|
|
print("Request headers: \(httpResponse.allHeaderFields.debugDescription)")
|
|
|
|
|
|
|
|
|
|
guard let data = data else {
|
|
|
|
|
print("Request error: missing data")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
let rawHttp = String(decoding: data, as: UTF8.self)
|
|
|
|
|
let regex = "url=((https|http):\\/\\/[\\S]*)\">"
|
|
|
|
|
guard var redirectURL = matches(for: regex, in: rawHttp).first else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
redirectURL = redirectURL.replacingOccurrences(of: "url=", with: "")
|
|
|
|
|
redirectURL = redirectURL.replacingOccurrences(of: "\">", with: "")
|
|
|
|
|
print("redirectURL: \(redirectURL)")
|
|
|
|
|
return redirectURL
|
|
|
|
|
} else {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-30 17:00:04 -07:00
|
|
|
@objc(FetchSourceOperation)
|
2023-01-04 09:52:12 -05:00
|
|
|
final class FetchSourceOperation: ResultOperation<Source>
|
2019-06-17 14:49:23 -07:00
|
|
|
{
|
2019-07-30 17:00:04 -07:00
|
|
|
let sourceURL: URL
|
2020-03-24 13:27:44 -07:00
|
|
|
let managedObjectContext: NSManagedObjectContext
|
2019-07-30 17:00:04 -07:00
|
|
|
|
2022-11-06 17:49:06 -05:00
|
|
|
private let session: URLSession = AppServices.network.sessionNoCache
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2019-09-07 15:37:08 -07:00
|
|
|
private lazy var dateFormatter: ISO8601DateFormatter = {
|
|
|
|
|
let dateFormatter = ISO8601DateFormatter()
|
2019-06-17 14:49:23 -07:00
|
|
|
return dateFormatter
|
|
|
|
|
}()
|
|
|
|
|
|
2020-03-24 13:27:44 -07:00
|
|
|
init(sourceURL: URL, managedObjectContext: NSManagedObjectContext = DatabaseManager.shared.persistentContainer.newBackgroundContext())
|
2019-07-30 17:00:04 -07:00
|
|
|
{
|
|
|
|
|
self.sourceURL = sourceURL
|
2020-03-24 13:27:44 -07:00
|
|
|
self.managedObjectContext = managedObjectContext
|
2019-07-30 17:00:04 -07:00
|
|
|
}
|
|
|
|
|
|
2019-06-17 14:49:23 -07:00
|
|
|
override func main()
|
|
|
|
|
{
|
|
|
|
|
super.main()
|
2022-11-07 00:12:34 -05:00
|
|
|
loadSource(self.sourceURL)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func loadSource(_ url: URL) {
|
|
|
|
|
let dataTask = createDataTask(with: url)
|
|
|
|
|
self.progress.addChild(dataTask.progress, withPendingUnitCount: 1)
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
dataTask.resume()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func createDataTask(with url: URL) -> URLSessionDataTask {
|
|
|
|
|
let dataTask = self.session.dataTask(with: url) { (data, response, error) in
|
|
|
|
|
// Test code for http redirect HTML, though seems I got jekyll/sidestore to work without this now - @JoeMatt
|
|
|
|
|
if let error = error {
|
|
|
|
|
print("Request error: \(error)")
|
|
|
|
|
// TODO: Handle error
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-08-27 16:23:50 -07:00
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
if let redirect = containsRedirect(response, data: data), let redirectURL = URL(string: redirect) {
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
|
self.loadSource(redirectURL)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
self.processJSON(data: data, response: response, error: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dataTask
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func processJSON(data: Data?, response: URLResponse?, error: Error?) {
|
|
|
|
|
let childContext = DatabaseManager.shared.persistentContainer.newBackgroundContext(withParent: self.managedObjectContext)
|
|
|
|
|
childContext.mergePolicy = NSOverwriteMergePolicy
|
|
|
|
|
childContext.perform {
|
|
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
let (data, _) = try Result((data, response), error).get()
|
|
|
|
|
|
|
|
|
|
let decoder = AltStoreCore.JSONDecoder()
|
|
|
|
|
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
|
|
|
|
|
let container = try decoder.singleValueContainer()
|
|
|
|
|
let text = try container.decode(String.self)
|
2019-06-17 14:49:23 -07:00
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
// Full ISO8601 Format.
|
|
|
|
|
self.dateFormatter.formatOptions = [.withFullDate, .withFullTime, .withTimeZone]
|
|
|
|
|
if let date = self.dateFormatter.date(from: text)
|
|
|
|
|
{
|
|
|
|
|
return date
|
|
|
|
|
}
|
2019-11-04 13:38:54 -08:00
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
// Just date portion of ISO8601.
|
|
|
|
|
self.dateFormatter.formatOptions = [.withFullDate]
|
|
|
|
|
if let date = self.dateFormatter.date(from: text)
|
|
|
|
|
{
|
|
|
|
|
return date
|
|
|
|
|
}
|
2020-08-27 16:23:50 -07:00
|
|
|
|
2022-11-07 00:12:34 -05:00
|
|
|
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Date is in invalid format.")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
decoder.managedObjectContext = childContext
|
|
|
|
|
// Note: This may need to be response.url instead, to handle redirects @JoeMatt
|
2022-11-07 00:16:46 -05:00
|
|
|
decoder.sourceURL = response?.url ?? self.sourceURL
|
2022-11-07 00:12:34 -05:00
|
|
|
|
|
|
|
|
let source = try decoder.decode(Source.self, from: data)
|
|
|
|
|
let identifier = source.identifier
|
|
|
|
|
|
|
|
|
|
try childContext.save()
|
|
|
|
|
|
|
|
|
|
self.managedObjectContext.perform {
|
|
|
|
|
if let source = Source.first(satisfying: NSPredicate(format: "%K == %@", #keyPath(Source.identifier), identifier), in: self.managedObjectContext)
|
|
|
|
|
{
|
|
|
|
|
self.finish(.success(source))
|
2020-08-27 16:23:50 -07:00
|
|
|
}
|
2022-11-07 00:12:34 -05:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
self.finish(.failure(OperationError.noSources))
|
2020-08-27 16:23:50 -07:00
|
|
|
}
|
2019-06-17 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
2022-11-07 00:12:34 -05:00
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
self.managedObjectContext.perform {
|
|
|
|
|
self.finish(.failure(error))
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-06-17 14:49:23 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|