7
7
import Foundation
8
8
enum ApiVideoUploaderError : Error {
9
9
case invalidApplicationName
10
+ case invalidApplicationVersion
11
+ case missingApplicationName
10
12
}
11
13
12
14
public class ApiVideoUploader {
13
- private static let DEFAULT_USER_AGENT = " api.video uploader (iOS; v:0.1.3; ) " ;
14
15
15
16
public static var apiKey : String ? = nil
16
17
public static var basePath = " https://ws.api.video "
17
- internal static var customHeaders : [ String : String ] = [ " User-Agent " : ApiVideoUploader . DEFAULT_USER_AGENT ]
18
+ internal static var customHeaders : [ String : String ] = [ " AV-Origin-Client " : " ios-uploader:0.1.4 " ]
18
19
private static var chunkSize : Int = 50 * 1024 * 1024
19
20
internal static var requestBuilderFactory : RequestBuilderFactory = AlamofireRequestBuilderFactory ( )
20
21
internal static var credential = ApiVideoCredential ( )
@@ -34,16 +35,38 @@ public class ApiVideoUploader {
34
35
return ApiVideoUploader . chunkSize
35
36
}
36
37
37
- public static func setApplicationName( applicationName: String ) throws {
38
- let pattern = #"^[\w\-.\/]{1,50}$"#
38
+
39
+ public static func setApplicationName( applicationName: String , applicationVersion: String ? ) throws {
40
+ if ( applicationName. isEmpty) {
41
+ if ( applicationVersion != nil && !applicationVersion!. isEmpty) {
42
+ throw ApiVideoUploaderError . missingApplicationName
43
+ }
44
+ ApiVideoUploader . customHeaders [ " AV-Origin-App " ] = nil
45
+ return
46
+ }
47
+
48
+ let pattern = #"^[\w\-]{1,50}$"#
39
49
let regex = try ! NSRegularExpression ( pattern: pattern, options: . anchorsMatchLines)
40
50
let stringRange = NSRange ( location: 0 , length: applicationName. utf16. count)
41
51
let matches = regex. matches ( in: applicationName, range: stringRange)
42
52
if ( matches. isEmpty) {
43
53
throw ApiVideoUploaderError . invalidApplicationName
44
54
}
45
- ApiVideoUploader . customHeaders [ " User-Agent " ] = ApiVideoUploader . DEFAULT_USER_AGENT + " " + applicationName
55
+
56
+ if ( applicationVersion != nil && !applicationVersion!. isEmpty) {
57
+ let pattern2 = #"^[\w\-]{1,50}$"#
58
+ let regex2 = try ! NSRegularExpression ( pattern: pattern2, options: . anchorsMatchLines)
59
+ let stringRange2 = NSRange ( location: 0 , length: applicationVersion!. utf16. count)
60
+ let matches2 = regex2. matches ( in: applicationVersion!, range: stringRange2)
61
+ if ( matches2. isEmpty) {
62
+ throw ApiVideoUploaderError . invalidApplicationVersion
63
+ }
64
+ ApiVideoUploader . customHeaders [ " AV-Origin-App " ] = applicationName + " : " + applicationVersion!
65
+ return
66
+ }
67
+ ApiVideoUploader . customHeaders [ " AV-Origin-App " ] = applicationName
46
68
}
69
+
47
70
}
48
71
49
72
open class RequestBuilder < T> {
0 commit comments