Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
372 lines (336 loc) · 11.7 KB
/
Copy pathModel.js
File metadata and controls
372 lines (336 loc) · 11.7 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Pure queue, path, and JSON helpers for the OmaXerahs service.
// Qt-free so tests can load this file under node without QML.
var PLUGIN_ID = "io.github.sharex.omaxerahs"
var SCHEMA_VERSION = 1
var QUEUE_BOUND = 8
var CAPTURE_MODES = ["smart", "region", "windows", "fullscreen"]
var TRANSIENT_CODES = ["network", "timeout"]
var NO_AUTO_RETRY_CODES = ["auth", "not_ready", "secret_store"]
var CAPTURE_DELAY_MIN = 0
var CAPTURE_DELAY_MAX = 60
var CAPTURE_DELAY_DEFAULT = 3
var SCREENSHOT_BASENAME = /^screenshot-[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}(?:-[A-Za-z0-9_-]+)?\.png$/
function isCaptureMode(mode) {
var value = String(mode === undefined || mode === null ? "" : mode)
for (var i = 0; i < CAPTURE_MODES.length; i++) {
if (CAPTURE_MODES[i] === value) return true
}
return false
}
function clampDelaySeconds(value) {
var n = Number(value)
if (!isFinite(n) || isNaN(n)) return CAPTURE_DELAY_DEFAULT
n = Math.floor(n)
if (n < CAPTURE_DELAY_MIN) return CAPTURE_DELAY_MIN
if (n > CAPTURE_DELAY_MAX) return CAPTURE_DELAY_MAX
return n
}
function basename(path) {
var value = String(path === undefined || path === null ? "" : path)
var cut = value.lastIndexOf("/")
if (cut === -1) cut = value.lastIndexOf("\\")
return cut === -1 ? value : value.substring(cut + 1)
}
function isHttpUrl(url) {
if (typeof url !== "string") return false
return url.indexOf("http://") === 0 || url.indexOf("https://") === 0
}
function hostFromUrl(url) {
if (!isHttpUrl(url)) return ""
var rest = url.substring(url.indexOf("://") + 3)
var slash = rest.indexOf("/")
var hostport = slash === -1 ? rest : rest.substring(0, slash)
var at = hostport.lastIndexOf("@")
if (at !== -1) hostport = hostport.substring(at + 1)
return hostport
}
function errorObject(code, message) {
return {
ok: false,
error: {
code: String(code || "provider"),
message: String(message || code || "error")
}
}
}
function errorJson(code, message) {
return JSON.stringify(errorObject(code, message))
}
function acceptedJson(state, extra) {
var payload = {
ok: true,
accepted: true,
schemaVersion: SCHEMA_VERSION,
pluginId: PLUGIN_ID,
state: String(state || "")
}
if (extra && typeof extra === "object") {
for (var key in extra) payload[key] = extra[key]
}
return JSON.stringify(payload)
}
function parseOneJsonObject(text) {
var raw = String(text === undefined || text === null ? "" : text)
var trimmed = raw.replace(/^\uFEFF/, "").trim()
if (!trimmed) return errorObject("invalid_json", "stdout was empty")
var value
try {
value = JSON.parse(trimmed)
} catch (e) {
return errorObject("invalid_json", "stdout was not exactly one JSON object")
}
if (!value || typeof value !== "object" || Array.isArray(value)) {
return errorObject("invalid_json", "stdout was not a JSON object")
}
return { ok: true, value: value }
}
function acceptUpload(exitCode, stdout) {
var parsed = parseOneJsonObject(stdout)
if (Number(exitCode) !== 0) {
if (parsed.ok && parsed.value && parsed.value.error) {
return errorObject(parsed.value.error.code || "provider", parsed.value.error.message || "omaxerahs upload failed")
}
if (!parsed.ok) return parsed
return errorObject("provider", "omaxerahs upload failed")
}
if (!parsed.ok) return parsed
var value = parsed.value
if (value.ok !== true) {
if (value.error) {
return errorObject(value.error.code || "provider", value.error.message || "upload was not successful")
}
return errorObject("provider", "upload was not successful")
}
if (!isHttpUrl(value.url)) {
return errorObject("provider", "upload result did not include an http(s) URL")
}
return { ok: true, value: value }
}
function validateScreenshotPath(path) {
var value = String(path === undefined || path === null ? "" : path)
if (value !== value.trim()) {
return errorObject("invalid_path", "path has surrounding whitespace")
}
if (!value) return errorObject("invalid_path", "path is empty")
if (value.indexOf("\0") !== -1 || value.indexOf("\n") !== -1 || value.indexOf("\r") !== -1) {
return errorObject("invalid_path", "path contains invalid characters")
}
if (value.charAt(0) !== "/") {
return errorObject("invalid_path", "path is not absolute")
}
var name = basename(value)
if (!SCREENSHOT_BASENAME.test(name)) {
return errorObject("invalid_path", "basename is not an Omarchy screenshot PNG")
}
return { ok: true, path: value, filename: name }
}
function capabilitiesCompatible(obj) {
if (!obj || typeof obj !== "object") return false
if (Number(obj.schemaVersion) !== SCHEMA_VERSION) return false
var min = obj.minPluginProtocol
if (min !== undefined && min !== null && Number(min) > 1) return false
if (!Array.isArray(obj.capabilities)) return false
var doctorImage = false
var uploadImage = false
for (var i = 0; i < obj.capabilities.length; i++) {
if (obj.capabilities[i] === "doctor.image") doctorImage = true
if (obj.capabilities[i] === "upload.image") uploadImage = true
}
return doctorImage && uploadImage
}
function doctorReady(obj) {
return !!(obj && obj.ok === true && obj.image && obj.image.ready === true)
}
function isTransientError(code) {
var value = String(code || "")
for (var i = 0; i < TRANSIENT_CODES.length; i++) {
if (TRANSIENT_CODES[i] === value) return true
}
return false
}
function noAutoRetry(code) {
var value = String(code || "")
for (var i = 0; i < NO_AUTO_RETRY_CODES.length; i++) {
if (NO_AUTO_RETRY_CODES[i] === value) return true
}
return false
}
function retryDelayMs(attemptsAlreadyMade) {
if (Number(attemptsAlreadyMade) <= 1) return 2000
return 8000
}
function canRetryAttempt(attemptsAlreadyMade, code) {
if (noAutoRetry(code) || !isTransientError(code)) return false
return Number(attemptsAlreadyMade) < 3
}
function queueContains(queue, path) {
var list = Array.isArray(queue) ? queue : []
for (var i = 0; i < list.length; i++) {
if (list[i] && list[i].path === path) return true
}
return false
}
function isClaimed(queue, inFlightPath, path) {
if (!path) return false
if (inFlightPath && inFlightPath === path) return true
return queueContains(queue, path)
}
function canEnqueue(queue, inFlightPath, path) {
var check = validateScreenshotPath(path)
if (!check.ok) return check
if (isClaimed(queue, inFlightPath, check.path)) {
return errorObject("duplicate", "path is already queued or in flight")
}
var list = Array.isArray(queue) ? queue : []
if (list.length >= QUEUE_BOUND) {
return errorObject("queue_full", "Upload queue is full (" + QUEUE_BOUND + ")")
}
return { ok: true, path: check.path, filename: check.filename }
}
function enqueue(queue, path, attempts) {
var list = Array.isArray(queue) ? queue.slice() : []
list.push({
path: path,
attempts: Number(attempts || 0)
})
return list
}
function dequeue(queue) {
var list = Array.isArray(queue) ? queue : []
if (list.length === 0) return { queue: list, item: null }
return { queue: list.slice(1), item: list[0] }
}
function claimPath(claimed, path) {
var next = {}
var existing = claimed && typeof claimed === "object" ? claimed : {}
for (var key in existing) next[key] = existing[key]
next[path] = true
return next
}
function readinessMessage(code) {
switch (String(code || "")) {
case "ready":
return ""
case "cli_missing":
return "omaxerahs is not on PATH. Install native XerahS so /usr/bin/omaxerahs exists."
case "cli_incompatible":
return "omaxerahs is too old or incompatible. Update the native XerahS package."
case "cli_flatpak":
return "Flatpak XerahS is not supported. Install native xerahs so /usr/bin/omaxerahs is on PATH."
case "image_not_ready":
return "Configure an image or file destination in XerahS (Settings > Destination Settings), then run omaxerahs doctor --json."
case "secret_store":
return "The XerahS secret store is not ready. Unlock libsecret or re-enter destination credentials."
case "secret_store_fallback":
return "XerahS is using the AES secret-store fallback instead of libsecret."
default:
return "OmaXerahs is not ready."
}
}
function errorMessage(code) {
switch (String(code || "")) {
case "":
return ""
case "timeout":
return "Timed out waiting for capture or upload."
case "network":
return "Network error while uploading screenshot."
case "auth":
return "Destination authentication failed. Check credentials in XerahS."
case "not_ready":
return "No usable image or file destination is configured in XerahS."
case "secret_store":
return "XerahS could not access the secret store."
case "invalid_path":
return "Captured screenshot path could not be verified."
case "invalid_json":
return "Unexpected output from omaxerahs CLI."
case "queue_full":
return "Upload queue is full."
case "cancelled":
return "Operation was cancelled."
case "provider":
return "Capture or upload provider reported an error."
default:
return "Last operation failed (" + String(code) + ")."
}
}
function lastResult(fields) {
var src = fields && typeof fields === "object" ? fields : {}
return {
ok: src.ok === true,
localPath: src.localPath ? String(src.localPath) : "",
host: src.host ? String(src.host) : "",
filename: src.filename ? String(src.filename) : "",
at: src.at ? String(src.at) : "",
errorCode: src.errorCode === undefined ? null : src.errorCode
}
}
function persistableLast(last) {
var row = lastResult(last)
return {
ok: row.ok,
localPath: row.localPath,
host: row.host,
filename: row.filename,
at: row.at,
errorCode: row.errorCode
}
}
function statusPayload(fields) {
var src = fields && typeof fields === "object" ? fields : {}
return {
schemaVersion: SCHEMA_VERSION,
pluginId: PLUGIN_ID,
state: String(src.state || "not_ready"),
readiness: String(src.readiness || "cli_missing"),
queueLength: Number(src.queueLength || 0),
copyUrlToClipboard: src.copyUrlToClipboard !== false,
notifyOnComplete: src.notifyOnComplete !== false,
openUrlOnNotificationClick: src.openUrlOnNotificationClick === true,
captureMode: isCaptureMode(src.captureMode) ? String(src.captureMode) : "smart",
delaySeconds: clampDelaySeconds(src.delaySeconds),
countdownRemaining: Number(src.countdownRemaining || 0),
last: src.last === undefined ? null : src.last
}
}
if (typeof module !== "undefined") {
module.exports = {
PLUGIN_ID: PLUGIN_ID,
SCHEMA_VERSION: SCHEMA_VERSION,
QUEUE_BOUND: QUEUE_BOUND,
CAPTURE_MODES: CAPTURE_MODES,
CAPTURE_DELAY_MIN: CAPTURE_DELAY_MIN,
CAPTURE_DELAY_MAX: CAPTURE_DELAY_MAX,
CAPTURE_DELAY_DEFAULT: CAPTURE_DELAY_DEFAULT,
SCREENSHOT_BASENAME: SCREENSHOT_BASENAME,
isCaptureMode: isCaptureMode,
clampDelaySeconds: clampDelaySeconds,
basename: basename,
isHttpUrl: isHttpUrl,
hostFromUrl: hostFromUrl,
errorObject: errorObject,
errorJson: errorJson,
acceptedJson: acceptedJson,
parseOneJsonObject: parseOneJsonObject,
acceptUpload: acceptUpload,
validateScreenshotPath: validateScreenshotPath,
capabilitiesCompatible: capabilitiesCompatible,
doctorReady: doctorReady,
isTransientError: isTransientError,
noAutoRetry: noAutoRetry,
retryDelayMs: retryDelayMs,
canRetryAttempt: canRetryAttempt,
queueContains: queueContains,
isClaimed: isClaimed,
canEnqueue: canEnqueue,
enqueue: enqueue,
dequeue: dequeue,
claimPath: claimPath,
readinessMessage: readinessMessage,
errorMessage: errorMessage,
lastResult: lastResult,
persistableLast: persistableLast,
statusPayload: statusPayload
}
}