aboutsummaryrefslogtreecommitdiff
path: root/Captura/Data/CapturaURLDecoder.swift
blob: f4c325b0d56c7ff9f597716b9b36a415cc867242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Foundation


protocol ConfigureActionProtocol {
  var action: String { get }
  var fps: Int? { get }
  var outputs: OutputFormatSetting? { get }
  var backend: URL? { get }
  var backendOutput: OutputFormatSetting? { get }
  var keepLocalFiles: Bool? { get }
}

protocol RecordActionProtocol {
  var action: String { get }
  
  var x: Int? { get }
  var y: Int? { get }
  var width: Int? { get }
  var height: Int? { get }
  var preventResize: Bool? { get }
  var preventMove: Bool? { get }
  var fps: Int? { get }
  var backend: URL? { get }
  var outputs: OutputFormatSetting? { get }
  var backendOutput: OutputFormatSetting? { get }
  var keepLocalFiles: Bool? { get }
  var autoStart: Bool? { get }
  var skipBackend: Bool { get }
  var maxLength: Int? { get }
}

// The concrete implementations
struct ConfigureAction: ConfigureActionProtocol {
  let action: String
  var fps: Int?
  var outputs: OutputFormatSetting?
  var backend: URL?
  var backendOutput: OutputFormatSetting?
  var keepLocalFiles: Bool?
}

struct RecordAction: RecordActionProtocol {
  let action: String
  var x: Int?
  var y: Int?
  var width: Int?
  var height: Int?
  var preventResize: Bool?
  var preventMove: Bool?
  var fps: Int?
  var outputs: OutputFormatSetting?
  var backend: URL?
  var backendOutput: OutputFormatSetting?
  var keepLocalFiles: Bool?
  var autoStart: Bool?
  var skipBackend: Bool
  var maxLength: Int?
}

enum CapturaAction {
    case record(RecordAction)
    case configure(ConfigureAction)
}

struct CapturaURLDecoder {
  
  static func decodeParams(url: URL) -> CapturaAction? {
      guard let components = URLComponents(url: url, resolvingAgainstBaseURL: false),
            let params = components.queryItems else {
          return nil
      }

      var paramsDict = [String: Any]()

      params.forEach { item in
          paramsDict[item.name] = item.value
      }

      guard let action = paramsDict["action"] as? String else {
          return nil
      }

      switch action {
      case "configure":
        var fps = Int(paramsDict["fps"] as? String ?? "")
        let backend = URL(string: paramsDict["backend"] as? String ?? "")
        let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
        let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
        var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
        
        if fps != nil {
          fps = min(10, max(4, fps!))
        }
        
        if backendOutput == .all {
          backendOutput = .gifOnly
        }
        
        return .configure(ConfigureAction(
          action: action,
          fps: fps,
          outputs: outputs,
          backend: backend,
          backendOutput: backendOutput,
          keepLocalFiles: keepLocalFiles
        ))

      case "record":
        let x = Int(paramsDict["x"] as? String ?? "")
        let y = Int(paramsDict["y"] as? String ?? "")
        let width = Int(paramsDict["width"] as? String ?? "")
        let height = Int(paramsDict["height"] as? String ?? "")
        let preventResize = Bool(paramsDict["prevent_resize"] as? String ?? "")
        let preventMove = Bool(paramsDict["prevent_move"] as? String ?? "")
        var fps = Int(paramsDict["fps"] as? String ?? "")
        let backend = URL(string: paramsDict["backend"] as? String ?? "")
        let keepLocalFiles = Bool(paramsDict["keep_local_files"] as? String ?? "")
        let outputs = OutputFormatSetting(paramsDict["outputs"] as? String ?? "")
        var backendOutput = OutputFormatSetting(paramsDict["backend_output"] as? String ?? "")
        let autoStart = Bool(paramsDict["auto_start"] as? String ?? "")
        var maxLength = Int(paramsDict["max_length"] as? String ?? "")
        
        if fps != nil {
          fps = min(10, max(4, fps!))
        }
        
        if maxLength != nil {
          maxLength = min(300, max(1, fps!))
        }
        
        if backendOutput == .all {
          backendOutput = .gifOnly
        }
        
        var skipBackend = false
        if let backendString = paramsDict["backend"] as? String {
          if backendString == "" {
            skipBackend = true
          }
        }
        
        return .record(RecordAction(
          action: action,
          x: x,
          y: y,
          width: width,
          height: height,
          preventResize: preventResize,
          preventMove: preventMove,
          fps: fps,
          outputs: outputs,
          backend: backend,
          backendOutput: backendOutput,
          keepLocalFiles: keepLocalFiles,
          autoStart: autoStart,
          skipBackend: skipBackend,
          maxLength: maxLength
        ))

      default:
          return nil
      }
  }
}