Skip to content

Commit

Permalink
swift5: upload in background
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibaultBee authored Nov 27, 2023
1 parent 435cf28 commit 0be8d39
Show file tree
Hide file tree
Showing 17 changed files with 850 additions and 584 deletions.
4 changes: 1 addition & 3 deletions .openapi-generator/FILES
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ Sources/APIHelper.swift
Sources/APIs.swift
Sources/APIs/AdvancedAuthenticationAPI.swift
Sources/APIs/VideosAPI.swift
Sources/AlamofireImplementations.swift
Sources/Auth/ApiVideoAuthenticator.swift
Sources/Auth/ApiVideoCredential.swift
Sources/CodableHelper.swift
Sources/Configuration.swift
Sources/Extensions.swift
Expand All @@ -31,6 +28,7 @@ Sources/Models/VideoSourceLiveStream.swift
Sources/Models/VideoSourceLiveStreamLink.swift
Sources/OpenISO8601DateFormatter.swift
Sources/SynchronizedDictionary.swift
Sources/URLSessionImplementations.swift
Sources/Upload/FileChunkInputStream.swift
Sources/Upload/ProgressiveUploadSessionProtocol.swift
Sources/Upload/RequestTaskQueue.swift
Expand Down
7 changes: 3 additions & 4 deletions ApiVideoUploader.podspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Pod::Spec.new do |s|
s.name = 'ApiVideoUploader'
s.ios.deployment_target = '10.0'
s.osx.deployment_target = '10.12'
s.tvos.deployment_target = '10.0'
s.ios.deployment_target = '9.0'
s.osx.deployment_target = '10.11'
s.tvos.deployment_target = '9.0'
# Add back when CocoaPods/CocoaPods#11558 is released
#s.watchos.deployment_target = '3.0'
s.version = '1.2.2'
Expand All @@ -13,5 +13,4 @@ Pod::Spec.new do |s|
s.summary = 'The official Swift api.video uploader for iOS, macOS and tvOS'
s.source_files = 'Sources/**/*.swift'
s.dependency 'AnyCodable-FlightSchool', '~> 0.6.1'
s.dependency 'Alamofire', '~> 5.4.3'
end
1 change: 0 additions & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
github "Flight-School/AnyCodable" ~> 0.6.1
github "Alamofire/Alamofire" ~> 5.4.3
9 changes: 4 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import PackageDescription
let package = Package(
name: "ApiVideoUploader",
platforms: [
.iOS(.v10),
.macOS(.v10_12),
.tvOS(.v10),
.iOS(.v9),
.macOS(.v10_11),
.tvOS(.v9),
.watchOS(.v3),
],
products: [
Expand All @@ -20,14 +20,13 @@ let package = Package(
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/Flight-School/AnyCodable", from: "0.6.1"),
.package(url: "https://github.com/Alamofire/Alamofire", from: "5.4.3"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "ApiVideoUploader",
dependencies: ["AnyCodable", "Alamofire", ],
dependencies: ["AnyCodable", ],
path: "Sources"
),
// Targets for tests
Expand Down
47 changes: 36 additions & 11 deletions Sources/APIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,33 @@

import Foundation
public class ApiVideoUploader {
public static var apiKey: String? = nil
private static var apiKey: String? = nil
public static var basePath = "https://ws.api.video"
internal static var customHeaders:[String: String] = ["AV-Origin-Client": "swift-uploader:1.2.2"]
internal static var defaultHeaders:[String: String] = ["AV-Origin-Client": "swift-uploader:1.2.2"]
internal static var credential: URLCredential?
private static var chunkSize: Int = 50 * 1024 * 1024
internal static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
internal static var credential = ApiVideoCredential()
internal static var requestBuilderFactory: RequestBuilderFactory = URLSessionRequestBuilderFactory()
public static var apiResponseQueue: DispatchQueue = .main

public static var backgroundIdentifier: String = "video.api.upload.background"
public static var timeout: TimeInterval = 60
internal static var customHeaders:[String: String] {
var headers = defaultHeaders
if let apiKey = apiKey {
headers["Authorization"] = apiKey
}
return headers
}

public static func setApiKey(_ apiKey: String?) {
if let apiKey = apiKey {
self.apiKey = "Basic " + "\(apiKey):".toBase64()
} else {
self.apiKey = nil
}
}

public static func setChunkSize(chunkSize: Int) throws {
public static func setChunkSize(_ chunkSize: Int) throws {
if (chunkSize > 128 * 1024 * 1024) {
throw ParameterError.outOfRange
} else if (chunkSize < 5 * 1024 * 1024) {
Expand All @@ -40,25 +57,25 @@ public class ApiVideoUploader {
}
}

static func isValidVersion(version: String) -> Bool {
static func isValidVersion(_ version: String) -> Bool {
let pattern = #"^\d{1,3}(\.\d{1,3}(\.\d{1,3})?)?$"#
return isValid(pattern: pattern, field: version)
}

static func isValidName(name: String) -> Bool {
static func isValidName(_ name: String) -> Bool {
let pattern = #"^[\w\-]{1,50}$"#
return isValid(pattern: pattern, field: name)
}

static func setName(key: String, name: String, version: String) throws {
if(!isValidName(name: name)) {
if(!isValidName(name)) {
throw ParameterError.invalidName
}

if(!isValidVersion(version: version)) {
if(!isValidVersion(version)) {
throw ParameterError.invalidVersion
}
ApiVideoUploader.customHeaders[key] = name + ":" + version
ApiVideoUploader.defaultHeaders[key] = name + ":" + version
}

public static func setSdkName(name: String, version: String) throws {
Expand All @@ -68,17 +85,19 @@ public class ApiVideoUploader {
public static func setApplicationName(name: String, version: String) throws {
try setName(key: "AV-Origin-App", name: name, version: version)
}

}

open class RequestBuilder<T> {
var credential: URLCredential?
var headers: [String: String]
public var parameters: [String: Any]?
public let method: String
public let URLString: String
public let requestTask: RequestTask = RequestTask()

/// Optional block to obtain a reference to the request's progress instance when available.
/// With the URLSession http client the request's progress only works on iOS 11.0, macOS 10.13, macCatalyst 13.0, tvOS 11.0, watchOS 4.0.
/// If you need to get the request's progress in older OS versions, please use Alamofire http client.
public var onProgressReady: ((Progress) -> Void)?

required public init(method: String, URLString: String, parameters: [String: Any]?, headers: [String: String] = [:], onProgressReady: ((Progress) -> Void)? = nil) {
Expand Down Expand Up @@ -108,9 +127,15 @@ open class RequestBuilder<T> {
}
return self
}

open func addCredential() -> Self {
credential = ApiVideoUploader.credential
return self
}
}

public protocol RequestBuilderFactory {
func getNonDecodableBuilder<T>() -> RequestBuilder<T>.Type
func getBuilder<T: Decodable>() -> RequestBuilder<T>.Type
func getBackgroundBuilder<T: Decodable>() -> RequestBuilder<T>.Type
}
8 changes: 4 additions & 4 deletions Sources/APIs/VideosAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ The latter allows you to split a video source into X chunks and send those chunk

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)

let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()

return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
}
Expand Down Expand Up @@ -202,7 +202,7 @@ The latter allows you to split a video source into X chunks and send those chunk

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)

let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()

return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ The latter allows you to split a video source into X chunks and send those chunk

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)

let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()

return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
}
Expand Down Expand Up @@ -392,7 +392,7 @@ The latter allows you to split a video source into X chunks and send those chunk

let localVariableHeaderParameters = APIHelper.rejectNilHeaders(localVariableNillableHeaders)

let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBuilder()
let localVariableRequestBuilder: RequestBuilder<Video>.Type = ApiVideoUploader.requestBuilderFactory.getBackgroundBuilder()

return localVariableRequestBuilder.init(method: "POST", URLString: (localVariableUrlComponents?.string ?? localVariableURLString), parameters: localVariableParameters, headers: localVariableHeaderParameters, onProgressReady: onProgressReady)
}
Expand Down
Loading

0 comments on commit 0be8d39

Please sign in to comment.