]> git.r.bdr.sh - rbdr/captura/blob - Captura/Data/CapturaURLDecoder.swift
Remove SwiftData references
[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 var skipBackend: Bool { get }
29 var maxLength: Int? { get }
30 }
31
32 // The concrete implementations
33 struct ConfigureAction: ConfigureActionProtocol {
34 let action: String
35 var fps: Int?
36 var outputs: OutputFormatSetting?
37 var backend: URL?
38 var backendOutput: OutputFormatSetting?
39 var keepLocalFiles: Bool?
40 }
41
42 struct RecordAction: RecordActionProtocol {
43 let action: String
44 var x: Int?
45 var y: Int?
46 var width: Int?
47 var height: Int?
48 var preventResize: Bool?
49 var preventMove: Bool?
50 var fps: Int?
51 var outputs: OutputFormatSetting?
52 var backend: URL?
53 var backendOutput: OutputFormatSetting?
54 var keepLocalFiles: Bool?
55 var autoStart: Bool?
56 var skipBackend: Bool
57 var maxLength: Int?
58 }
59
60 enum CapturaAction {
61 case record(RecordAction)
62 case configure(ConfigureAction)
63 }
64
65 struct CapturaURLDecoder {
66
67 static func decodeParams(url: URL) -> CapturaAction? {
68 guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
69 let params = components.queryItems else {
70 return nil
71 }
72
73 var paramsDict = [String: Any]()
74
75 params.forEach { item in
76 paramsDict[item.name] = item.value
77 }
78
79 guard let action = paramsDict["action"] as? String else {
80 return nil
81 }
82
83 switch action {
84 case "configure":
85 var fps = Int(paramsDict["fps"] as? String ?? "")
86 let backend = URL(string: paramsDict["backend"] as? String ?? "")
87 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
88 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
89 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
90
91 if fps != nil {
92 fps = min(10, max(4, fps!))
93 }
94
95 if backendOutput == .all {
96 backendOutput = .gifOnly
97 }
98
99 return .configure(ConfigureAction(
100 action: action,
101 fps: fps,
102 outputs: outputs,
103 backend: backend,
104 backendOutput: backendOutput,
105 keepLocalFiles: keepLocalFiles
106 ))
107
108 case "record":
109 let x = Int(paramsDict["x"] as? String ?? "")
110 let y = Int(paramsDict["y"] as? String ?? "")
111 let width = Int(paramsDict["width"] as? String ?? "")
112 let height = Int(paramsDict["height"] as? String ?? "")
113 let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
114 let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
115 var fps = Int(paramsDict["fps"] as? String ?? "")
116 let backend = URL(string: paramsDict["backend"] as? String ?? "")
117 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
118 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
119 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
120 let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
121 var maxLength = Int(paramsDict["max_length"] as? String ?? "")
122
123 if fps != nil {
124 fps = min(10, max(4, fps!))
125 }
126
127 if maxLength != nil {
128 maxLength = min(300, max(1, fps!))
129 }
130
131 if backendOutput == .all {
132 backendOutput = .gifOnly
133 }
134
135 var skipBackend = false
136 if let backendString = paramsDict["backend"] as? String {
137 if backendString == "" {
138 skipBackend = true
139 }
140 }
141
142 return .record(RecordAction(
143 action: action,
144 x: x,
145 y: y,
146 width: width,
147 height: height,
148 preventResize: preventResize,
149 preventMove: preventMove,
150 fps: fps,
151 outputs: outputs,
152 backend: backend,
153 backendOutput: backendOutput,
154 keepLocalFiles: keepLocalFiles,
155 autoStart: autoStart,
156 skipBackend: skipBackend,
157 maxLength: maxLength
158 ))
159
160 default:
161 return nil
162 }
163 }
164 }