]> git.r.bdr.sh - rbdr/captura/blame - Captura/Data/CapturaURLDecoder.swift
Add license and contributing
[rbdr/captura] / Captura / Data / CapturaURLDecoder.swift
CommitLineData
5802c153
RBR
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
ba17de89 8
5802c153
RBR
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://captura.tranquil.systems.
16 */
17import Foundation
ba17de89
RBR
18
19protocol ConfigureActionProtocol {
20 var action: String { get }
21 var fps: Int? { get }
22 var outputs: OutputFormatSetting? { get }
23 var backend: URL? { get }
24 var backendOutput: OutputFormatSetting? { get }
25 var keepLocalFiles: Bool? { get }
26}
27
28protocol RecordActionProtocol {
29 var action: String { get }
30
31 var x: Int? { get }
32 var y: Int? { get }
33 var width: Int? { get }
34 var height: Int? { get }
35 var preventResize: Bool? { get }
36 var preventMove: Bool? { get }
37 var fps: Int? { get }
38 var backend: URL? { get }
39 var outputs: OutputFormatSetting? { get }
40 var backendOutput: OutputFormatSetting? { get }
41 var keepLocalFiles: Bool? { get }
42 var autoStart: Bool? { get }
8e932130
RBR
43 var skipBackend: Bool { get }
44 var maxLength: Int? { get }
ba17de89
RBR
45}
46
47// The concrete implementations
48struct ConfigureAction: ConfigureActionProtocol {
49 let action: String
50 var fps: Int?
51 var outputs: OutputFormatSetting?
52 var backend: URL?
53 var backendOutput: OutputFormatSetting?
54 var keepLocalFiles: Bool?
55}
56
57struct RecordAction: RecordActionProtocol {
58 let action: String
59 var x: Int?
60 var y: Int?
61 var width: Int?
62 var height: Int?
63 var preventResize: Bool?
64 var preventMove: Bool?
65 var fps: Int?
66 var outputs: OutputFormatSetting?
67 var backend: URL?
68 var backendOutput: OutputFormatSetting?
69 var keepLocalFiles: Bool?
70 var autoStart: Bool?
8e932130
RBR
71 var skipBackend: Bool
72 var maxLength: Int?
ba17de89
RBR
73}
74
75enum CapturaAction {
76 case record(RecordAction)
77 case configure(ConfigureAction)
78}
79
80struct CapturaURLDecoder {
81
82 static func decodeParams(url: URL) -> CapturaAction? {
83 guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
84 let params = components.queryItems else {
85 return nil
86 }
87
88 var paramsDict = [String: Any]()
89
90 params.forEach { item in
91 paramsDict[item.name] = item.value
92 }
93
94 guard let action = paramsDict["action"] as? String else {
95 return nil
96 }
97
98 switch action {
99 case "configure":
100 var fps = Int(paramsDict["fps"] as? String ?? "")
101 let backend = URL(string: paramsDict["backend"] as? String ?? "")
102 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
103 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
104 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
105
106 if fps != nil {
107 fps = min(10, max(4, fps!))
108 }
109
110 if backendOutput == .all {
111 backendOutput = .gifOnly
112 }
113
114 return .configure(ConfigureAction(
115 action: action,
116 fps: fps,
117 outputs: outputs,
118 backend: backend,
119 backendOutput: backendOutput,
120 keepLocalFiles: keepLocalFiles
121 ))
122
123 case "record":
124 let x = Int(paramsDict["x"] as? String ?? "")
125 let y = Int(paramsDict["y"] as? String ?? "")
126 let width = Int(paramsDict["width"] as? String ?? "")
127 let height = Int(paramsDict["height"] as? String ?? "")
128 let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
129 let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
130 var fps = Int(paramsDict["fps"] as? String ?? "")
131 let backend = URL(string: paramsDict["backend"] as? String ?? "")
132 let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
133 let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
134 var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
8e932130
RBR
135 let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
136 var maxLength = Int(paramsDict["max_length"] as? String ?? "")
ba17de89
RBR
137
138 if fps != nil {
139 fps = min(10, max(4, fps!))
140 }
141
8e932130
RBR
142 if maxLength != nil {
143 maxLength = min(300, max(1, fps!))
144 }
145
ba17de89
RBR
146 if backendOutput == .all {
147 backendOutput = .gifOnly
148 }
149
8e932130
RBR
150 var skipBackend = false
151 if let backendString = paramsDict["backend"] as? String {
152 if backendString == "" {
153 skipBackend = true
154 }
155 }
156
ba17de89
RBR
157 return .record(RecordAction(
158 action: action,
159 x: x,
160 y: y,
161 width: width,
162 height: height,
163 preventResize: preventResize,
164 preventMove: preventMove,
165 fps: fps,
166 outputs: outputs,
167 backend: backend,
168 backendOutput: backendOutput,
8e932130
RBR
169 keepLocalFiles: keepLocalFiles,
170 autoStart: autoStart,
171 skipBackend: skipBackend,
172 maxLength: maxLength
ba17de89
RBR
173 ))
174
175 default:
176 return nil
177 }
178 }
179}