-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimages.ts
637 lines (515 loc) · 18.2 KB
/
images.ts
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
// Copyright 2023 the .NET Foundation
// An image corresponds to a WWT imageset. It is owned by a handle. One or more
// images are combined into "scenes", which are the things that we show to
// users.
//
// See `SCHEMA.md` for more information about the schema used here.
import { Response } from "express";
import { Request as JwtRequest } from "express-jwt";
import { isLeft } from "fp-ts/lib/Either.js";
import * as t from "io-ts";
import { PathReporter } from "io-ts/lib/PathReporter.js";
import { ObjectId, UpdateFilter, WithId } from "mongodb";
import { create } from "xmlbuilder2";
import { XMLBuilder } from "xmlbuilder2/lib/interfaces";
import { State } from "./globals.js";
import { isAllowed as handleIsAllowed } from "./handles.js";
import { CleanHtml, SpdxExpression } from "./util.js";
export interface MongoImage {
handle_id: ObjectId;
creation_date: Date;
wwt: ImageWwtT;
storage: ImageStorageT;
note: string;
alt_text?: string;
permissions: ImagePermissionsT;
}
const ImageWwt = t.type({
base_degrees_per_tile: t.number,
bottoms_up: t.boolean,
center_x: t.number,
center_y: t.number,
file_type: t.string,
offset_x: t.number,
offset_y: t.number,
projection: t.string,
quad_tree_map: t.string,
rotation: t.number,
thumbnail_url: t.string,
tile_levels: t.number,
width_factor: t.number,
});
type ImageWwtT = t.TypeOf<typeof ImageWwt>;
const ImageStorage = t.type({
legacy_url_template: t.union([t.string, t.undefined]),
});
type ImageStorageT = t.TypeOf<typeof ImageStorage>;
const ImagePermissions = t.intersection([
t.type({
copyright: t.string,
license: SpdxExpression,
}),
t.partial({
credits: CleanHtml,
})
]);
type ImagePermissionsT = t.TypeOf<typeof ImagePermissions>;
// Authorization tools
export type ImageCapability =
"edit"
;
export async function isAllowed(state: State, req: JwtRequest, image: MongoImage, cap: ImageCapability): Promise<boolean> {
// One day we might have finer-grained permissions, but not yet. We might also
// have some kind of caching that allows us to not always look up the owning
// handle info.
const owner_handle = await state.handles.findOne({ "_id": image.handle_id });
if (owner_handle === null) {
throw new Error(`Internal database inconsistency: image missing owner ${image.handle_id}`);
}
switch (cap) {
case "edit": {
return handleIsAllowed(req, owner_handle, "editImages");
}
default: {
return false; // this is a can't-happen but might as well be safe
}
}
}
// Various data exports
export async function imageToJson(image: WithId<MongoImage>, state: State): Promise<Record<string, any>> {
const handle = await state.handles.findOne({ "_id": image.handle_id });
if (handle === null) {
throw new Error(`Database consistency failure, image ${image._id} missing handle ${image.handle_id}`);
}
const output: Record<string, any> = {
id: image._id,
handle_id: image.handle_id,
handle: {
handle: handle.handle,
display_name: handle.display_name,
},
creation_date: image.creation_date,
wwt: image.wwt,
permissions: image.permissions,
storage: image.storage,
note: image.note,
};
if (image.alt_text !== undefined) {
output.alt_text = image.alt_text;
}
return output;
}
export function imageToDisplayJson(image: WithId<MongoImage>): Record<string, any> {
const output: Record<string, any> = {
id: image._id,
wwt: image.wwt,
permissions: image.permissions,
storage: image.storage,
};
if (image.alt_text !== undefined) {
output.alt_text = image.alt_text;
}
return output;
}
export function imageToImageset(image: MongoImage, root: XMLBuilder): XMLBuilder {
const iset = root.ele("ImageSet");
// Bad hardcodings!!
iset.att("BandPass", "Visible");
iset.att("DataSetType", "Sky");
// Hardcodings that are probably OK:
iset.att("BaseTileLevel", "0");
iset.att("ElevationModel", "False");
iset.att("Generic", "False");
iset.att("Sparse", "True");
iset.att("StockSet", "False");
iset.att("BaseDegreesPerTile", String(image.wwt.base_degrees_per_tile));
iset.att("BottomsUp", image.wwt.bottoms_up ? "True" : "False");
iset.att("CenterX", String(image.wwt.center_x));
iset.att("CenterY", String(image.wwt.center_y));
iset.att("FileType", image.wwt.file_type);
iset.att("Name", image.note);
iset.att("OffsetX", String(image.wwt.offset_x));
iset.att("OffsetY", String(image.wwt.offset_y));
iset.att("Projection", image.wwt.projection);
iset.att("QuadTreeMap", image.wwt.quad_tree_map);
iset.att("Rotation", String(image.wwt.rotation));
iset.att("TileLevels", String(image.wwt.tile_levels));
iset.att("WidthFactor", String(image.wwt.width_factor));
if (image.storage.legacy_url_template) {
iset.att("Url", image.storage.legacy_url_template);
} else {
throw new Error("no derivable URL for imageset WTML");
}
iset.ele("Description").txt(image.note);
if (image.permissions.credits) {
iset.ele("Credits").txt(image.permissions.credits);
}
// TODO? CreditsUrl pointing to somewhere in Constellations frontend?
iset.ele("ThumbnailUrl").txt(image.wwt.thumbnail_url);
return iset;
}
export function initializeImageEndpoints(state: State) {
// POST /handle/:handle/image: post a new image record (data have already
// been processed and uploaded)
const ImageCreation = t.intersection([
t.type({
wwt: ImageWwt,
storage: ImageStorage,
note: t.string,
permissions: ImagePermissions,
}),
t.partial({
alt_text: t.string,
})
]);
type ImageCreationT = t.TypeOf<typeof ImageCreation>;
state.app.post(
"/handle/:handle/image",
async (req: JwtRequest, res: Response) => {
const handle_name = req.params.handle;
// Are we authorized?
const handle = await state.handles.findOne({ "handle": handle_name });
if (handle === null) {
res.statusCode = 404;
res.json({ error: true, message: "Handle not found" });
return;
}
if (!handleIsAllowed(req, handle, "addImages")) {
res.statusCode = 403;
res.json({ error: true, message: "Forbidden" });
return;
}
// Does the input look valid?
const maybe = ImageCreation.decode(req.body);
if (isLeft(maybe)) {
res.statusCode = 400;
res.json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const input: ImageCreationT = maybe.right;
// OK, looks good.
const new_rec: MongoImage = {
handle_id: handle._id,
creation_date: new Date(),
wwt: input.wwt,
storage: input.storage,
note: input.note,
permissions: input.permissions,
};
if (input.alt_text !== undefined) {
new_rec.alt_text = input.alt_text;
}
try {
const result = await state.images.insertOne(new_rec);
res.json({
error: false,
id: "" + result.insertedId,
rel_url: "/image/" + encodeURIComponent("" + result.insertedId),
});
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// POST /images/find-by-legacy-url - locate image records based on their WWT
// "legacy URL" field.
//
// This helps us bootstrap the collection by allowing us to associate existing
// images with new scenes (~WWT places). It should probably eventually become
// part of a more generic query interface.
//
// We don't (yet?) filter results by handle or anything.
const FindByLegacy = t.type({
wwt_legacy_url: t.string,
});
type FindByLegacyT = t.TypeOf<typeof FindByLegacy>;
state.app.post(
"/images/find-by-legacy-url",
async (req: JwtRequest, res: Response) => {
// No authentication required.
// Does the input look valid?
const maybe = FindByLegacy.decode(req.body);
if (isLeft(maybe)) {
res.statusCode = 400;
res.json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const input: FindByLegacyT = maybe.right;
// OK, looks good.
try {
// Don't include the WWT astrometric/data-format info, which is more
// specific than callers will generally want.
const items = await state.images.find(
{ "storage.legacy_url_template": { $eq: input.wwt_legacy_url } },
).project(
{ "_id": 1, "handle_id": 1, "creation_date": 1, "note": 1, "storage": 1 }
).toArray();
res.json({
error: false,
results: items,
});
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// GET /images/builtin-backgrounds - get the list of built-in backgrounds
state.app.get(
"/images/builtin-backgrounds",
async (req: JwtRequest, res: Response) => {
// No authentication required.
// No inputs.
try {
const items = await state.images.find(
{ builtin_background_sort_key: { $gte: 0 } },
).sort(
{ builtin_background_sort_key: 1 }
).project(
{ "_id": 1, "handle_id": 1, "creation_date": 1, "note": 1, "storage": 1 }
).toArray();
res.json({
error: false,
results: items,
});
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// GET /image/:id - information about an image
state.app.get(
"/image/:id",
async (req: JwtRequest, res: Response) => {
try {
const image = await state.images.findOne({ "_id": new ObjectId(req.params.id) });
if (image === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
const output = await imageToJson(image, state);
output["error"] = false;
res.json(output);
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// GET /image/:id/img.wtml - get WTML with a single imageset
state.app.get(
"/image/:id/img.wtml",
async (req: JwtRequest, res: Response) => {
try {
const image = await state.images.findOne({ "_id": new ObjectId(req.params.id) });
if (image === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
const root = create().ele("Folder");
root.att("Browseable", "True");
root.att("Group", "Explorer");
root.att("Name", image.note);
root.att("Searchable", "True");
root.att("Type", "Sky");
imageToImageset(image, root);
root.end({ prettyPrint: true });
res.type("application/xml")
res.send(root.toString());
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// GET /image/:id/permissions - get information about the logged-in user's
// permissions with regards to this image.
//
// Note that this API regards website authorization, not the information about
// copyright, credits, etc.
//
// This API is only informative -- of course, direct API calls are the final
// arbiters of what is and isn't allowed. But the frontend can use this
// information to decide what UI elements to expose to a user.
state.app.get(
"/image/:id/permissions",
async (req: JwtRequest, res: Response) => {
try {
const image = await state.images.findOne({ "_id": new ObjectId(req.params.id) });
if (image === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
// TODO: if we end up reporting more categories, we should somehow batch
// the checks to not look up the same handle over and over.
const edit = await isAllowed(state, req, image, "edit");
const output = {
error: false,
id: image._id,
edit: edit,
};
res.json(output);
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// PATCH /image/:id - update image properties
const ImagePatch = t.partial({
note: t.string,
alt_text: t.string,
permissions: ImagePermissions,
});
type ImagePatchT = t.TypeOf<typeof ImagePatch>;
state.app.patch(
"/image/:id",
async (req: JwtRequest, res: Response) => {
try {
// Validate inputs
const thisImage = { "_id": new ObjectId(req.params.id) };
const image = await state.images.findOne(thisImage);
if (image === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
const maybe = ImagePatch.decode(req.body);
if (isLeft(maybe)) {
res.statusCode = 400;
res.json({ error: true, message: `Submission did not match schema: ${PathReporter.report(maybe).join("\n")}` });
return;
}
const input: ImagePatchT = maybe.right;
// For this operation, we might require different permissions depending
// on what changes are exactly being requested. Note that patch
// operations should either fully succeed or fully fail -- no partial
// applications. Here we cache the `canEdit` permission since everything
// uses it.
let allowed = true;
const canEdit = await isAllowed(state, req, image, "edit");
// For convenience, this value should be pre-filled with whatever
// operations we might use below. We have to hack around the typing
// below, though, because TypeScript takes some elements here to be
// read-only.
let operation: UpdateFilter<MongoImage> = { "$set": {} };
if (input.note) {
allowed = allowed && canEdit;
// Validate this particular input. (TODO: I think io-ts could do this?)
if (input.note.length > 500) {
res.statusCode = 400;
res.json({ error: true, message: "Invalid input `note`: too long" });
return;
}
(operation as any)["$set"]["note"] = input.note;
}
if (input.alt_text) {
allowed = allowed && canEdit;
if (input.alt_text.length > 5000) {
res.statusCode = 400;
res.json({ error: true, message: "Invalid input `alt_text`: too long" });
return;
}
(operation as any)["$set"]["alt_text"] = input.alt_text;
}
if (input.permissions) {
// Validation is performed by io-ts, which checks that credits is
// CleanHtml and that license is an SPDX license identifier.
allowed = allowed && canEdit;
(operation as any)["$set"]["permissions"] = input.permissions;
}
// How did we do?
if (!allowed) {
res.statusCode = 403;
res.json({ error: true, message: "Forbidden" });
return;
}
await state.images.findOneAndUpdate(
thisImage,
operation
);
res.json({
error: false,
});
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
// GET /handle/:handle/imageinfo?page=$int&pagesize=$int - get admin
// information about images
//
// This endpoint is for the handle dashboard showing summary information about
// the handle's images.
state.app.get(
"/handle/:handle/imageinfo",
async (req: JwtRequest, res: Response) => {
try {
// Validate input(s)
const handle = await state.handles.findOne({ "handle": req.params.handle });
if (handle === null) {
res.statusCode = 404;
res.json({ error: true, message: "Not found" });
return;
}
var page_num = 0;
try {
const qpage = parseInt(req.query.page as string, 10);
if (qpage >= 0) {
page_num = qpage;
}
} catch {
res.statusCode = 400;
res.json({ error: true, message: `invalid page number` });
}
var page_size = 10;
try {
const qps = parseInt(req.query.pagesize as string, 10);
if (qps > 0 && qps <= 100) {
page_size = qps;
}
} catch {
res.statusCode = 400;
res.json({ error: true, message: `invalid page size` });
}
// Check authorization
if (!handleIsAllowed(req, handle, "viewDashboard")) {
res.statusCode = 403;
res.json({ error: true, message: "Forbidden" });
return;
}
// OK to proceed
const filter = { "handle_id": handle._id };
const count = await state.images.countDocuments(filter);
const infos = await state.images.find(filter)
.sort({ creation_date: -1 })
.skip(page_num * page_size)
.limit(page_size)
.project({ "_id": 1, "handle_id": 1, "creation_date": 1, "note": 1, "storage": 1 })
.toArray();
res.json({
error: false,
total_count: count,
results: infos,
});
} catch (err) {
console.error(`${req.method} ${req.path} exception:`, err);
res.statusCode = 500;
res.json({ error: true, message: `error serving ${req.method} ${req.path}` });
}
}
);
}