]> git.r.bdr.sh - rbdr/captura/blob - Captura/Data/CapturaURLDecoder.swift
3fa6412849a626fcef87e0485706a42fed9a7ac0
[rbdr/captura] / Captura / Data / CapturaURLDecoder.swift
1 import Foundation
2
3
4 protocol ConfigureActionProtocol {
5 var action: String { get }
6 var fps: Int? { get }
7 var outputs: OutputFormatSetting? { get }
8 var backend: URL? { get }
9 var backendOutput: OutputFormatSetting? { get }
10 var keepLocalFiles: Bool? { get }
11 }
12
13 protocol RecordActionProtocol {
14 var action: String { get }
15
16 var x: Int? { get }
17 var y: Int? { get }
18 var width: Int? { get }
19 var height: Int? { get }
20 var preventResize: Bool? { get }
21 var preventMove: Bool? { get }
22 var fps: Int? { get }
23 var backend: URL? { get }
24 var outputs: OutputFormatSetting? { get }
25 var backendOutput: OutputFormatSetting? { get }
26 var keepLocalFiles: Bool? { get }
27 var autoStart: Bool? { get }
28 }
29
30 // The concrete implementations
31 struct ConfigureAction: ConfigureActionProtocol {
32 let action: String
33 var fps: Int?
34 var outputs: OutputFormatSetting?
35 var backend: URL?
36 var backendOutput: OutputFormatSetting?
37 var keepLocalFiles: Bool?
38 }
39
40 struct RecordAction: RecordActionProtocol {
41 let action: String
42 var x: Int?
43 var y: Int?
44 var width: Int?
45 var height: Int?
46 var preventResize: Bool?
47 var preventMove: Bool?
48 var fps: Int?
49 var outputs: OutputFormatSetting?
50 var backend: URL?
51 var backendOutput: OutputFormatSetting?
52 var keepLocalFiles: Bool?
53 var autoStart: Bool?
54 }
55
56 enum CapturaAction {
57 case record(RecordAction)
58 case configure(ConfigureAction)
59 }
60
61 struct CapturaURLDecoder {
62
63 static func decodeParams(url: URL) -> CapturaAction? {
64 guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
65 let params = components.queryItems else {
66 return nil
67 }
68
69 var paramsDict = [String: Any]()
70
71 params.forEach { item in
72 paramsDict[item.name] = item.value
73 }
74
75 guard let action = paramsDict["action"] as? String else {
76 return nil
77 }
78
79 switch action {
80 case "configure":
81 var fps = Int(paramsDict["fps"] as? String ?? "")
82 let backend = URL(string: paramsDict["backend"] as? String ?? "")
83 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
84 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
85 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
86
87 if fps != nil {
88 fps = min(10, max(4, fps!))
89 }
90
91 if backendOutput == .all {
92 backendOutput = .gifOnly
93 }
94
95 return .configure(ConfigureAction(
96 action: action,
97 fps: fps,
98 outputs: outputs,
99 backend: backend,
100 backendOutput: backendOutput,
101 keepLocalFiles: keepLocalFiles
102 ))
103
104 case "record":
105 let x = Int(paramsDict["x"] as? String ?? "")
106 let y = Int(paramsDict["y"] as? String ?? "")
107 let width = Int(paramsDict["width"] as? String ?? "")
108 let height = Int(paramsDict["height"] as? String ?? "")
109 let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
110 let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
111 var fps = Int(paramsDict["fps"] as? String ?? "")
112 let backend = URL(string: paramsDict["backend"] as? String ?? "")
113 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
114 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
115 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
116
117 if fps != nil {
118 fps = min(10, max(4, fps!))
119 }
120
121 if backendOutput == .all {
122 backendOutput = .gifOnly
123 }
124
125 return .record(RecordAction(
126 action: action,
127 x: x,
128 y: y,
129 width: width,
130 height: height,
131 preventResize: preventResize,
132 preventMove: preventMove,
133 fps: fps,
134 outputs: outputs,
135 backend: backend,
136 backendOutput: backendOutput,
137 keepLocalFiles: keepLocalFiles
138 ))
139
140 default:
141 return nil
142 }
143 }
144 }